Class: JustBackgammon::GameState
- Inherits:
-
Object
- Object
- JustBackgammon::GameState
- 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
-
#bar ⇒ Bar
readonly
The bar where hit pieces go.
-
#current_phase ⇒ String
readonly
The current phase of the turn.
-
#current_player_number ⇒ Fixnum
readonly
Who’s turn it is.
-
#dice ⇒ DiceSet
readonly
An array of dice, each with a number.
-
#errors ⇒ Array<Error>
readonly
Errors if any.
-
#last_change ⇒ Hash
readonly
The last change made.
-
#off_board ⇒ Fixnum
readonly
Who’s turn it is.
-
#points ⇒ PointSet
readonly
An array of points, each with a number and an array of pieces.
Class Method Summary collapse
-
.default ⇒ GameState
Instantiates a new GameState object in the starting position.
Instance Method Summary collapse
-
#as_json ⇒ Hash
A hashed serialized representation of the game state.
-
#initialize(current_player_number:, current_phase:, dice:, bar:, points:, off_board:) ⇒ GameState
constructor
A new instance of GameState.
-
#move(player_number, list) ⇒ Boolean
Moves each piece specified from one point, to another.
-
#roll(player_number) ⇒ Boolean
Rolls the dice.
-
#winner ⇒ Fixnum, NilClass
The player number of the winner.
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: [],
}
})
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() @points = JustBackgammon::PointSet.load(points: points) @off_board = JustBackgammon::OffBoard.load(off_board) @errors = [] @last_change = {} end |
Instance Attribute Details
#bar ⇒ Bar (readonly)
Returns 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 end |
#current_phase ⇒ String (readonly)
Returns 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_number ⇒ Fixnum (readonly)
Returns 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 |
#dice ⇒ DiceSet (readonly)
Returns an array of dice, each with a number.
137 138 139 |
# File 'lib/just_backgammon/game_state.rb', line 137 def dice @dice end |
#errors ⇒ Array<Error> (readonly)
Returns errors if any.
149 150 151 |
# File 'lib/just_backgammon/game_state.rb', line 149 def errors @errors end |
#last_change ⇒ Hash (readonly)
Returns the last change made.
152 153 154 |
# File 'lib/just_backgammon/game_state.rb', line 152 def last_change @last_change end |
#off_board ⇒ Fixnum (readonly)
Returns who’s turn it is.
146 147 148 |
# File 'lib/just_backgammon/game_state.rb', line 146 def off_board @off_board end |
#points ⇒ PointSet (readonly)
Returns 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
.default ⇒ GameState
Instantiates a new GameState object in the starting position
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_json ⇒ Hash
A hashed serialized representation of the game state
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: .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}])
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)
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 |
#winner ⇒ Fixnum, NilClass
The player number of the winner. It returns nil if there is no winner.
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 |