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.
-
#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 |
# 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) = JustBackgammon::Bar.load() @points = JustBackgammon::PointSet.load(points: points) @off_board = JustBackgammon::OffBoard.load(off_board) @errors = [] end |
Instance Attribute Details
#bar ⇒ Bar (readonly)
Returns the bar where hit pieces go. Contains an array of pieces.
139 140 141 |
# File 'lib/just_backgammon/game_state.rb', line 139 def end |
#current_phase ⇒ String (readonly)
Returns 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_number ⇒ Fixnum (readonly)
Returns 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 |
#dice ⇒ DiceSet (readonly)
Returns an array of dice, each with a number.
136 137 138 |
# File 'lib/just_backgammon/game_state.rb', line 136 def dice @dice end |
#errors ⇒ Array<Error> (readonly)
Returns errors if any.
148 149 150 |
# File 'lib/just_backgammon/game_state.rb', line 148 def errors @errors end |
#off_board ⇒ Fixnum (readonly)
Returns who’s turn it is.
145 146 147 |
# File 'lib/just_backgammon/game_state.rb', line 145 def off_board @off_board end |
#points ⇒ PointSet (readonly)
Returns 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
.default ⇒ GameState
Instantiates a new GameState object in the starting position
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_json ⇒ Hash
A hashed serialized representation of the game state
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: .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}])
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)
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 |
#winner ⇒ Fixnum, NilClass
The player number of the winner. It returns nil if there is no winner.
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 |