Class: JustBackgammon::GameState

Inherits:
Object
  • Object
show all
Defined in:
lib/just_backgammon/game_state.rb

Overview

Game State

Represents a game of Backgammon in progress.

Constant Summary collapse

ROLL =

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

'roll'
MOVE =

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

'move'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_player_number:, current_phase:, dice:, bar:, points:, off_board:) ⇒ GameState

A new instance of GameState.

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: [{owner: 1}, {owner: 1}] },
    { number: 2, pieces: [] }
  ],
  off_board: {
    pieces: [],
  }
})

Parameters:

  • current_player_number (Fixnum)

    Who’s turn it is, 1 or 2

  • current_phase (Fixnum)

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

  • dice (Array<Hash>)

    An array of dice, each with a number.

  • bar (Hash)

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

  • points (Array<Hash>)

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

  • off_board (Hash)

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



74
75
76
77
78
79
80
81
82
# 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 = []
end

Instance Attribute Details

#barBar (readonly)

Returns the bar where hit pieces go. Contains an array of pieces.

Returns:

  • (Bar)

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



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

def bar
  @bar
end

#current_phaseString (readonly)

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

Returns:

  • (String)

    the current phase of the turn. Either move or roll



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

def current_phase
  @current_phase
end

#current_player_numberFixnum (readonly)

Returns who’s turn it is.

Returns:

  • (Fixnum)

    who’s turn it is



130
131
132
# File 'lib/just_backgammon/game_state.rb', line 130

def current_player_number
  @current_player_number
end

#diceDiceSet (readonly)

Returns an array of dice, each with a number.

Returns:

  • (DiceSet)

    an array of dice, each with a number



136
137
138
# File 'lib/just_backgammon/game_state.rb', line 136

def dice
  @dice
end

#errorsArray<Error> (readonly)

Returns errors if any.

Returns:

  • (Array<Error>)

    errors if any.



148
149
150
# File 'lib/just_backgammon/game_state.rb', line 148

def errors
  @errors
end

#off_boardFixnum (readonly)

Returns who’s turn it is.

Returns:

  • (Fixnum)

    who’s turn it is.



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

def off_board
  @off_board
end

#pointsPointSet (readonly)

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

Returns:

  • (PointSet)

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



142
143
144
# File 'lib/just_backgammon/game_state.rb', line 142

def points
  @points
end

Class Method Details

.defaultGameState

Instantiates a new GameState object in the starting position

Returns:



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
# File 'lib/just_backgammon/game_state.rb', line 87

def self.default
  new({
    current_player_number: 1,
    current_phase: ROLL,
    dice: [
      { number: nil },
      { number: nil }
    ],
    bar: { pieces: [] },
    points: [
      { number: 1, pieces: [{owner: 1}, {owner: 1}] },
      { number: 2, pieces: [] },
      { number: 3, pieces: [] },
      { number: 4, pieces: [] },
      { number: 5, pieces: [] },
      { number: 6, pieces: [{owner: 2}, {owner: 2}, {owner: 2}, {owner: 2}, {owner: 2}] },

      { number: 7, pieces: [] },
      { number: 8, pieces: [{owner: 2}, {owner: 2}, {owner: 2}] },
      { number: 9, pieces: [] },
      { number: 10, pieces: [] },
      { number: 11, pieces: [] },
      { number: 12, pieces: [{owner: 1}, {owner: 1}, {owner: 1}, {owner: 1}, {owner: 1}] },

      { number: 13, pieces: [{owner: 2}, {owner: 2}, {owner: 2}, {owner: 2}, {owner: 2}] },
      { number: 14, pieces: [] },
      { number: 15, pieces: [] },
      { number: 16, pieces: [] },
      { number: 17, pieces: [{owner: 1}, {owner: 1}, {owner: 1}] },
      { number: 18, pieces: [] },

      { number: 19, pieces: [{owner: 1}, {owner: 1}, {owner: 1}, {owner: 1}, {owner: 1}] },
      { number: 20, pieces: [] },
      { number: 21, pieces: [] },
      { number: 22, pieces: [] },
      { number: 23, pieces: [] },
      { number: 24, pieces: [{owner: 2}, {owner: 2}] },
    ],
    off_board: { pieces: [] }
  })
end

Instance Method Details

#as_jsonHash

A hashed serialized representation of the game state

Returns:

  • (Hash)


222
223
224
225
226
227
228
229
230
231
# File 'lib/just_backgammon/game_state.rb', line 222

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) ⇒ Boolean

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}])

Parameters:

  • player_number (Fixnum)

    the player number, 1 or 2.

  • list (Array<Hash>)

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

Returns:

  • (Boolean)


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/just_backgammon/game_state.rb', line 196

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
  end

  @errors.none?
end

#roll(player_number) ⇒ Boolean

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)

Parameters:

  • player_number (Fixnum)

    the player number, 1 or 2.

Returns:

  • (Boolean)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/just_backgammon/game_state.rb', line 165

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
  end

  @errors.none?
end

#winnerFixnum, NilClass

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

Returns:

  • (Fixnum, NilClass)


215
216
217
# File 'lib/just_backgammon/game_state.rb', line 215

def winner
  [1, 2].find { |player_number| @off_board.number_of_pieces_owned_by_player(player_number) == 15 }
end