gameboard 1.2.0 → 2.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/lib/gameboard.rb +2 -0
- data/lib/gameboard/board.rb +68 -32
- data/lib/gameboard/cell.rb +2 -1
- data/lib/gameboard/coordinate.rb +18 -1
- data/lib/gameboard/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc06eb2b5a787b4ffa5f157f737ac4e06c3431dd
|
4
|
+
data.tar.gz: 2cc0b36fd6542e428b3af420f0abde9debf42347
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c7824e45aaf8091103d4be8fcbfc8cf394fcbf87d4601edbc1ae1035ceda53e7722b9ad86b7a2be9aebb080371298ea57497c4ab5c9bfc22f9a6acf94782e7e8
|
7
|
+
data.tar.gz: dfc07732f725670ba6ebdd2e9fe31c2e610bf71a52fabf178027e262333ea85ea26c3b8fddc9c87fab2799ff1615e86b8bb5b1999524ecb83d2cdce973b22a3a
|
data/lib/gameboard.rb
CHANGED
data/lib/gameboard/board.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
require_relative './cell'
|
2
|
+
|
2
3
|
module Gameboard
|
3
4
|
class Board
|
4
5
|
attr_reader :height, :width, :board
|
6
|
+
include Enumerable
|
5
7
|
|
6
8
|
def initialize(height:false, width: false, cells: false, preset: false)
|
7
9
|
raise ArgumentError unless ((height.is_a?(Integer) && width.is_a?(Integer)) || !!preset)
|
@@ -11,13 +13,60 @@ module Gameboard
|
|
11
13
|
preset ? load_game(preset) : new_board
|
12
14
|
end
|
13
15
|
|
14
|
-
def
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
def each(&block)
|
17
|
+
return enum_for(__method__) if block.nil?
|
18
|
+
board.each(&block)
|
19
|
+
board
|
20
|
+
end
|
21
|
+
|
22
|
+
def each_value(&block)
|
23
|
+
return enum_for(__method__) if block.nil?
|
24
|
+
each{|cell| yield(cell.value)}
|
25
|
+
end
|
26
|
+
|
27
|
+
def each_coordinate(&block)
|
28
|
+
return enum_for(__method__) if block.nil?
|
29
|
+
each { |cell| yield(cell.coord.position) }
|
30
|
+
end
|
31
|
+
|
32
|
+
def delta(point, slope, coord = false)
|
33
|
+
delta = point.zip(slope).map {|point| point.reduce(:+) }
|
34
|
+
piece = board.find { |cell| cell.coord.position == delta }
|
35
|
+
raise "Off Grid" unless !!piece
|
36
|
+
coord ? piece.coord.position : piece.value
|
37
|
+
end
|
38
|
+
|
39
|
+
def diagonal(coords = false)
|
40
|
+
diagonals = []
|
41
|
+
|
42
|
+
height.times do |i|
|
43
|
+
diagonals << get_diagonal([0, i], coords)
|
44
|
+
diagonals << get_diagonal([0, height - 1 - i], coords, false)
|
45
|
+
end
|
46
|
+
(1...width).each do
|
47
|
+
|i| diagonals << get_diagonal([i, 0], coords)
|
48
|
+
diagonals << get_diagonal([i, height - 1], coords, false)
|
49
|
+
end
|
50
|
+
|
51
|
+
diagonals
|
52
|
+
end
|
53
|
+
|
54
|
+
def find_cell(coord)
|
55
|
+
board.find {|cell| cell.coord.position == coord}
|
56
|
+
end
|
57
|
+
|
58
|
+
def full?
|
59
|
+
board.none? { |cell| cell.value == cells }
|
60
|
+
end
|
61
|
+
|
62
|
+
def horizontal(coords = false)
|
63
|
+
columns = []
|
64
|
+
height.times do |y|
|
65
|
+
columns << board.select { |cell| cell.coord.y == y }.map do |cell|
|
66
|
+
coords ? cell.coord.position : cell.value
|
19
67
|
end
|
20
68
|
end
|
69
|
+
columns
|
21
70
|
end
|
22
71
|
|
23
72
|
def load_game(saved_game)
|
@@ -32,18 +81,20 @@ module Gameboard
|
|
32
81
|
end
|
33
82
|
end
|
34
83
|
|
35
|
-
def
|
36
|
-
|
84
|
+
def neighbors(coord, points = false)
|
85
|
+
temp = Coordinate.new(coord[0], coord[1])
|
86
|
+
valid_neighbors(temp).map do |cell|
|
87
|
+
points ? cell.coord.position : cell.value
|
88
|
+
end
|
37
89
|
end
|
38
90
|
|
39
|
-
def
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
91
|
+
def new_board
|
92
|
+
@board = []
|
93
|
+
width.times do |x|
|
94
|
+
height.times do |y|
|
95
|
+
@board << Cell.new(Coordinate.new(x,y), value: cells)
|
44
96
|
end
|
45
97
|
end
|
46
|
-
columns
|
47
98
|
end
|
48
99
|
|
49
100
|
def vertical(coords = false)
|
@@ -56,25 +107,6 @@ module Gameboard
|
|
56
107
|
columns
|
57
108
|
end
|
58
109
|
|
59
|
-
def diagonal(coords = false)
|
60
|
-
diagonals = []
|
61
|
-
|
62
|
-
height.times do |i|
|
63
|
-
diagonals << get_diagonal([0, i], coords)
|
64
|
-
diagonals << get_diagonal([0, height - 1 - i], coords, false)
|
65
|
-
end
|
66
|
-
(1...width).each do
|
67
|
-
|i| diagonals << get_diagonal([i, 0], coords)
|
68
|
-
diagonals << get_diagonal([i, height - 1], coords, false)
|
69
|
-
end
|
70
|
-
|
71
|
-
diagonals
|
72
|
-
end
|
73
|
-
|
74
|
-
def full?
|
75
|
-
@board.none? { |cell| cell.value == cells }
|
76
|
-
end
|
77
|
-
|
78
110
|
private
|
79
111
|
attr_reader :cells
|
80
112
|
|
@@ -88,5 +120,9 @@ module Gameboard
|
|
88
120
|
end
|
89
121
|
diagonal.compact.map{|cell| coords ? cell.coord.position : cell.value }
|
90
122
|
end
|
123
|
+
|
124
|
+
def valid_neighbors(coord)
|
125
|
+
coord.neighbors.collect { |point| find_cell(point) }.compact
|
126
|
+
end
|
91
127
|
end
|
92
128
|
end
|
data/lib/gameboard/cell.rb
CHANGED
data/lib/gameboard/coordinate.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module Gameboard
|
2
2
|
class Coordinate
|
3
3
|
attr_reader :position, :x, :y
|
4
|
+
|
4
5
|
def initialize(x,y)
|
5
6
|
invalid_type = "Coordinates must be integers!"
|
6
7
|
raise invalid_type unless (x.is_a?(Integer) && y.is_a?(Integer))
|
@@ -8,5 +9,21 @@ module Gameboard
|
|
8
9
|
@x = x
|
9
10
|
@y = y
|
10
11
|
end
|
12
|
+
|
13
|
+
def neighbors
|
14
|
+
relative_neighbors.map { |point| delta(point) }
|
15
|
+
end
|
16
|
+
private
|
17
|
+
def delta(point)
|
18
|
+
point.zip(position).map {|combined| combined.reduce(:+) }
|
19
|
+
end
|
20
|
+
|
21
|
+
def relative_neighbors
|
22
|
+
[
|
23
|
+
[-1, 1], [0, 1], [1, 1],
|
24
|
+
[-1, 0], [1, 0],
|
25
|
+
[-1, -1], [-1, 0], [-1, 1]
|
26
|
+
]
|
27
|
+
end
|
11
28
|
end
|
12
|
-
end
|
29
|
+
end
|
data/lib/gameboard/version.rb
CHANGED