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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGameState



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

def initialize
  @current_player_color = PlayerColor::RED
  @start_player_color = PlayerColor::RED
  @board = Board.new
  @turn = 0
  @undeployed_red_pieces = GameState.parse_pieces_string(Constants::STARTING_PIECES, PlayerColor::RED)
  @undeployed_blue_pieces = GameState.parse_pieces_string(Constants::STARTING_PIECES, PlayerColor::BLUE)
end

Instance Attribute Details

#bluePlayer (readonly)



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

def blue
  @blue
end

#boardBoard



43
44
45
# File 'lib/software_challenge_client/game_state.rb', line 43

def board
  @board
end

#conditionCondition



51
52
53
# File 'lib/software_challenge_client/game_state.rb', line 51

def condition
  @condition
end

#current_player_colorPlayerColor



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

def current_player_color
  @current_player_color
end

#last_moveMove



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

def last_move
  @last_move
end

#redPlayer (readonly)



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

def red
  @red
end

#start_player_colorPlayerColor



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

def start_player_color
  @start_player_color
end

#turnInteger



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

def turn
  @turn
end

#undeployed_blue_piecesPlayer



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

def undeployed_blue_pieces
  @undeployed_blue_pieces
end

#undeployed_red_piecesPlayer



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

def undeployed_red_pieces
  @undeployed_red_pieces
end

Class Method Details

.parse_pieces_string(string, color) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/software_challenge_client/game_state.rb', line 57

def self.parse_pieces_string(string, color)
  string.chars.map do |c|
    case c
    when 'Q'
      Piece.new(color, PieceType::BEE)
    when 'S'
      Piece.new(color, PieceType::SPIDER)
    when 'G'
      Piece.new(color, PieceType::GRASSHOPPER)
    when 'B'
      Piece.new(color, PieceType::BEETLE)
    when 'A'
      Piece.new(color, PieceType::ANT)
    end
  end
end

Instance Method Details

#==(other) ⇒ Object



166
167
168
169
170
171
172
173
174
175
# File 'lib/software_challenge_client/game_state.rb', line 166

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.



86
87
88
89
90
91
92
# File 'lib/software_challenge_client/game_state.rb', line 86

def add_player(player)
  if player.color == PlayerColor::RED
    @red = player
  elsif player.color == PlayerColor::BLUE
    @blue = player
  end
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.



180
181
182
# File 'lib/software_challenge_client/game_state.rb', line 180

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

#current_playerPlayer



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

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

#deployed_pieces(color) ⇒ Object



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

def deployed_pieces(color)
  board.deployed_pieces(color)
end

#field(x, y) ⇒ Object

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



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

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

#game_ended?Boolean



139
140
141
# File 'lib/software_challenge_client/game_state.rb', line 139

def game_ended?
  !condition.nil?
end

#other_playerPlayer



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

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

#other_player_colorPlayerColor



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

def other_player_color
  PlayerColor.opponent_color(current_player_color)
end

#own_fieldsArray<Field>



190
191
192
# File 'lib/software_challenge_client/game_state.rb', line 190

def own_fields
  board.fields_of_color(current_player_color)
end

#perform!(move) ⇒ Object

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



133
134
135
# File 'lib/software_challenge_client/game_state.rb', line 133

def perform!(move)
  GameRuleLogic.perform_move(self, move)
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.



161
162
163
164
# File 'lib/software_challenge_client/game_state.rb', line 161

def points_for_player(player)
  # TODO
  -1
end

#roundInteger



112
113
114
# File 'lib/software_challenge_client/game_state.rb', line 112

def round
  turn / 2
end

#switch_current_playerObject

Wechselt den Spieler, der aktuell an der Reihe ist.



185
186
187
# File 'lib/software_challenge_client/game_state.rb', line 185

def switch_current_player
  @current_player_color = other_player_color
end

#undeployed_pieces(color) ⇒ Object



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

def undeployed_pieces(color)
  case color
  when PlayerColor::RED
    undeployed_red_pieces
  when PlayerColor::BLUE
    undeployed_blue_pieces
  end
end

#winnerPlayer



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

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

#winning_reasonString



151
152
153
# File 'lib/software_challenge_client/game_state.rb', line 151

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