class JustCheckers::GameState
Game State¶ ↑
Represents a game of Checkers in progress.
Attributes
@return [Fixnum] who's turn it is.
@return [Array<Error>] errors if any.
@return [Hash] most recent change.
@return [Array<Square>] the board state.
Public Class Methods
Instantiates a new GameState
object in the starting position
@return [GameState]
# File lib/just_checkers/game_state.rb, line 52 def self.default new( current_player_number: 1, squares: [ { id: 1, x: 1, y: 0, piece: { id: 1, player_number: 1, king: false }}, { id: 2, x: 3, y: 0, piece: { id: 2, player_number: 1, king: false }}, { id: 3, x: 5, y: 0, piece: { id: 3, player_number: 1, king: false }}, { id: 4, x: 7, y: 0, piece: { id: 4, player_number: 1, king: false }}, { id: 5, x: 0, y: 1, piece: { id: 5, player_number: 1, king: false }}, { id: 6, x: 2, y: 1, piece: { id: 6, player_number: 1, king: false }}, { id: 7, x: 4, y: 1, piece: { id: 7, player_number: 1, king: false }}, { id: 8, x: 6, y: 1, piece: { id: 8, player_number: 1, king: false }}, { id: 9, x: 1, y: 2, piece: { id: 9, player_number: 1, king: false }}, { id: 10, x: 3, y: 2, piece: { id: 10, player_number: 1, king: false }}, { id: 11, x: 5, y: 2, piece: { id: 11, player_number: 1, king: false }}, { id: 12, x: 7, y: 2, piece: { id: 12, player_number: 1, king: false }}, { id: 13, x: 0, y: 3, piece: nil }, { id: 14, x: 2, y: 3, piece: nil }, { id: 15, x: 4, y: 3, piece: nil }, { id: 16, x: 6, y: 3, piece: nil }, { id: 17, x: 1, y: 4, piece: nil }, { id: 18, x: 3, y: 4, piece: nil }, { id: 19, x: 5, y: 4, piece: nil }, { id: 20, x: 7, y: 4, piece: nil }, { id: 21, x: 0, y: 5, piece: { id: 13, player_number: 2, king: false }}, { id: 22, x: 2, y: 5, piece: { id: 14, player_number: 2, king: false }}, { id: 23, x: 4, y: 5, piece: { id: 15, player_number: 2, king: false }}, { id: 24, x: 6, y: 5, piece: { id: 16, player_number: 2, king: false }}, { id: 25, x: 1, y: 6, piece: { id: 17, player_number: 2, king: false }}, { id: 26, x: 3, y: 6, piece: { id: 18, player_number: 2, king: false }}, { id: 27, x: 5, y: 6, piece: { id: 19, player_number: 2, king: false }}, { id: 28, x: 7, y: 6, piece: { id: 20, player_number: 2, king: false }}, { id: 29, x: 0, y: 7, piece: { id: 21, player_number: 2, king: false }}, { id: 30, x: 2, y: 7, piece: { id: 22, player_number: 2, king: false }}, { id: 31, x: 4, y: 7, piece: { id: 23, player_number: 2, king: false }}, { id: 32, x: 6, y: 7, piece: { id: 24, player_number: 2, king: false }} ] ) end
New objects can be instantiated.
@param [Fixnum] current_player_number
Who's turn it is, 1 or 2
@param [Array<Square>] squares
An array of squares, each with x and y co-ordinates and a piece.
Example:¶ ↑
# Instantiates a new Game of Checkers JustCheckers::GameState.new({ current_player_number: 1, squares: [ { x: 1, y: 0, piece: { player_number: 1, king: false }} ] })
# File lib/just_checkers/game_state.rb, line 30 def initialize(current_player_number: , squares: []) @current_player_number = current_player_number @squares = SquareSet.new(squares: squares) @errors = [] @last_change = {} end
Public Instance Methods
A hashed serialized representation of the game state
@return [Hash]
# File lib/just_checkers/game_state.rb, line 102 def as_json { current_player_number: current_player_number, squares: squares.as_json } end
Moves a piece owned by the player, from one square, to another
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 double jump game_state.move(1, {x: 0, y: 1}, [{x: 1, y: 2}, {x: 3, y: 4}])
@param [Fixnum] player_number
the player number, 1 or 2
@param [Hash, Fixnum] from
where the moving piece currently is.
@param [Array<Hash>, Array<Fixnum>] to
each place the piece is going to move to.
@return [Boolean]
# File lib/just_checkers/game_state.rb, line 141 def move(player_number, from, to) @errors = [] from_square = if from.is_a?(Hash) squares.find_by_x_and_y(from[:x].to_i, from[:y].to_i) else squares.find_by_id(from.to_i) end to_squares = if to.all? { |s| s.is_a?(Hash) } to.map { |position| squares.find_by_x_and_y(position[:x].to_i, position[:y].to_i) } else to.map { |id| squares.find_by_id(id.to_i) } end if player_number != current_player_number @errors.push NotPlayersTurnError.new elsif move_valid?(from_square, to_squares) @last_change = { type: 'move', data: {player_number: player_number, from: from, to: to} } perform_move(from_square, to_squares) to_squares.last.promote if to_squares.last.promotable? turn end @errors.empty? end
The player number of the winner. It returns nil if there is no winner.
@return [Fixnum,NilClass]
# File lib/just_checkers/game_state.rb, line 112 def winner if no_pieces_for_player?(1) 2 elsif no_pieces_for_player?(2) 1 else nil end end