-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.rb
243 lines (195 loc) · 5.11 KB
/
board.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
require_relative 'square.rb'
require_relative 'grid.rb'
require_relative 'line.rb'
class Board
attr_reader :side, :squares, :lines, :win_length
# note: no need for write access for squares hash
# only read and write once for square marker
def initialize(side, win_length, squares = {})
if squares.empty?
@side = validate_side_length(side)
@squares = squares
reset
else
@side = validate_side_length(Math.sqrt(squares.count).to_i)
@squares = copy_squares squares
end
@win_length = validate_winning_line_length(win_length)
end
def to_s; grid; end
def [](key)
check(key)
squares[key].marker
end
def []=(key, marker)
check(key)
squares[key].marker = marker
end
def square_numbers
(1..side * side).to_a
end
def copy
Board.new side, win_length, copy_squares(squares)
end
# we initialize a copy of each Square obj
# if board is initialized with old(possibly still being used) squares,
def copy_squares(s)
s.map { |number, square_obj| [number, square_obj.copy] }.to_h
end
def reset
square_numbers.each { |key| @squares[key] = Square.new(key) }
nil
end
# draw forced when all lines are blocked
def no_wins_possible?
lines.all?(&:blocked?)
end
def full?
squares.all? { |_, square| !square.empty? }
end
def line_formed?
line_formed.any?
end
def winning_marker
return if !line_formed?
line = line_formed.first
line.first.marker
end
def at_risk(marker)
risk, = lines.select { |l| l.at_risk? marker }
risk.nil? ? return : risk.empty_cells[0].number
end
def at_chance(marker)
chance, = lines.select { |l| l.win_chance? marker }
chance.nil? ? return : chance.empty_cells[0].number
end
# return [] of empty squares
# grid indexed by int 1..9, left..right, top..bottom
def unmarked_squares
squares.select { |_, val| val.empty? }.keys
end
def lines
squares_at_lines.map { |squares_at_line| Line.new squares_at_line }
end
def lines_with_empty(number)
lines.select { |l| l.empty_cell? number }
end
def lines_involved(number)
lines.select { |l| l.numbers.include? number }
end
def center_square
side.next / 2
end
private
def check(key)
raise "square #{key.inspect} not found" unless squares[key]
end
# between 3 and side
def validate_winning_line_length(len)
unless len >= 3 && len <= side
raise ArgumentError, "Length must be >= 3 and <= #{side}."
end
len
end
# square board side must be odd, starting up from 3
def validate_side_length(side)
raise NotImplementedError, 'Board size > 15 is not supported.' if side > 15
return side if side.odd? && side >= 3
raise ArgumentError, 'Side length must be odd and >= 3'
end
def row_start_squares
square_numbers.each_slice(side).map(&:first)
end
def col_start_squares
square_numbers.first side
end
# array of squares that are on the end of grid rows
def row_end_squares
square_numbers.each_slice(side).map(&:last)
end
def col_end_squares
square_numbers.last side
end
# returns an array of arrays
# each the length of winning_line_length
# generate_groups_to_scan
def groups
horizontals + verticals + upwards + downwards
end
def horizontals
# select each full groups that are between row-start and row-ends
square_numbers.each_slice(side).flat_map do |row|
row.each_cons(win_length).to_a
end
end
def verticals
v = square_numbers.each_slice(side).to_a.transpose
v.flat_map { |col| col.each_cons(win_length).to_a }
end
def downward_starts
col_start_squares | row_start_squares
end
def downward_ends
row_end_squares | col_end_squares
end
def downward_groups(starts, ends)
starts.map do |n|
line = []
loop do
line << n
break line if ends.include?(n) || n < 1
n += (side + 1)
end
end
end
def downwards
groups = downward_groups(downward_starts, downward_ends)
groups
.reject { |line| line.size < win_length }
.flat_map { |line| line.each_cons(win_length).to_a }
end
def upward_starts
row_start_squares | col_end_squares
end
def upward_ends
col_start_squares | row_end_squares
end
def upward_groups(starts, ends)
starts.map do |n|
line = []
loop do
line << n
break line if ends.include?(n) || n < 1
n -= (side - 1)
end
end
end
def upwards
groups = upward_groups(upward_starts, upward_ends)
groups
.reject { |line| line.size < win_length }
.flat_map { |line| line.each_cons(win_length).to_a }
end
def squares_at_lines(g = groups)
g.map { |square| squares_at(*square) }
end
def squares_at(*args)
squares.values_at(*args)
end
def line_formed
squares_at_lines.select do |line|
square1 = line.first
next if square1.empty?
line.all? { |square| square.marker == square1.marker }
end
end
def grid
Grid.new(squares.values.map(&:symbol), side).to_s
end
end
if __FILE__ == $PROGRAM_NAME
# test board display at side length 3..9
b = Board.new 5, 4
puts b
puts b[1]
end