Class: GameState

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

Overview

Ein Spielzustand. Wird vom Server an die Computerspieler übermittelt und enthält alles, was der Computerspieler wissen muss, um einen Zug zu machen.

Um eine Liste der gerade möglichen Züge zu bekommen, gibt es die Methode #possible_moves.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGameState

Erstellt einen neuen Spielzustand.


44
45
46
47
48
49
# File 'lib/software_challenge_client/game_state.rb', line 44

def initialize
  @current_player_color = PlayerColor::RED
  @start_player_color = PlayerColor::RED
  @board = Board.new
  @turn = 0
end

Instance Attribute Details

#bluePlayer (readonly)


27
28
29
# File 'lib/software_challenge_client/game_state.rb', line 27

def blue
  @blue
end

#boardBoard


30
31
32
# File 'lib/software_challenge_client/game_state.rb', line 30

def board
  @board
end

#conditionCondition


36
37
38
# File 'lib/software_challenge_client/game_state.rb', line 36

def condition
  @condition
end

#current_player_colorPlayerColor


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

def current_player_color
  @current_player_color
end

#last_moveMove


33
34
35
# File 'lib/software_challenge_client/game_state.rb', line 33

def last_move
  @last_move
end

#redPlayer (readonly)


24
25
26
# File 'lib/software_challenge_client/game_state.rb', line 24

def red
  @red
end

#start_player_colorPlayerColor


18
19
20
# File 'lib/software_challenge_client/game_state.rb', line 18

def start_player_color
  @start_player_color
end

#turnInteger


15
16
17
# File 'lib/software_challenge_client/game_state.rb', line 15

def turn
  @turn
end

Instance Method Details

#==(other) ⇒ Object


114
115
116
117
118
119
120
121
122
123
# File 'lib/software_challenge_client/game_state.rb', line 114

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 &&
      condition == other.condition
end

#add_player(player) ⇒ Object

Fügt einen Spieler zum Spielzustand hinzu.


54
55
56
57
58
59
60
# File 'lib/software_challenge_client/game_state.rb', line 54

def add_player(player)
  if player.color == PlayerColor::RED
    @red = player
  elsif player.color == PlayerColor::BLUE
    @blue = player
  end
end

#current_playerPlayer


63
64
65
66
# File 'lib/software_challenge_client/game_state.rb', line 63

def current_player
  return red if current_player_color == PlayerColor::RED
  return blue if current_player_color == PlayerColor::BLUE
end

#deep_cloneObject

Erzeugt eine Kopie des Spielzustandes. Änderungen an dieser Kopie beeinflussen den originalen Spielzustand nicht. Die Kopie kann also zum testen von Spielzügen genutzt werden.


126
127
128
# File 'lib/software_challenge_client/game_state.rb', line 126

def deep_clone
  Marshal.load(Marshal.dump(self))
end

#field(x, y) ⇒ Object

Zugriff auf ein Feld des Spielbrettes. Siehe Board#field.


39
40
41
# File 'lib/software_challenge_client/game_state.rb', line 39

def field(x, y)
  board.field(x, y)
end

#game_ended?Boolean


92
93
94
# File 'lib/software_challenge_client/game_state.rb', line 92

def game_ended?
  !condition.nil?
end

#other_playerPlayer


69
70
71
72
# File 'lib/software_challenge_client/game_state.rb', line 69

def other_player
  return blue if current_player_color == PlayerColor::RED
  return red if current_player_color == PlayerColor::BLUE
end

#other_player_colorPlayerColor


75
76
77
# File 'lib/software_challenge_client/game_state.rb', line 75

def other_player_color
  PlayerColor.opponent_color(current_player_color)
end

#own_fieldsArray<Field>


136
137
138
139
140
# File 'lib/software_challenge_client/game_state.rb', line 136

def own_fields
  board.fields_of_type(
    PlayerColor.field_type(current_player_color)
  )
end

#perform!(move) ⇒ Object

Führt einen Zug auf dem Spielzustand aus. Das Spielbrett wird entsprechend modifiziert.


87
88
89
# File 'lib/software_challenge_client/game_state.rb', line 87

def perform!(move)
  move.perform!(self)
end

#points_for_player(player) ⇒ Integer

Ermittelt die Punkte eines Spielers. Wenn das Spiel durch Erreichen des Rundenlimits beendet wird, hat der Spieler mit den meisten Punkten gewonnen.


110
111
112
# File 'lib/software_challenge_client/game_state.rb', line 110

def points_for_player(player)
  GameRuleLogic.swarm_size(board, player)
end

#possible_movesArray<Move>


143
144
145
146
147
# File 'lib/software_challenge_client/game_state.rb', line 143

def possible_moves
  own_fields.map do |f|
    GameRuleLogic.possible_moves(board, f, current_player_color)
  end.flatten
end

#roundInteger


80
81
82
# File 'lib/software_challenge_client/game_state.rb', line 80

def round
  turn / 2
end

#switch_current_playerObject

Wechselt den Spieler, der aktuell an der Reihe ist.


131
132
133
# File 'lib/software_challenge_client/game_state.rb', line 131

def switch_current_player
  current_player_color = other_player_color
end

#winnerPlayer


97
98
99
# File 'lib/software_challenge_client/game_state.rb', line 97

def winner
  condition.nil? ? nil : condition.winner
end

#winning_reasonString


102
103
104
# File 'lib/software_challenge_client/game_state.rb', line 102

def winning_reason
  condition.nil? ? nil : condition.reason
end