Class: GameEngine::GameState

Inherits:
Object
  • Object
show all
Defined in:
lib/smack_engine/game_state.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game_data) ⇒ GameState

Returns a new instance of GameState.



6
7
8
9
10
11
12
13
# File 'lib/smack_engine/game_state.rb', line 6

def initialize(game_data)
  @id = game_data.id
  @player_one = GameEngine::Player.new(game_data.player_one)
  @player_two = GameEngine::Player.new(game_data.player_two)
  @round = 0
  @phase = :setup
  @time = Time.now
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/smack_engine/game_state.rb', line 3

def id
  @id
end

#phaseObject

Returns the value of attribute phase.



4
5
6
# File 'lib/smack_engine/game_state.rb', line 4

def phase
  @phase
end

#player_oneObject (readonly)

Returns the value of attribute player_one.



3
4
5
# File 'lib/smack_engine/game_state.rb', line 3

def player_one
  @player_one
end

#player_twoObject (readonly)

Returns the value of attribute player_two.



3
4
5
# File 'lib/smack_engine/game_state.rb', line 3

def player_two
  @player_two
end

#roundObject

Returns the value of attribute round.



4
5
6
# File 'lib/smack_engine/game_state.rb', line 4

def round
  @round
end

#timeObject

Returns the value of attribute time.



4
5
6
# File 'lib/smack_engine/game_state.rb', line 4

def time
  @time
end

Instance Method Details

#target_player(player_id) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
# File 'lib/smack_engine/game_state.rb', line 15

def target_player(player_id)
  return player_one if player_one.id.to_i == player_id.to_i
  return player_two if player_two.id.to_i == player_id.to_i
  raise ArgumentError.new("No player found with id = #{player_id}")
end

#winnerObject



21
22
23
24
# File 'lib/smack_engine/game_state.rb', line 21

def winner
  return player_one if player_one.points > 0 && player_two.points <= 0
  return player_two if player_two.points > 0 && player_one.points <= 0
end

#won?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/smack_engine/game_state.rb', line 26

def won?
  winner != nil
end