just_backgammon 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/just_backgammon/combined_move.rb +10 -5
- data/lib/just_backgammon/game_state.rb +10 -23
- data/lib/just_backgammon/move_list.rb +55 -24
- data/lib/just_backgammon/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c1ebb2844a2a6fef92a1ce1644cd4a16b984dbc0
|
4
|
+
data.tar.gz: 29d1a815550ca78b52da80cf48c3b7ab135e92f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3d06f071fb46878e406ddfe2bb16707cba385282cfe730d99a8e935f21647bb86212fe14898d247bbf86cab26a6b1f203c5853f725931275db535b492d428721
|
7
|
+
data.tar.gz: e518b7e4825bb4e88656b301de9740bfa6137701ed3ff675b93d9add0cbf618072ee452e76e3a1ed45a7caefc68ec523a073a73624c4728b64ce63f7d414655d
|
@@ -25,29 +25,34 @@ module JustBackgammon
|
|
25
25
|
# @return [Array<Move>] the legs of the combined move.
|
26
26
|
attr_reader :legs
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
# Does the combined move start from a point?
|
28
|
+
# Checks if the combined move start from a point.
|
31
29
|
#
|
32
30
|
# @return [Boolean]
|
33
31
|
def from_point?
|
34
32
|
first_leg.instance_of?(JustBackgammon::Point) if first_leg
|
35
33
|
end
|
36
34
|
|
37
|
-
#
|
35
|
+
# Checks if the combined move start with an empty point.
|
38
36
|
#
|
39
37
|
# @return [Boolean]
|
40
38
|
def empty?
|
41
39
|
first_leg.empty? if first_leg
|
42
40
|
end
|
43
41
|
|
44
|
-
#
|
42
|
+
# Checks if the combined move have pieces owned by the opponent.
|
45
43
|
#
|
46
44
|
# @return [Boolean]
|
47
45
|
def owned_by_opponent?(player_number)
|
48
46
|
first_leg.owned_by_opponent?(player_number) if first_leg
|
49
47
|
end
|
50
48
|
|
49
|
+
# Checks if the combined move have pieces owned by the opponent.
|
50
|
+
#
|
51
|
+
# @return [Boolean]
|
52
|
+
def multi_leg?
|
53
|
+
legs.size > 2
|
54
|
+
end
|
55
|
+
|
51
56
|
private
|
52
57
|
|
53
58
|
def first_leg # :nodoc:
|
@@ -248,30 +248,17 @@ module JustBackgammon
|
|
248
248
|
@errors.push JustBackgammon::BlockedError.new
|
249
249
|
when move_list.any_wrong_direction?(current_player_number)
|
250
250
|
@errors.push JustBackgammon::WrongDirectionError.new
|
251
|
-
when
|
252
|
-
move_list.number_of_moves_from_bar != bar.number_of_pieces_owned_by_player(current_player_number) && \
|
253
|
-
points.destinations(bar, dice, current_player_number).size >= move_list.number_of_moves_from_bar
|
251
|
+
when move_list.pieces_still_on_bar?(current_player_number, points, dice, bar)
|
254
252
|
@errors.push JustBackgammon::PiecesOnBarError.new
|
255
|
-
when move_list.
|
256
|
-
!(points.not_home(current_player_number).map(&:number) - move_list.map { |m| m.from.number }).empty?
|
253
|
+
when move_list.cannot_bear_off?(current_player_number, points)
|
257
254
|
@errors.push JustBackgammon::BearOffError.new
|
258
|
-
when
|
255
|
+
when move_list.dice_mismatch?(current_player_number, points, dice, bar)
|
259
256
|
@errors.push JustBackgammon::DiceMismatchError.new
|
260
257
|
end
|
261
258
|
|
262
259
|
@errors.none?
|
263
260
|
end
|
264
261
|
|
265
|
-
def current_player_has_moves? # :nodoc:
|
266
|
-
if bar.any_pieces_for_player?(@current_player_number)
|
267
|
-
@points.destinations(bar, @dice, @current_player_number).any?
|
268
|
-
else
|
269
|
-
@points.owned_by_player(@current_player_number).any? do |p|
|
270
|
-
@points.destinations(p, @dice, @current_player_number).any?
|
271
|
-
end
|
272
|
-
end
|
273
|
-
end
|
274
|
-
|
275
262
|
def perform_move(list) # :nodoc:
|
276
263
|
list.each do |move|
|
277
264
|
from = find_point(move.from.number)
|
@@ -311,22 +298,22 @@ module JustBackgammon
|
|
311
298
|
end
|
312
299
|
end
|
313
300
|
|
314
|
-
def find_point(
|
315
|
-
case
|
301
|
+
def find_point(identifier) # :nodoc:
|
302
|
+
case identifier
|
316
303
|
when 'bar'
|
317
304
|
bar
|
318
305
|
when 'off_board'
|
319
306
|
off_board
|
320
307
|
else
|
321
|
-
@points.find_by_number(
|
308
|
+
@points.find_by_number(identifier.to_i)
|
322
309
|
end
|
323
310
|
end
|
324
311
|
|
325
|
-
def sanitize_list(
|
326
|
-
|
327
|
-
JustBackgammon::Move.new({ from: find_point(
|
312
|
+
def sanitize_list(move_list) # :nodoc:
|
313
|
+
moves = move_list.map do |move|
|
314
|
+
JustBackgammon::Move.new({ from: find_point(move[:from]), to: find_point(move[:to]) })
|
328
315
|
end
|
329
|
-
JustBackgammon::MoveList.new(moves:
|
316
|
+
JustBackgammon::MoveList.new(moves: moves)
|
330
317
|
end
|
331
318
|
end
|
332
319
|
end
|
@@ -31,12 +31,12 @@ module JustBackgammon
|
|
31
31
|
#
|
32
32
|
# @return [Array<CombinedMove>]
|
33
33
|
def combined_moves
|
34
|
-
combined_data = moves.inject([]) do |combined,
|
35
|
-
matching_move = combined.find_index { |
|
34
|
+
combined_data = moves.inject([]) do |combined, move|
|
35
|
+
matching_move = combined.find_index { |combined_move| combined_move.last.number == move.from.number }
|
36
36
|
if matching_move
|
37
|
-
combined[matching_move].push(
|
37
|
+
combined[matching_move].push(move.to)
|
38
38
|
else
|
39
|
-
combined.push([
|
39
|
+
combined.push([move.from, move.to])
|
40
40
|
end
|
41
41
|
|
42
42
|
combined
|
@@ -49,94 +49,116 @@ module JustBackgammon
|
|
49
49
|
#
|
50
50
|
# @return [Boolean]
|
51
51
|
def piece_moves_multiple_times?
|
52
|
-
combined_moves.any?
|
52
|
+
combined_moves.any?(&:multi_leg?)
|
53
53
|
end
|
54
54
|
|
55
55
|
# Checks if any move have no points.
|
56
56
|
#
|
57
57
|
# @return [Boolean]
|
58
58
|
def any_missing_point?
|
59
|
-
moves.any?
|
59
|
+
moves.any?(&:missing_point?)
|
60
60
|
end
|
61
61
|
|
62
62
|
# Checks if any move have empty points.
|
63
63
|
#
|
64
64
|
# @return [Boolean]
|
65
65
|
def any_point_empty?
|
66
|
-
combined_moves.any? { |
|
66
|
+
combined_moves.any? { |move| move.from_point? && move.empty? }
|
67
67
|
end
|
68
68
|
|
69
69
|
# Checks if any move have is owned by the opponent.
|
70
70
|
#
|
71
71
|
# @return [Boolean]
|
72
72
|
def any_point_owned_by_opponent?(current_player_number)
|
73
|
-
combined_moves.any? { |
|
73
|
+
combined_moves.any? { |move| move.from_point? && move.owned_by_opponent?(current_player_number) }
|
74
74
|
end
|
75
75
|
|
76
76
|
# Checks if any move is from an empty bar.
|
77
77
|
#
|
78
78
|
# @return [Boolean]
|
79
79
|
def any_bar_empty_for_player?(current_player_number)
|
80
|
-
moves.any? { |
|
80
|
+
moves.any? { |move| move.from_bar? && move.empty_for_player?(current_player_number) }
|
81
81
|
end
|
82
82
|
|
83
83
|
# Checks if any move is blocked from moving.
|
84
84
|
#
|
85
85
|
# @return [Boolean]
|
86
86
|
def any_blocked?(current_player_number)
|
87
|
-
moves.any? { |
|
87
|
+
moves.any? { |move| move.blocked?(current_player_number) }
|
88
88
|
end
|
89
89
|
|
90
90
|
# Checks if any move is going the wrong direction.
|
91
91
|
#
|
92
92
|
# @return [Boolean]
|
93
93
|
def any_wrong_direction?(current_player_number)
|
94
|
-
moves.any? { |
|
94
|
+
moves.any? { |move| move.wrong_direction?(current_player_number) }
|
95
95
|
end
|
96
96
|
|
97
97
|
# Checks if any move is bearing off.
|
98
98
|
#
|
99
99
|
# @return [Boolean]
|
100
100
|
def any_bear_off?
|
101
|
-
moves.any?
|
101
|
+
moves.any?(&:bear_off?)
|
102
102
|
end
|
103
103
|
|
104
104
|
# Checks if all moves are from the bar.
|
105
105
|
#
|
106
106
|
# @return [Boolean]
|
107
107
|
def all_moves_from_bar?
|
108
|
-
moves.all?
|
108
|
+
moves.all?(&:from_bar?)
|
109
109
|
end
|
110
110
|
|
111
111
|
# The number of moves from the bar.
|
112
112
|
#
|
113
113
|
# @return [Fixnum]
|
114
114
|
def number_of_moves_from_bar
|
115
|
-
moves.select
|
115
|
+
moves.select(&:from_bar?).size
|
116
116
|
end
|
117
117
|
|
118
118
|
# How far each of the moves go.
|
119
119
|
#
|
120
120
|
# @return [Array<Fixnum>]
|
121
121
|
def absolute_distances(current_player_number)
|
122
|
-
moves.map { |
|
122
|
+
moves.map { |move| move.absolute_distance_for_player(current_player_number) }
|
123
|
+
end
|
124
|
+
|
125
|
+
# Checks if there are still pieces on the bar.
|
126
|
+
#
|
127
|
+
# @return [Boolean]
|
128
|
+
def pieces_still_on_bar?(current_player_number, points, dice, bar)
|
129
|
+
!all_moves_from_bar? && \
|
130
|
+
number_of_moves_from_bar != bar.number_of_pieces_owned_by_player(current_player_number) && \
|
131
|
+
points.destinations(bar, dice, current_player_number).size >= number_of_moves_from_bar
|
132
|
+
end
|
133
|
+
|
134
|
+
# Checks if they player cannot bear off.
|
135
|
+
#
|
136
|
+
# @return [Boolean]
|
137
|
+
def cannot_bear_off?(current_player_number, points)
|
138
|
+
any_bear_off? && !(points.not_home(current_player_number).map(&:number) - map { |move| move.from.number }).empty?
|
123
139
|
end
|
124
140
|
|
125
141
|
# Checks if any of the moves don't match the dice rolls
|
126
142
|
#
|
127
143
|
# @return [Boolean]
|
128
|
-
def dice_mismatch?(current_player_number, dice)
|
129
|
-
|
144
|
+
def dice_mismatch?(current_player_number, points, dice, bar)
|
145
|
+
current_player_has_moves?(current_player_number, points, dice, bar) && moves_mismatch_dice?(current_player_number, dice)
|
146
|
+
end
|
147
|
+
|
148
|
+
private
|
149
|
+
|
150
|
+
def moves_mismatch_dice?(current_player_number, dice) # :nodoc:
|
151
|
+
unallocated = dice.numbers.sort
|
130
152
|
allocated = []
|
131
153
|
|
132
|
-
moves.each do |
|
133
|
-
move_distance =
|
154
|
+
moves.each do |move|
|
155
|
+
move_distance = move.absolute_distance_for_player(current_player_number)
|
134
156
|
|
135
|
-
index = unallocated.index do |
|
136
|
-
if
|
137
|
-
|
157
|
+
index = unallocated.index do |die_number|
|
158
|
+
if move.bear_off?
|
159
|
+
die_number >= move_distance
|
138
160
|
else
|
139
|
-
|
161
|
+
die_number == move_distance
|
140
162
|
end
|
141
163
|
end
|
142
164
|
|
@@ -145,8 +167,17 @@ module JustBackgammon
|
|
145
167
|
allocated.push(die)
|
146
168
|
end
|
147
169
|
end
|
148
|
-
|
149
170
|
allocated.size != moves.size
|
150
171
|
end
|
172
|
+
|
173
|
+
def current_player_has_moves?(current_player_number, points, dice, bar) # :nodoc:
|
174
|
+
if bar.any_pieces_for_player?(current_player_number)
|
175
|
+
points.destinations(bar, dice, current_player_number).any?
|
176
|
+
else
|
177
|
+
points.owned_by_player(current_player_number).any? do |point|
|
178
|
+
points.destinations(point, dice, current_player_number).any?
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
151
182
|
end
|
152
183
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: just_backgammon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Humphreys
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-09-
|
11
|
+
date: 2016-09-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|