Class: GameState
- Inherits:
-
Object
- Object
- GameState
- Defined in:
- lib/software_challenge_client/game_state.rb
Overview
The state of a game, as received from the server.
Constant Summary collapse
- POINTS_PER_TILE =
5
- POINTS_PER_PASSENGER =
5
Instance Attribute Summary collapse
-
#additional_free_turn_after_push ⇒ Boolean
(also: #additional_free_turn_after_push?)
available.
-
#blue ⇒ Player
readonly
The blue player.
-
#board ⇒ Board
The game’s board.
-
#condition ⇒ Condition
The winner and winning reason.
-
#current_player_color ⇒ PlayerColor
The current player’s color.
-
#free_acceleration ⇒ Boolean
(also: #free_acceleration?)
True if the free acceleration for this turn is still available.
-
#free_turn ⇒ Boolean
(also: #free_turn?)
available.
-
#lastMove ⇒ Move
The last move, that was made.
-
#red ⇒ Player
readonly
The red player.
-
#start_player_color ⇒ PlayerColor
The start-player’s color.
-
#turn ⇒ Integer
Turn number.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compared with other state.
-
#add_player(player) ⇒ Object
adds a player to the gamestate.
-
#current_player ⇒ Player
gets the current player.
-
#deep_clone ⇒ Object
Create a deep copy of the gamestate.
-
#game_ended? ⇒ Boolean
has the game ended?.
-
#initialize ⇒ GameState
constructor
A new instance of GameState.
-
#occupied_by_other_player?(field) ⇒ Boolean
True if the given field is occupied by the other (not current) player.
-
#other_player ⇒ Player
gets the other (not the current) player.
-
#other_player_color ⇒ PlayerColor
gets the other (not the current) player’s color.
-
#perform!(move, player) ⇒ Object
performs a move on the gamestate.
-
#points_for_player(player) ⇒ Integer
calculates a player’s points based on the current gamestate.
-
#round ⇒ Integer
gets the current round.
-
#winner ⇒ Player
gets the game’s winner.
-
#winning_reason ⇒ String
gets the winning reason.
Constructor Details
#initialize ⇒ GameState
Returns a new instance of GameState.
56 57 58 59 60 61 62 63 |
# File 'lib/software_challenge_client/game_state.rb', line 56 def initialize @current_player_color = PlayerColor::RED @start_player_color = PlayerColor::RED @board = Board.new @free_acceleration = true @free_turn = true @additional_free_turn_after_push = false end |
Instance Attribute Details
#additional_free_turn_after_push ⇒ Boolean Also known as: additional_free_turn_after_push?
available.
50 51 52 |
# File 'lib/software_challenge_client/game_state.rb', line 50 def additional_free_turn_after_push @additional_free_turn_after_push end |
#blue ⇒ Player (readonly)
Returns the blue player.
25 26 27 |
# File 'lib/software_challenge_client/game_state.rb', line 25 def blue @blue end |
#board ⇒ Board
Returns the game’s board.
28 29 30 |
# File 'lib/software_challenge_client/game_state.rb', line 28 def board @board end |
#condition ⇒ Condition
Returns the winner and winning reason.
34 35 36 |
# File 'lib/software_challenge_client/game_state.rb', line 34 def condition @condition end |
#current_player_color ⇒ PlayerColor
Returns the current player’s color.
19 20 21 |
# File 'lib/software_challenge_client/game_state.rb', line 19 def current_player_color @current_player_color end |
#free_acceleration ⇒ Boolean Also known as: free_acceleration?
Returns true if the free acceleration for this turn is still available.
38 39 40 |
# File 'lib/software_challenge_client/game_state.rb', line 38 def free_acceleration @free_acceleration end |
#free_turn ⇒ Boolean Also known as: free_turn?
available.
44 45 46 |
# File 'lib/software_challenge_client/game_state.rb', line 44 def free_turn @free_turn end |
#lastMove ⇒ Move
Returns the last move, that was made.
31 32 33 |
# File 'lib/software_challenge_client/game_state.rb', line 31 def lastMove @lastMove end |
#red ⇒ Player (readonly)
Returns the red player.
22 23 24 |
# File 'lib/software_challenge_client/game_state.rb', line 22 def red @red end |
#start_player_color ⇒ PlayerColor
Returns the start-player’s color.
16 17 18 |
# File 'lib/software_challenge_client/game_state.rb', line 16 def start_player_color @start_player_color end |
#turn ⇒ Integer
Returns turn number.
13 14 15 |
# File 'lib/software_challenge_client/game_state.rb', line 13 def turn @turn end |
Instance Method Details
#==(other) ⇒ Object
Compared with other state.
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/software_challenge_client/game_state.rb', line 160 def ==(other) turn == other.turn && start_player_color == other.start_player_color && current_player_color == other.current_player_color && red == other.red && blue == other.blue && board == other.board && lastMove == other.lastMove && free_acceleration == other.free_acceleration && condition == other.condition end |
#add_player(player) ⇒ Object
adds a player to the gamestate
68 69 70 71 72 73 74 |
# File 'lib/software_challenge_client/game_state.rb', line 68 def add_player(player) if player.color == PlayerColor::RED @red = player elsif player.color == PlayerColor::BLUE @blue = player end end |
#current_player ⇒ Player
gets the current player
79 80 81 82 83 84 |
# File 'lib/software_challenge_client/game_state.rb', line 79 def current_player if current_player_color == PlayerColor::RED then red else blue end end |
#deep_clone ⇒ Object
Create a deep copy of the gamestate. Can be used to perform moves on without changing the original gamestate.
174 175 176 |
# File 'lib/software_challenge_client/game_state.rb', line 174 def deep_clone Marshal.load(Marshal.dump(self)) end |
#game_ended? ⇒ Boolean
has the game ended?
124 125 126 |
# File 'lib/software_challenge_client/game_state.rb', line 124 def game_ended? !condition.nil? end |
#occupied_by_other_player?(field) ⇒ Boolean
Returns true if the given field is occupied by the other (not current) player.
155 156 157 |
# File 'lib/software_challenge_client/game_state.rb', line 155 def occupied_by_other_player?(field) field.x == other_player.x && field.y == other_player.y end |
#other_player ⇒ Player
gets the other (not the current) player
89 90 91 92 93 94 95 |
# File 'lib/software_challenge_client/game_state.rb', line 89 def other_player if current_player_color == PlayerColor::RED return blue else return red end end |
#other_player_color ⇒ PlayerColor
gets the other (not the current) player’s color
100 101 102 |
# File 'lib/software_challenge_client/game_state.rb', line 100 def other_player_color PlayerColor.opponent_color(current_player_color) end |
#perform!(move, player) ⇒ Object
performs a move on the gamestate
115 116 117 118 119 |
# File 'lib/software_challenge_client/game_state.rb', line 115 def perform!(move, player) move.actions.each do |action| action.perform!(self, player) end end |
#points_for_player(player) ⇒ Integer
calculates a player’s points based on the current gamestate
146 147 148 149 150 151 |
# File 'lib/software_challenge_client/game_state.rb', line 146 def points_for_player(player) player_field = board.field(player.x, player.y) POINTS_PER_TILE * player_field.index + POINTS_PER_PASSENGER * player.passengers + player_field.points end |
#round ⇒ Integer
gets the current round
107 108 109 |
# File 'lib/software_challenge_client/game_state.rb', line 107 def round turn / 2 end |
#winner ⇒ Player
gets the game’s winner
131 132 133 |
# File 'lib/software_challenge_client/game_state.rb', line 131 def winner condition.nil? ? nil : condition.winner end |
#winning_reason ⇒ String
gets the winning reason
138 139 140 |
# File 'lib/software_challenge_client/game_state.rb', line 138 def winning_reason condition.nil? ? nil : condition.reason end |