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
83
# 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

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



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

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



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

def current_phase
  @current_phase
end

#current_player_numberFixnum (readonly)

Returns who’s turn it is.

Returns:

  • (Fixnum)

    who’s turn it is



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

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



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

def dice
  @dice
end

#errorsArray<Error> (readonly)

Returns errors if any.

Returns:

  • (Array<Error>)

    errors if any.



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

def errors
  @errors
end

#last_changeHash (readonly)

Returns the last change made.

Returns:

  • (Hash)

    the last change made.



152
153
154
# File 'lib/just_backgammon/game_state.rb', line 152

def last_change
  @last_change
end

#off_boardFixnum (readonly)

Returns who’s turn it is.

Returns:

  • (Fixnum)

    who’s turn it is.



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

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



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

def points
  @points
end

Class Method Details

.defaultGameState

Instantiates a new GameState object in the starting position

Returns:



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
# 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, owner: 1}, {id: 2, owner: 1}] },
      { number: 2, pieces: [] },
      { number: 3, pieces: [] },
      { number: 4, pieces: [] },
      { number: 5, pieces: [] },
      { number: 6, pieces: [{id: 3, owner: 2}, {id: 4, owner: 2}, {id: 5, owner: 2}, {id: 6, owner: 2}, {id: 7, owner: 2}] },

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

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

      { number: 19, pieces: [{id: 24, owner: 1}, {id: 25, owner: 1}, {id: 26, owner: 1}, {id: 27, owner: 1}, {id: 28, owner: 1}] },
      { number: 20, pieces: [] },
      { number: 21, pieces: [] },
      { number: 22, pieces: [] },
      { number: 23, pieces: [] },
      { number: 24, pieces: [{id: 29, owner: 2}, {id: 30, owner: 2}] },
    ],
    off_board: { pieces: [] }
  })
end

Instance Method Details

#as_jsonHash

A hashed serialized representation of the game state

Returns:

  • (Hash)


228
229
230
231
232
233
234
235
236
237
# 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) ⇒ 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)


201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# 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) ⇒ 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)


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# 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

#winnerFixnum, NilClass

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

Returns:

  • (Fixnum, NilClass)


221
222
223
# 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