gameboard 3.5.0 → 4.0.0
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/bin/preview +2 -2
- data/lib/gameboard/board.rb +100 -121
- data/lib/gameboard/cell.rb +3 -3
- data/lib/gameboard/coordinate.rb +4 -2
- data/lib/gameboard/render.rb +2 -2
- data/lib/gameboard/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: a2ca6f9592b619a43214ddc04663d93b3ef7206d
|
4
|
+
data.tar.gz: a0d06dad2b4044952a95580f84043faa7f54cc64
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 093e50a3af3a3cd74f2e7518d8712205c1f457bba00534f718220f6e49f6721f42fdb86c571cdd53f0c74fae53184a0bbb085f148677ccae74e706a49f437d8f
|
7
|
+
data.tar.gz: 5b8e24283506264ec88edad80e8c826006461c42f8d7c2ac0a7a323db1e8a46cb1a6e8dbec3905b27074771f02428390d4f6a35587a120036b76543a7fb537dc
|
data/bin/preview
CHANGED
@@ -10,10 +10,10 @@ class ShowOff
|
|
10
10
|
# ["A", "B", "C"],
|
11
11
|
# ["X", "Y", "Z"]
|
12
12
|
# ]
|
13
|
-
board = Board.new(height: 8, width: 10,
|
13
|
+
board = Board.new(height: 8, width: 10, cell_value: "O")
|
14
14
|
# board = Board.new(preset: set)
|
15
15
|
Render::clear
|
16
|
-
Render::board(board.horizontal)
|
16
|
+
Render::board(board.to_val(:horizontal))
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
data/lib/gameboard/board.rb
CHANGED
@@ -14,20 +14,20 @@ module Gameboard
|
|
14
14
|
|
15
15
|
# Public: Initialize a gameboard.
|
16
16
|
#
|
17
|
-
# height
|
18
|
-
#
|
19
|
-
#
|
20
|
-
# width
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
# preset -
|
26
|
-
#
|
17
|
+
# height - The number of horizontal rows on the gameboard. Not
|
18
|
+
# passing a height will raise an ArgumentError unless a
|
19
|
+
# preset board is passed.
|
20
|
+
# width - The number of columns on the gameboard. Not passing a
|
21
|
+
# width will raise an ArgumentError unless a preset board
|
22
|
+
# is passed.
|
23
|
+
# cell_value - An optional value, preferably a String in most cases,
|
24
|
+
# that changes the default cell value.
|
25
|
+
# preset - An optional 2D Array to load a preset gameboard. Saved
|
26
|
+
# games should be created in rows from the top down.
|
27
27
|
#
|
28
28
|
# Examples
|
29
29
|
#
|
30
|
-
# board = Board.new(
|
30
|
+
# board = Board.new(cell_value: "O", width: 10, height: 8)
|
31
31
|
# # => -----------------------------------------
|
32
32
|
# 0 | O | O | O | O | O | O | O | O | O | O |
|
33
33
|
# -----------------------------------------
|
@@ -65,12 +65,12 @@ module Gameboard
|
|
65
65
|
# - 0 - 1 - 2 -
|
66
66
|
#
|
67
67
|
#
|
68
|
-
def initialize(height:
|
68
|
+
def initialize(height: nil, width: nil, cell_value: nil, preset: false)
|
69
69
|
raise ArgumentError unless ((height.is_a?(Integer) && width.is_a?(Integer)) || !!preset)
|
70
70
|
@height = height
|
71
71
|
@width = width
|
72
72
|
@nailed = false
|
73
|
-
@
|
73
|
+
@default_cell = cell_value
|
74
74
|
preset ? load_game(preset) : new_board
|
75
75
|
end
|
76
76
|
|
@@ -95,7 +95,7 @@ module Gameboard
|
|
95
95
|
board
|
96
96
|
end
|
97
97
|
|
98
|
-
# Public: An enumerator that yields each
|
98
|
+
# Public: An enumerator that yields each cell's value.
|
99
99
|
# Example
|
100
100
|
#
|
101
101
|
# board.each_value { |cell| p cell }
|
@@ -105,28 +105,28 @@ module Gameboard
|
|
105
105
|
#
|
106
106
|
def each_value(&block)
|
107
107
|
return enum_for(__method__) if block.nil?
|
108
|
-
each{|cell| yield(cell.
|
108
|
+
each{|cell| yield(cell.val)}
|
109
109
|
end
|
110
110
|
|
111
|
-
# Public: An enumerator that yields each
|
111
|
+
# Public: An enumerator that yields each cell's coordinates.
|
112
112
|
# Example
|
113
113
|
#
|
114
114
|
# board.each { |cell| p cell }
|
115
|
-
# #=> [0, 0]
|
116
|
-
# #=> [1, 0]
|
117
|
-
# #=> [2, 0]
|
118
|
-
# #=> [0, 1]
|
119
|
-
# #=> [1, 1]...
|
115
|
+
# #=> Coordinate (.position == [0, 0])
|
116
|
+
# #=> Coordinate (.position == [1, 0])
|
117
|
+
# #=> Coordinate (.position == [2, 0])
|
118
|
+
# #=> Coordinate (.position == [0, 1])
|
119
|
+
# #=> Coordinate (.position == [1, 1])...
|
120
120
|
#
|
121
121
|
def each_coordinate(&block)
|
122
122
|
return enum_for(__method__) if block.nil?
|
123
|
-
each { |cell| yield(cell.coord
|
123
|
+
each { |cell| yield(cell.coord) }
|
124
124
|
end
|
125
125
|
|
126
126
|
# Public: Return the value of the next available point given a coordinate
|
127
127
|
# and a slope.
|
128
128
|
#
|
129
|
-
# point - The starting coordinate
|
129
|
+
# point - The starting point. [x, y] coordinate
|
130
130
|
# slope - An [x, y] slope (e.g. [1,1] means over one row, up one column).
|
131
131
|
# It must be an integer.
|
132
132
|
# coords - An optional boolean to specify returning a coordinate pair
|
@@ -148,15 +148,14 @@ module Gameboard
|
|
148
148
|
# delta([0,0], [2, 1])
|
149
149
|
# #=> C ([2,1] if coord argument given)
|
150
150
|
#
|
151
|
-
def delta(point, slope
|
152
|
-
|
153
|
-
|
154
|
-
raise "Off Grid" unless !!piece
|
155
|
-
coords ? piece.coord.position : piece.value
|
151
|
+
def delta(point, slope)
|
152
|
+
base = find_cell(point)
|
153
|
+
find_cell(base.coord.send(:delta, slope))
|
156
154
|
end
|
157
155
|
|
158
|
-
# Public: Return every diagonal on the gameboard. It does not
|
159
|
-
# square board to function.
|
156
|
+
# Public: Return an Array of Gameboard::Cell for every diagonal on the gameboard. It does not
|
157
|
+
# require a square board to function. Run #to_val(:diagonal) if you only need the
|
158
|
+
# values at each index
|
160
159
|
#
|
161
160
|
# coords: An optional boolean to specify returning an Array of coordinates
|
162
161
|
# instead of values.
|
@@ -179,37 +178,26 @@ module Gameboard
|
|
179
178
|
# ["Y", "C"], [2, "C"], ["Z"], [3]
|
180
179
|
# ]
|
181
180
|
#
|
182
|
-
#
|
183
|
-
#
|
184
|
-
#
|
185
|
-
# [[0, 2], [1, 1], [2, 0]],
|
186
|
-
# [[0, 1], [1, 2]],
|
187
|
-
# [[0, 1], [1, 0]],
|
188
|
-
# [[0, 2]],
|
189
|
-
# [[0, 0]],
|
190
|
-
# [[1, 0], [2, 1]],
|
191
|
-
# [[1, 2], [2, 1]],
|
192
|
-
# [[2, 0]],
|
193
|
-
# [[2, 2]]
|
194
|
-
# ]
|
181
|
+
# #=> Note: Values above are for demonstration. Each value will actually be a
|
182
|
+
# Gameboard::Cell with methods #coord and #val
|
183
|
+
#
|
195
184
|
#
|
196
|
-
def diagonal
|
185
|
+
def diagonal
|
197
186
|
diagonals = []
|
198
187
|
|
199
188
|
height.times do |i|
|
200
|
-
diagonals << get_diagonal([0, i]
|
201
|
-
diagonals << get_diagonal([0, height - 1 - i],
|
189
|
+
diagonals << get_diagonal([0, i])
|
190
|
+
diagonals << get_diagonal([0, height - 1 - i], -1)
|
202
191
|
end
|
203
|
-
(1...width).each do
|
204
|
-
|
205
|
-
diagonals << get_diagonal([i, height - 1],
|
192
|
+
(1...width).each do |i|
|
193
|
+
diagonals << get_diagonal([i, 0])
|
194
|
+
diagonals << get_diagonal([i, height - 1], -1)
|
206
195
|
end
|
207
|
-
|
208
196
|
diagonals
|
209
197
|
end
|
210
198
|
|
211
199
|
def empty?
|
212
|
-
board.all? { |cell| cell.
|
200
|
+
board.all? { |cell| cell.val == default_cell }
|
213
201
|
end
|
214
202
|
|
215
203
|
# Public: Find a cell at given coordinate.
|
@@ -245,7 +233,7 @@ module Gameboard
|
|
245
233
|
#
|
246
234
|
def flip
|
247
235
|
raise "The Board is Nailed to the Table" unless !nailed_down?
|
248
|
-
board.each {|cell| cell.
|
236
|
+
board.each {|cell| cell.val = default_cell}
|
249
237
|
end
|
250
238
|
|
251
239
|
# Public: Check if the gameboard is full relative to the default cell.
|
@@ -267,19 +255,19 @@ module Gameboard
|
|
267
255
|
# full?
|
268
256
|
# #=> true
|
269
257
|
#
|
270
|
-
# Board.new(height: 3, width: 3,
|
258
|
+
# Board.new(height: 3, width: 3, default: "Y")
|
271
259
|
#
|
272
260
|
# full?
|
273
261
|
# #=> false (because our default piece is now "Y")
|
274
262
|
#
|
275
263
|
def full?
|
276
|
-
board.none? { |cell| cell.
|
264
|
+
board.none? { |cell| cell.val == default_cell }
|
277
265
|
end
|
278
266
|
|
279
|
-
# Public: Return every row on the gameboard.
|
267
|
+
# Public: Return every row on the gameboard. Run #to_val(:horizontal) if you only need the
|
268
|
+
# values at each index
|
269
|
+
#
|
280
270
|
#
|
281
|
-
# coords: An optional boolean to specify returning an Array of coordinates
|
282
|
-
# instead of values.
|
283
271
|
#
|
284
272
|
# Example
|
285
273
|
# -------------
|
@@ -299,19 +287,13 @@ module Gameboard
|
|
299
287
|
# [1, 2, 3]
|
300
288
|
# ]
|
301
289
|
#
|
302
|
-
#
|
303
|
-
#
|
304
|
-
# [[0, 0], [1, 0], [2, 0]],
|
305
|
-
# [[0, 1], [1, 1], [2, 1]],
|
306
|
-
# [[0, 2], [1, 2], [2, 2]]]
|
307
|
-
# ]
|
290
|
+
# #=> Note: Values above are for demonstration. Each value will actually be a
|
291
|
+
# Gameboard::Cell with methods #coord and #val
|
308
292
|
#
|
309
|
-
def horizontal
|
293
|
+
def horizontal
|
310
294
|
rows = []
|
311
295
|
height.times do |y|
|
312
|
-
rows << board.select { |cell| cell.coord.y == y }
|
313
|
-
coords ? cell.coord.position : cell.value
|
314
|
-
end
|
296
|
+
rows << board.select { |cell| cell.coord.y == y }
|
315
297
|
end
|
316
298
|
rows
|
317
299
|
end
|
@@ -326,11 +308,9 @@ module Gameboard
|
|
326
308
|
!!nailed
|
327
309
|
end
|
328
310
|
|
329
|
-
# Public: Return
|
311
|
+
# Public: Return an array of Gameboard::Cell for valid neighbors of a given coordinate.
|
330
312
|
#
|
331
|
-
#
|
332
|
-
# coords - An optional boolean to return an array of coordinate
|
333
|
-
# pairs
|
313
|
+
# coord - an [x, y] coordinate
|
334
314
|
#
|
335
315
|
#
|
336
316
|
# Examples
|
@@ -343,23 +323,11 @@ module Gameboard
|
|
343
323
|
# -------------
|
344
324
|
# - 0 - 1 - 2 -
|
345
325
|
#
|
346
|
-
# neighbors(
|
326
|
+
# neighbors(Coordinate.new(0,0))
|
347
327
|
# #=> [1, 2, 3, "A", "C", "X", "A", 1]
|
348
328
|
#
|
349
|
-
|
350
|
-
|
351
|
-
# [0, 2], [1, 2],
|
352
|
-
# [2, 2], [0, 1],
|
353
|
-
# [2, 1], [0, 0],
|
354
|
-
# [0, 1], [0, 2]
|
355
|
-
# ]
|
356
|
-
#
|
357
|
-
#
|
358
|
-
def neighbors(point, coords = false)
|
359
|
-
temp = Coordinate.new(point[0], point[1])
|
360
|
-
valid_neighbors(temp).map do |cell|
|
361
|
-
coords ? cell.coord.position : cell.value
|
362
|
-
end
|
329
|
+
def neighbors(coord)
|
330
|
+
find_cell(coord).coord.neighbors.map { |point| find_cell(point) }.compact
|
363
331
|
end
|
364
332
|
|
365
333
|
# Public: Set random cells to a given value.
|
@@ -368,19 +336,21 @@ module Gameboard
|
|
368
336
|
#
|
369
337
|
def randomize(piece)
|
370
338
|
rand(1...board.length).times do
|
371
|
-
board[rand(board.length)].
|
339
|
+
board[rand(board.length)].val = piece
|
372
340
|
end
|
373
341
|
end
|
374
|
-
# Public: Set a
|
342
|
+
# Public: Set a cell's value at a given coordinate. Returns false if no cell found
|
375
343
|
#
|
376
344
|
# coord - A coordinate pair in the form X, Y
|
345
|
+
#
|
377
346
|
def set_cell(coord, value)
|
378
347
|
cell = find_cell(coord)
|
379
|
-
|
380
|
-
cell.
|
348
|
+
return false unless cell
|
349
|
+
cell.val = value if cell
|
381
350
|
end
|
382
351
|
|
383
|
-
# Public: Return every column on the gameboard.
|
352
|
+
# Public: Return every column on the gameboard. Run #to_val(:vertical) if you only need the
|
353
|
+
# values at each index
|
384
354
|
#
|
385
355
|
# coords: An optional boolean to specify returning an Array of coordinates
|
386
356
|
# instead of values.
|
@@ -402,43 +372,60 @@ module Gameboard
|
|
402
372
|
# ["Y", "B", 2],
|
403
373
|
# ["Z", "C", 3]
|
404
374
|
# ]
|
375
|
+
# #=> Note: Values above are for demonstration. Each value will actually be a
|
376
|
+
# Gameboard::Cell with methods #coord and #val
|
405
377
|
#
|
406
|
-
|
407
|
-
# #=> [
|
408
|
-
# [[0, 0], [0, 1], [0, 2]],
|
409
|
-
# [[1, 0], [1, 1], [1, 2]],
|
410
|
-
# [[2, 0], [2, 1], [2, 2]]]
|
411
|
-
# ]
|
412
|
-
#
|
413
|
-
def vertical(coords = false)
|
378
|
+
def vertical
|
414
379
|
columns = []
|
415
380
|
width.times do |x|
|
416
|
-
columns << board.select { |cell| cell.coord.x == x }
|
417
|
-
coords ? cell.coord.position : cell.value
|
418
|
-
end
|
381
|
+
columns << board.select { |cell| cell.coord.x == x }
|
419
382
|
end
|
420
383
|
columns
|
421
384
|
end
|
422
385
|
|
386
|
+
# Public: Helper method to return values instead of Cells for a given method.
|
387
|
+
#
|
388
|
+
# method_sym - A symbol of the method you want to run.
|
389
|
+
# args - optional, pass an argument to the method you are calling
|
390
|
+
#
|
391
|
+
# Example
|
392
|
+
# -------------
|
393
|
+
# 0 | 1 | 2 | 3 |
|
394
|
+
# -------------
|
395
|
+
# 1 | A | B | C |
|
396
|
+
# -------------
|
397
|
+
# 2 | X | Y | Z |
|
398
|
+
# -------------
|
399
|
+
# - 0 - 1 - 2 -
|
400
|
+
#
|
401
|
+
#
|
402
|
+
# to_val(:vertical)
|
403
|
+
# #=> [
|
404
|
+
# ["X", "A", 1],
|
405
|
+
# ["Y", "B", 2],
|
406
|
+
# ["Z", "C", 3]
|
407
|
+
# ]
|
408
|
+
def to_val(method_sym, args = (no_args = true; nil))
|
409
|
+
(no_args ? public_send(method_sym) : public_send(method_sym,args)).map do |item|
|
410
|
+
item.is_a?(Array) ? item.collect { |cell| cell.val } : item.val
|
411
|
+
end
|
412
|
+
end
|
413
|
+
|
423
414
|
private
|
424
415
|
# Internal: Returns the gameboard array.
|
425
416
|
attr_reader :board
|
426
417
|
# Internal: Returns the default gameboard cell value
|
427
|
-
attr_reader :
|
418
|
+
attr_reader :default_cell
|
428
419
|
# Internal: Stores whether the board is nailed down
|
429
420
|
attr_reader :nailed
|
430
421
|
|
431
422
|
# Internal: return a diagonal array given a starting point and a
|
432
423
|
# slope.
|
433
|
-
def get_diagonal(start,
|
434
|
-
oper = (slope == true ? :+ : :-)
|
424
|
+
def get_diagonal(start, slope = 1)
|
435
425
|
diagonal = (0...height).map do |i|
|
436
|
-
|
437
|
-
if ((0...width).include?(start[0] + i) && (0...height).include?(start[1].send(oper, i)))
|
438
|
-
board.find {|cell| cell.coord.position == position}
|
439
|
-
end
|
426
|
+
delta(start, [i, i * slope])
|
440
427
|
end
|
441
|
-
diagonal.compact
|
428
|
+
diagonal.compact
|
442
429
|
end
|
443
430
|
|
444
431
|
# Internal: Set the current gameboard.
|
@@ -449,7 +436,7 @@ module Gameboard
|
|
449
436
|
#
|
450
437
|
# Examples
|
451
438
|
#
|
452
|
-
# board = Board.new(
|
439
|
+
# board = Board.new(default_cell: "O", width: 10, height: 8)
|
453
440
|
# # => -----------------------------------------
|
454
441
|
# 0 | O | O | O | O | O | O | O | O | O | O |
|
455
442
|
# -----------------------------------------
|
@@ -495,7 +482,7 @@ module Gameboard
|
|
495
482
|
@width = saved_game[0].length
|
496
483
|
saved_game.transpose.each_with_index do |row, x|
|
497
484
|
row.each.each_with_index do |cell, y|
|
498
|
-
@board << Cell.new(coord: Coordinate.new(x,y),
|
485
|
+
@board << Cell.new(coord: Coordinate.new(x,y), val: cell)
|
499
486
|
end
|
500
487
|
end
|
501
488
|
end
|
@@ -505,17 +492,9 @@ module Gameboard
|
|
505
492
|
@board = []
|
506
493
|
width.times do |x|
|
507
494
|
height.times do |y|
|
508
|
-
@board << Cell.new(coord: Coordinate.new(x,y),
|
495
|
+
@board << Cell.new(coord: Coordinate.new(x,y), val: default_cell)
|
509
496
|
end
|
510
497
|
end
|
511
498
|
end
|
512
|
-
|
513
|
-
# Internal: use the Coordinate::neighbors function to collect the
|
514
|
-
# neighbors of a given Coordinate on the existing gameboard
|
515
|
-
#
|
516
|
-
# point - instance of Coordinate class at a specific x,y point
|
517
|
-
def valid_neighbors(coord)
|
518
|
-
coord.neighbors.collect { |point| find_cell(point) }.compact
|
519
|
-
end
|
520
499
|
end
|
521
500
|
end
|
data/lib/gameboard/cell.rb
CHANGED
@@ -3,7 +3,7 @@ module Gameboard
|
|
3
3
|
class Cell
|
4
4
|
|
5
5
|
# Public: Returns the value of the Cell instance.
|
6
|
-
attr_accessor :
|
6
|
+
attr_accessor :val
|
7
7
|
|
8
8
|
# Public: Retuns the Coordinate of the Cell instance. Returns type
|
9
9
|
# Gameboard::Coordinate.
|
@@ -14,9 +14,9 @@ module Gameboard
|
|
14
14
|
# coord - A Coordinate class instance at postition X,Y.
|
15
15
|
# value - The cell's value.
|
16
16
|
#
|
17
|
-
def initialize(coord: false,
|
17
|
+
def initialize(coord: false, val: false)
|
18
18
|
raise TypeError unless coord.is_a?(Coordinate)
|
19
|
-
@
|
19
|
+
@val = val
|
20
20
|
@coord = coord
|
21
21
|
end
|
22
22
|
end
|
data/lib/gameboard/coordinate.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
module Gameboard
|
2
2
|
class Coordinate
|
3
3
|
# Public: Returns the X,Y coordinate of the instance as [X, Y].
|
4
|
-
attr_reader :position
|
5
4
|
# Public: Returns the X coordinate of the instance.
|
6
5
|
attr_reader :x
|
7
6
|
# Public: Returns the Y coordinate of the instance.
|
@@ -15,7 +14,6 @@ module Gameboard
|
|
15
14
|
def initialize(x,y)
|
16
15
|
invalid_type = "Coordinates must be integers!"
|
17
16
|
raise invalid_type unless (x.is_a?(Integer) && y.is_a?(Integer))
|
18
|
-
@position = [x,y]
|
19
17
|
@x = x
|
20
18
|
@y = y
|
21
19
|
end
|
@@ -34,6 +32,10 @@ module Gameboard
|
|
34
32
|
def neighbors
|
35
33
|
relative_neighbors.map { |point| delta(point) }
|
36
34
|
end
|
35
|
+
|
36
|
+
def position
|
37
|
+
[x, y]
|
38
|
+
end
|
37
39
|
private
|
38
40
|
|
39
41
|
# Internal: Add an array with :position.
|
data/lib/gameboard/render.rb
CHANGED
@@ -10,11 +10,11 @@ module Gameboard
|
|
10
10
|
# ClassMethod: calls render methods in order of needed
|
11
11
|
# execution.
|
12
12
|
#
|
13
|
-
# gameboard - A 2D array built by Board.horizontal
|
13
|
+
# gameboard - A 2D array built by Board.to_val(:horizontal)
|
14
14
|
#
|
15
15
|
# Example
|
16
16
|
#
|
17
|
-
# Render::board(Board.new(height:3, width: 3).horizontal)
|
17
|
+
# Render::board(Board.new(height:3, width: 3).to_val(:horizontal))
|
18
18
|
#
|
19
19
|
def self.board(gameboard)
|
20
20
|
system("tput setab 0;") unless Gem.win_platform?
|
data/lib/gameboard/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gameboard
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 4.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sampson Crowley
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-11-
|
11
|
+
date: 2016-11-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|