Class: JustXiangqi::GameState
- Inherits:
-
Object
- Object
- JustXiangqi::GameState
- Defined in:
- lib/just_xiangqi/game_state.rb
Overview
Game State
Represents a game of Xiangqi in progress.
Instance Attribute Summary collapse
-
#current_player_number ⇒ Object
readonly
Returns the value of attribute current_player_number.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#squares ⇒ Object
readonly
Returns the value of attribute squares.
Class Method Summary collapse
-
.default ⇒ GameState
Instantiates a new GameState object in the starting position.
Instance Method Summary collapse
-
#as_json ⇒ Hash
serializes the game state as ahash.
-
#clone ⇒ GameState
deep clone of the game state.
- #in_check?(player_number) ⇒ Boolean
- #in_checkmate?(player_number) ⇒ Boolean
-
#initialize(current_player_number:, squares: []) ⇒ GameState
constructor
A new instance of GameState.
-
#move(player_number, from_id, to_id) ⇒ Boolean
Moves a piece owned by the player, from one square, to another.
-
#perform_complete_move(player_number, from_id, to_id) ⇒ Object
Moves a piece owned by the player, from one square, to another, with the option to promote.
-
#winner ⇒ Fixnum, NilClass
The player number of the winner.
Constructor Details
#initialize(current_player_number:, squares: []) ⇒ GameState
Returns a new instance of GameState.
17 18 19 20 21 22 23 24 |
# File 'lib/just_xiangqi/game_state.rb', line 17 def initialize(current_player_number: , squares: []) @current_player_number = current_player_number @squares = if squares.is_a?(SquareSet) squares else SquareSet.new(squares: squares) end end |
Instance Attribute Details
#current_player_number ⇒ Object (readonly)
Returns the value of attribute current_player_number.
26 27 28 |
# File 'lib/just_xiangqi/game_state.rb', line 26 def current_player_number @current_player_number end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
26 27 28 |
# File 'lib/just_xiangqi/game_state.rb', line 26 def errors @errors end |
#squares ⇒ Object (readonly)
Returns the value of attribute squares.
26 27 28 |
# File 'lib/just_xiangqi/game_state.rb', line 26 def squares @squares end |
Class Method Details
.default ⇒ GameState
Instantiates a new GameState object in the starting position.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/just_xiangqi/game_state.rb', line 31 def self.default new( current_player_number: 1, squares: [ { id: 'a10', x: 0, y: 0, piece: { id: 1, player_number: 2, type: 'ju' } }, { id: 'b10', x: 1, y: 0, piece: { id: 2, player_number: 2, type: 'ma' } }, { id: 'c10', x: 2, y: 0, piece: { id: 3, player_number: 2, type: 'xiang' } }, { id: 'd10', x: 3, y: 0, piece: { id: 4, player_number: 2, type: 'shi' } }, { id: 'e10', x: 4, y: 0, piece: { id: 5, player_number: 2, type: 'jiang' } }, { id: 'f10', x: 5, y: 0, piece: { id: 6, player_number: 2, type: 'shi' } }, { id: 'g10', x: 6, y: 0, piece: { id: 7, player_number: 2, type: 'xiang' } }, { id: 'h10', x: 7, y: 0, piece: { id: 8, player_number: 2, type: 'ma' } }, { id: 'i10', x: 8, y: 0, piece: { id: 9, player_number: 2, type: 'ju' } }, { id: 'a9', x: 0, y: 1, piece: nil }, { id: 'b9', x: 1, y: 1, piece: nil }, { id: 'c9', x: 2, y: 1, piece: nil }, { id: 'd9', x: 3, y: 1, piece: nil }, { id: 'e9', x: 4, y: 1, piece: nil }, { id: 'f9', x: 5, y: 1, piece: nil }, { id: 'g9', x: 6, y: 1, piece: nil }, { id: 'h9', x: 7, y: 1, piece: nil }, { id: 'i9', x: 8, y: 1, piece: nil }, { id: 'a8', x: 0, y: 2, piece: nil }, { id: 'b8', x: 1, y: 2, piece: { id: 10, player_number: 2, type: 'pao' } }, { id: 'c8', x: 2, y: 2, piece: nil }, { id: 'd8', x: 3, y: 2, piece: nil }, { id: 'e8', x: 4, y: 2, piece: nil }, { id: 'f8', x: 5, y: 2, piece: nil }, { id: 'g8', x: 6, y: 2, piece: nil }, { id: 'h8', x: 7, y: 2, piece: { id: 11, player_number: 2, type: 'pao' } }, { id: 'i8', x: 8, y: 2, piece: nil }, { id: 'a7', x: 0, y: 3, piece: { id: 12, player_number: 2, type: 'zu' } }, { id: 'b7', x: 1, y: 3, piece: nil }, { id: 'c7', x: 2, y: 3, piece: { id: 13, player_number: 2, type: 'zu' } }, { id: 'd7', x: 3, y: 3, piece: nil }, { id: 'e7', x: 4, y: 3, piece: { id: 14, player_number: 2, type: 'zu' } }, { id: 'f7', x: 5, y: 3, piece: nil }, { id: 'g7', x: 6, y: 3, piece: { id: 15, player_number: 2, type: 'zu' } }, { id: 'h7', x: 7, y: 3, piece: nil }, { id: 'i7', x: 8, y: 3, piece: { id: 16, player_number: 2, type: 'zu' } }, { id: 'a6', x: 0, y: 4, piece: nil }, { id: 'b6', x: 1, y: 4, piece: nil }, { id: 'c6', x: 2, y: 4, piece: nil }, { id: 'd6', x: 3, y: 4, piece: nil }, { id: 'e6', x: 4, y: 4, piece: nil }, { id: 'f6', x: 5, y: 4, piece: nil }, { id: 'g6', x: 6, y: 4, piece: nil }, { id: 'h6', x: 7, y: 4, piece: nil }, { id: 'i6', x: 8, y: 4, piece: nil }, { id: 'a5', x: 0, y: 5, piece: nil }, { id: 'b5', x: 1, y: 5, piece: nil }, { id: 'c5', x: 2, y: 5, piece: nil }, { id: 'd5', x: 3, y: 5, piece: nil }, { id: 'e5', x: 4, y: 5, piece: nil }, { id: 'f5', x: 5, y: 5, piece: nil }, { id: 'g5', x: 6, y: 5, piece: nil }, { id: 'h5', x: 7, y: 5, piece: nil }, { id: 'i5', x: 8, y: 5, piece: nil }, { id: 'a4', x: 0, y: 6, piece: { id: 17, player_number: 1, type: 'zu' } }, { id: 'b4', x: 1, y: 6, piece: nil }, { id: 'c4', x: 2, y: 6, piece: { id: 17, player_number: 1, type: 'zu' } }, { id: 'd4', x: 3, y: 6, piece: nil }, { id: 'e4', x: 4, y: 6, piece: { id: 17, player_number: 1, type: 'zu' } }, { id: 'f4', x: 5, y: 6, piece: nil }, { id: 'g4', x: 6, y: 6, piece: { id: 17, player_number: 1, type: 'zu' } }, { id: 'h4', x: 7, y: 6, piece: nil }, { id: 'i4', x: 8, y: 6, piece: { id: 17, player_number: 1, type: 'zu' } }, { id: 'a3', x: 0, y: 7, piece: nil }, { id: 'b3', x: 1, y: 7, piece: { id: 18, player_number: 1, type: 'pao' } }, { id: 'c3', x: 2, y: 7, piece: nil }, { id: 'd3', x: 3, y: 7, piece: nil }, { id: 'e3', x: 4, y: 7, piece: nil }, { id: 'f3', x: 5, y: 7, piece: nil }, { id: 'g3', x: 6, y: 7, piece: nil }, { id: 'h3', x: 7, y: 7, piece: { id: 19, player_number: 1, type: 'pao' } }, { id: 'i3', x: 8, y: 7, piece: nil }, { id: 'a2', x: 0, y: 8, piece: nil }, { id: 'b2', x: 1, y: 8, piece: nil }, { id: 'c2', x: 2, y: 8, piece: nil }, { id: 'd2', x: 3, y: 8, piece: nil }, { id: 'e2', x: 4, y: 8, piece: nil }, { id: 'f2', x: 5, y: 8, piece: nil }, { id: 'g2', x: 6, y: 8, piece: nil }, { id: 'h2', x: 7, y: 8, piece: nil }, { id: 'i2', x: 8, y: 8, piece: nil }, { id: 'a1', x: 0, y: 9, piece: { id: 20, player_number: 1, type: 'ju' } }, { id: 'b1', x: 1, y: 9, piece: { id: 21, player_number: 1, type: 'ma' } }, { id: 'c1', x: 2, y: 9, piece: { id: 22, player_number: 1, type: 'xiang' } }, { id: 'd1', x: 3, y: 9, piece: { id: 23, player_number: 1, type: 'shi' } }, { id: 'e1', x: 4, y: 9, piece: { id: 24, player_number: 1, type: 'jiang' } }, { id: 'f1', x: 5, y: 9, piece: { id: 25, player_number: 1, type: 'shi' } }, { id: 'g1', x: 6, y: 9, piece: { id: 26, player_number: 1, type: 'xiang' } }, { id: 'h1', x: 7, y: 9, piece: { id: 27, player_number: 1, type: 'ma' } }, { id: 'i1', x: 8, y: 9, piece: { id: 28, player_number: 1, type: 'ju' } } ] ) end |
Instance Method Details
#as_json ⇒ Hash
serializes the game state as ahash
141 142 143 144 145 146 |
# File 'lib/just_xiangqi/game_state.rb', line 141 def as_json { current_player_number: current_player_number, squares: squares.as_json, } end |
#clone ⇒ GameState
deep clone of the game state
151 152 153 |
# File 'lib/just_xiangqi/game_state.rb', line 151 def clone self.class.new(**as_json) end |
#in_check?(player_number) ⇒ Boolean
235 236 237 238 239 |
# File 'lib/just_xiangqi/game_state.rb', line 235 def in_check?(player_number) jiang_square = squares.find_jiang_for_player(player_number) threatened_by = squares.threatened_by(opposing_player_number(player_number), self) threatened_by.include?(jiang_square) end |
#in_checkmate?(player_number) ⇒ Boolean
241 242 243 |
# File 'lib/just_xiangqi/game_state.rb', line 241 def in_checkmate?(player_number) (in_check?(player_number) || non_jiang_pieces_cannot_move?(player_number)) && jiang_cannot_move?(player_number) end |
#move(player_number, from_id, to_id) ⇒ Boolean
Moves a piece owned by the player, from one square, to another.
It has an option to promote the moving piece. It moves the piece and returns true if the move is valid and it’s the player’s turn. It returns false otherwise.
Example:
# Moves a piece from a square to perform a move
game_state.move(1, '77', '78')
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/just_xiangqi/game_state.rb', line 175 def move(player_number, from_id, to_id) @errors = [] from = squares.find_by_id(from_id) to = squares.find_by_id(to_id) if current_player_number != player_number @errors.push JustXiangqi::NotPlayersTurnError.new elsif from.unoccupied? @errors.push JustXiangqi::NoPieceError.new elsif to.nil? @errors.push JustXiangqi::OffBoardError.new elsif from.piece.can_move?(from, to, self) duplicate = self.clone duplicate.perform_complete_move(player_number, from_id, to_id) if duplicate.in_check?(current_player_number) @errors.push JustXiangqi::MovedIntoCheckError.new else perform_complete_move(player_number, from_id, to_id) end else @errors.push JustXiangqi::InvalidMoveError.new end @errors.empty? end |
#perform_complete_move(player_number, from_id, to_id) ⇒ Object
Moves a piece owned by the player, from one square, to another, with the option to promote.
It moves the piece and returns true if the move is valid and it’s the player’s turn. It returns false otherwise.
222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/just_xiangqi/game_state.rb', line 222 def perform_complete_move(player_number, from_id, to_id) from = squares.find_by_id(from_id) to = squares.find_by_id(to_id) captured = to.occupied? ? to : nil @last_change = { type: 'move', data: { player_number: player_number, from: from_id, to: to_id } } perform_move(player_number, from, to, captured) pass_turn end |
#winner ⇒ Fixnum, NilClass
The player number of the winner. It returns nil if there is no winner.
207 208 209 210 211 212 213 214 215 216 |
# File 'lib/just_xiangqi/game_state.rb', line 207 def winner case when in_checkmate?(1) 2 when in_checkmate?(2) 1 else nil end end |