class JustBackgammon::GameState

Game State

Represents a game of Backgammon in progress.

Constants

MOVE

The move phase of the turn where the players move pieces.

ROLL

The roll phase of the turn where the players roll dice.

Attributes

bar[R]

@return [Bar] the bar where hit pieces go. Contains an array of pieces

current_phase[R]

@return [String] the current phase of the turn. Either move or roll

current_player_number[R]

@return [Fixnum] who's turn it is

dice[R]

@return [DiceSet] an array of dice, each with a number

errors[R]

@return [Array<Error>] errors if any.

last_change[R]

@return [Hash] the last change made.

off_board[R]

@return [Fixnum] who's turn it is.

points[R]

@return [PointSet] an array of points, each with a number and an array of pieces

Public Class Methods

default() click to toggle source

Instantiates a new GameState object in the starting position

@return [GameState]

# File lib/just_backgammon/game_state.rb, line 88
def self.default
  new(
    current_player_number: 1,
    current_phase: ROLL,
    dice: [
      { id: 1, number: nil },
      { id: 2, number: nil }
    ],
    bar: { pieces: [] },
    points: [
      { number: 1, pieces: [{id: 1, player_number: 1}, {id: 2, player_number: 1}] },
      { number: 2, pieces: [] },
      { number: 3, pieces: [] },
      { number: 4, pieces: [] },
      { number: 5, pieces: [] },
      { number: 6, pieces: [{id: 3, player_number: 2}, {id: 4, player_number: 2}, {id: 5, player_number: 2}, {id: 6, player_number: 2}, {id: 7, player_number: 2}] },

      { number: 7, pieces: [] },
      { number: 8, pieces: [{id: 8, player_number: 2}, {id: 9, player_number: 2}, {id: 10, player_number: 2}] },
      { number: 9, pieces: [] },
      { number: 10, pieces: [] },
      { number: 11, pieces: [] },
      { number: 12, pieces: [{id: 11, player_number: 1}, {id: 12, player_number: 1}, {id: 13, player_number: 1}, {id: 14, player_number: 1}, {id: 15, player_number: 1}] },

      { number: 13, pieces: [{id: 16, player_number: 2}, {id: 17, player_number: 2}, {id: 18, player_number: 2}, {id: 19, player_number: 2}, {id: 20, player_number: 2}] },
      { number: 14, pieces: [] },
      { number: 15, pieces: [] },
      { number: 16, pieces: [] },
      { number: 17, pieces: [{id: 21, player_number: 1}, {id: 22, player_number: 1}, {id: 23, player_number: 1}] },
      { number: 18, pieces: [] },

      { number: 19, pieces: [{id: 24, player_number: 1}, {id: 25, player_number: 1}, {id: 26, player_number: 1}, {id: 27, player_number: 1}, {id: 28, player_number: 1}] },
      { number: 20, pieces: [] },
      { number: 21, pieces: [] },
      { number: 22, pieces: [] },
      { number: 23, pieces: [] },
      { number: 24, pieces: [{id: 29, player_number: 2}, {id: 30, player_number: 2}] },
    ],
    off_board: { pieces: [] }
  )
end
new(current_player_number:, current_phase:, dice:, bar:, points:, off_board:) click to toggle source

A new instance of GameState.

@param [Fixnum] current_player_number

Who's turn it is, 1 or 2

@param [Fixnum] current_phase

The current phase of the turn. Either move or roll.

@param [Array<Hash>] dice

An array of dice, each with a number.

@param [Hash] bar

The bar where hit pieces go. Contains an array of pieces.

@param [Array<Hash>] points

An array of points, each with a number and an array of pieces.

@param [Hash] off_board

Off the board where pieces bear off. Contains an array of pieces.

Example:

# Instantiates a new Game of Backgammon
JustBackgammon::GameState.new({
  current_player_number: 1,
  current_phase: 'move',
  dice: [
    { number: 1 },
    { number: 4 },
  ],
  bar: {
    pieces: [],
  },
  points: [
    { number: 1, pieces: [{player_number: 1}, {player_number: 1}] },
    { number: 2, pieces: [] }
  ],
  off_board: {
    pieces: [],
  }
})
# File lib/just_backgammon/game_state.rb, line 74
def initialize(current_player_number:, current_phase:, dice:, bar:, points:, off_board:)
  @current_player_number = current_player_number
  @current_phase = current_phase
  @dice = JustBackgammon::DiceSet.load(dice: dice)
  @bar = JustBackgammon::Bar.load(bar)
  @points = JustBackgammon::PointSet.load(points: points)
  @off_board = JustBackgammon::OffBoard.load(off_board)
  @errors = []
  @last_change = {}
end

Public Instance Methods

as_json() click to toggle source

A hashed serialized representation of the game state

@return [Hash]

# File lib/just_backgammon/game_state.rb, line 228
def as_json
  {
    current_player_number: current_player_number,
    current_phase: current_phase,
    dice: dice.as_json,
    bar: bar.as_json,
    points: points.as_json,
    off_board: off_board.as_json
  }
end
move(player_number, list) click to toggle source

Moves each piece specified from one point, to another

It moves each piece specified and returns true on success. It returns false if the move is invalid, it's not the player's turn or the phase is roll..

Example:

# Moves two pieces for player 1
game_state.move(1, [{from: 1, to: 2}, {from: 3, to: 4}])

@param [Fixnum] player_number

the player number, 1 or 2

@param [Array<Hash>] list

a list of all the moves, each containing a from and to key.

@return [Boolean]

# File lib/just_backgammon/game_state.rb, line 201
def move(player_number, list)
  move_list = sanitize_list(list)

  @errors = []
  if player_number != current_player_number
    @errors.push JustBackgammon::NotPlayersTurnError.new
  elsif current_phase != MOVE
    @errors.push JustBackgammon::WrongPhaseError.new
  elsif move_valid?(move_list)
    perform_move(move_list)
    step
    @last_change = { type: 'move', data: { player_number: player_number, list: list }}
  end

  @errors.none?
end
roll(player_number) click to toggle source

Rolls the dice

Two dice are rolled and returns true on success. The results can be found on the dice attribute. If the dice have the same result, then the dice are duplicated. If it's not the player's turn or the phase is move, it will return false.

Example:

# Rolls the dice for player 1
game_state.roll(1)

@param [Fixnum] player_number

the player number, 1 or 2

@return [Boolean]

# File lib/just_backgammon/game_state.rb, line 169
def roll(player_number)
  @errors = []

  if player_number != current_player_number
    @errors.push JustBackgammon::NotPlayersTurnError.new
  elsif current_phase != ROLL
    @errors.push JustBackgammon::WrongPhaseError.new
  else
    @dice.roll
    step
    @last_change = { type: 'roll', data: { player_number: player_number, dice: @dice.as_json } }
  end

  @errors.none?
end
winner() click to toggle source

The player number of the winner. It returns nil if there is no winner.

@return [Fixnum,NilClass]

# File lib/just_backgammon/game_state.rb, line 221
def winner
  [1, 2].find { |player_number| @off_board.number_of_pieces_owned_by_player(player_number) == 15 }
end