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 leeren Spielstand.



53
54
55
56
# File 'lib/software_challenge_client/game_state.rb', line 53

def initialize
  @board = Board.new
  @turn = 0
end

Instance Attribute Details

#boardBoard



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

def board
  @board
end

#conditionCondition



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

def condition
  @condition
end

#current_playerPlayer



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

def current_player
  @current_player
end

#myself_playerPlayer



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

def myself_player
  @myself_player
end

#player_onePlayer (readonly)



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

def player_one
  @player_one
end

#player_twoPlayer (readonly)



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

def player_two
  @player_two
end

#start_playerPlayer



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

def start_player
  @start_player
end

#turnInteger



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

def turn
  @turn
end

Instance Method Details

#==(other) ⇒ Object

TODO: Fix



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/software_challenge_client/game_state.rb', line 162

def ==(other)
  turn == other.turn &&
    myself_player == other.myself_player &&
    current_player == other.current_player &&
    blue == other.blue &&
    yellow == other.yellow &&
    red == other.red &&
    green == other.green &&
    board == other.board &&
    lastMove == other.lastMove &&
    condition == other.condition
end

#add_player(player) ⇒ Object

Fügt einen Spieler zum Spielzustand hinzu.



61
62
63
64
65
66
67
68
# File 'lib/software_challenge_client/game_state.rb', line 61

def add_player(player)
  case player.team
  when Team::ONE
    @player_one = player
  when Team::TWO
    @player_two = player
  end
end

#can_move?(player) ⇒ Boolean

Überprüft ob der gegebene Spieler ziehen könnte oder blockiert ist



123
124
125
126
127
128
129
130
131
132
# File 'lib/software_challenge_client/game_state.rb', line 123

def can_move?(player)
  can = false

  for f in board.fields_of_team(player.team) do
    n = board.neighbors_of(f)
    can &= n.any? { |x| x.free? }
  end

  can
end

#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.



178
179
180
# File 'lib/software_challenge_client/game_state.rb', line 178

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

#field(x, y) ⇒ Object

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



48
49
50
# File 'lib/software_challenge_client/game_state.rb', line 48

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

#game_ended?Boolean



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

def game_ended?
  !condition.nil?
end

#is_first_round?Bool



107
108
109
# File 'lib/software_challenge_client/game_state.rb', line 107

def is_first_round?
  round == 1
end

#not_player(p) ⇒ Player



76
77
78
79
80
81
82
# File 'lib/software_challenge_client/game_state.rb', line 76

def not_player(p)
  if p == player_one
    player_two
  else
    player_one
  end
end

#other_playerPlayer



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

def other_player
  current_player == player_one ? player_two : player_one
end

#other_teamTeam



85
86
87
# File 'lib/software_challenge_client/game_state.rb', line 85

def other_team
  other_player.type
end

#own_fieldsArray<Field>



183
184
185
# File 'lib/software_challenge_client/game_state.rb', line 183

def own_fields
  board.fields_of_team(current_player.team)
end

#perform!(move) ⇒ Object

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



115
116
117
# File 'lib/software_challenge_client/game_state.rb', line 115

def perform!(move)
  GameRuleLogic.perform_move(self, move)
end

#player_from_team(team) ⇒ Player

Findet den Spieler für ein Team.



93
94
95
96
97
98
99
# File 'lib/software_challenge_client/game_state.rb', line 93

def player_from_team(team)
  if team == Team::ONE
    @player_one
  else
    @player_two
  end
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.



157
158
159
# File 'lib/software_challenge_client/game_state.rb', line 157

def points_for_player(player)
  player.nil? ? 0 : player.fishes
end

#roundInteger



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

def round
  turn / 2 + 1
end

#winnerPlayer



142
143
144
# File 'lib/software_challenge_client/game_state.rb', line 142

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

#winning_reasonString



148
149
150
# File 'lib/software_challenge_client/game_state.rb', line 148

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