Class: GameRuleLogic
- Inherits:
-
Object
- Object
- GameRuleLogic
- Includes:
- Constants
- Defined in:
- lib/software_challenge_client/game_rule_logic.rb
Overview
Methoden, welche die Spielregeln von Ostseeschach abbilden.
Es gibt hier viele Helfermethoden, die von den beiden Hauptmethoden GameRuleLogic.valid_move? und GameRuleLogic.possible_moves benutzt werden.
Constant Summary collapse
- SUM_MAX_SQUARES =
89
Constants included from Constants
Constants::BOARD_SIZE, Constants::GAME_IDENTIFIER, Constants::ROUND_LIMIT
Class Method Summary collapse
-
.moves_for_piece(gamestate, piece) ⇒ Array<Move>
Hilfsmethode um Legezüge für einen [Piece] zu berechnen.
-
.obstructed?(board, position) ⇒ Boolean
Überprüft, ob die gegebene [position] mit einem Spielstein belegt ist.
-
.perform_move(gamestate, move) ⇒ GameState
Führe den gegebenen [Move] im gebenenen [GameState] aus.
-
.possible_move(gamestate) ⇒ Move
Gibt einen zufälligen möglichen Zug zurück.
-
.possible_moves(gamestate) ⇒ Array<Move>
Gibt alle möglichen Züge für den Spieler zurück, der in der gamestate dran ist.
-
.valid_move?(gamestate, move) ⇒ Boolean
Prüft, ob der gegebene [Move] zulässig ist.
-
.winning_condition(gamestate) ⇒ Condition
Prueft, ob ein Spieler im gegebenen GameState gewonnen hat.
Class Method Details
.moves_for_piece(gamestate, piece) ⇒ Array<Move>
Hilfsmethode um Legezüge für einen [Piece] zu berechnen.
49 50 51 52 53 54 55 |
# File 'lib/software_challenge_client/game_rule_logic.rb', line 49 def self.moves_for_piece(gamestate, piece) moves = Set[] piece.target_coords.each do |c| moves << Move.new(piece.position, c) end moves.select { |m| valid_move?(gamestate, m) }.to_a end |
.obstructed?(board, position) ⇒ Boolean
Überprüft, ob die gegebene [position] mit einem Spielstein belegt ist.
83 84 85 |
# File 'lib/software_challenge_client/game_rule_logic.rb', line 83 def self.obstructed?(board, position) !board.field_at(position).empty? end |
.perform_move(gamestate, move) ⇒ GameState
Führe den gegebenen [Move] im gebenenen [GameState] aus.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/software_challenge_client/game_rule_logic.rb', line 94 def self.perform_move(gamestate, move) raise 'Invalid move!' unless valid_move?(gamestate, move) from_field = gamestate.board.field_at(move.from) to_field = gamestate.board.field_at(move.to) # Update board pieces if one is stepped on if not to_field.empty? from_field.piece.height = from_field.piece.height + 1 # Check for high tower if from_field.piece.height >= 3 gamestate.current_player.amber = gamestate.current_player.amber + 1 to_field.piece = nil end end # Update board fields to_field.piece = from_field.piece from_field.piece = nil # Update position value of the moved piece if !to_field.empty? && !to_field.piece.nil? to_field.piece.position = Coordinates.new(to_field.coordinates.x, to_field.coordinates.y) end gamestate.turn += 1 gamestate.last_move = move end |
.possible_move(gamestate) ⇒ Move
Gibt einen zufälligen möglichen Zug zurück
40 41 42 |
# File 'lib/software_challenge_client/game_rule_logic.rb', line 40 def self.possible_move(gamestate) possible_moves(gamestate).sample end |
.possible_moves(gamestate) ⇒ Array<Move>
Gibt alle möglichen Züge für den Spieler zurück, der in der gamestate dran ist. Diese ist die wichtigste Methode dieser Klasse für Schüler.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/software_challenge_client/game_rule_logic.rb', line 25 def self.possible_moves(gamestate) moves = [] fields = gamestate.board.fields_of_color(gamestate.current_player.color) fields.each do |f| moves.push(*moves_for_piece(gamestate, f.piece)) end moves.select { |m| valid_move?(gamestate, m) }.to_a end |
.valid_move?(gamestate, move) ⇒ Boolean
Prüft, ob der gegebene [Move] zulässig ist.
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/software_challenge_client/game_rule_logic.rb', line 64 def self.valid_move?(gamestate, move) return false unless gamestate.current_player.color == move.piece(gamestate).color return false unless gamestate.board.in_bounds?(move.to) return false if gamestate.board.field_at(move.to).color == move.piece(gamestate).color return false unless move.piece(gamestate).target_coords.include? move.to # TODO 2022: Forgot checks? true end |
.winning_condition(gamestate) ⇒ Condition
Prueft, ob ein Spieler im gegebenen GameState gewonnen hat.
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/software_challenge_client/game_rule_logic.rb', line 130 def self.winning_condition(gamestate) if gamestate.player_one.amber >= 2 Condition.new(gamestate.player_one, "Spieler 1 hat 2 Bernsteine erreicht") end if gamestate.player_two.amber >= 2 Condition.new(gamestate.player_two, "Spieler 2 hat 2 Bernsteine erreicht") end nil end |