Class: MazeGame
- Inherits:
-
Object
- Object
- MazeGame
- Defined in:
- lib/maze/game/maze_game.rb
Instance Method Summary collapse
- #all_players_reached_exit? ⇒ Boolean
-
#initialize(clients) ⇒ MazeGame
constructor
A new instance of MazeGame.
- #maze(client) ⇒ Object
- #move(client, orientation) ⇒ Object
- #moves ⇒ Object
- #player_reached_exit?(client) ⇒ Boolean
- #print_current_maze ⇒ Object
- #reached_player_exit? ⇒ Boolean
- #show_next_moves(client) ⇒ Object
- #winning_players ⇒ Object
Constructor Details
#initialize(clients) ⇒ MazeGame
Returns a new instance of MazeGame.
6 7 8 9 10 11 12 13 |
# File 'lib/maze/game/maze_game.rb', line 6 def initialize(clients) @players = Hash.new @maze = Maze.new(30, 30) start_position = rand_start_position clients.each do |_, client| @players[client] = MazePlayer.new(start_position, client.name) end end |
Instance Method Details
#all_players_reached_exit? ⇒ Boolean
45 46 47 |
# File 'lib/maze/game/maze_game.rb', line 45 def all_players_reached_exit? @players.reject{|client, _| player_reached_exit? client }.empty? end |
#maze(client) ⇒ Object
32 33 34 |
# File 'lib/maze/game/maze_game.rb', line 32 def maze(client) @maze.to_s_for_player(client.number, @players[client].current_position) end |
#move(client, orientation) ⇒ Object
20 21 22 |
# File 'lib/maze/game/maze_game.rb', line 20 def move(client, orientation) @players[client].move(orientation) end |
#moves ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/maze/game/maze_game.rb', line 24 def moves player_moves = {} @players.each do |client, player| player_moves[client.name] = player.moves end player_moves end |
#player_reached_exit?(client) ⇒ Boolean
41 42 43 |
# File 'lib/maze/game/maze_game.rb', line 41 def player_reached_exit?(client) @maze.exit?(@players[client].current_position) end |
#print_current_maze ⇒ Object
36 37 38 39 |
# File 'lib/maze/game/maze_game.rb', line 36 def print_current_maze puts 'Maze Field' puts @maze.to_s end |
#reached_player_exit? ⇒ Boolean
49 50 51 |
# File 'lib/maze/game/maze_game.rb', line 49 def reached_player_exit? !winning_players.empty? end |
#show_next_moves(client) ⇒ Object
15 16 17 18 |
# File 'lib/maze/game/maze_game.rb', line 15 def show_next_moves(client) player_position = @players[client].current_position @maze.possible_directions player_position end |
#winning_players ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/maze/game/maze_game.rb', line 53 def winning_players winning_players = [] @players.each do |_, player| winning_players << player if @maze.exit?(player.current_position) end winning_players.map { |player| player.name } end |