ai_games-four_in_a_row 0.3.0 → 0.4.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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 78694aacaf1fee4e7f09ec115fdd059f692ee45f
|
4
|
+
data.tar.gz: 29e1558eb5acc4440b439b454d888636bde3eeaa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6c6864f30229a00cb4d1e7c7368c8ce4943d440408b72140063210d043de00597ffadbe71797971bee416bd946d47a06cf15afa2fd779ccd68a3f6b1cf693d5
|
7
|
+
data.tar.gz: f95332b5907f6b97153bc5a82f7a8d5d97b9e15068da17e161f3ac0559b0af951b021ee3ac2b5439841a60b4e7928b011903576f5d1ea2ec1370780dc43d6be1
|
@@ -31,11 +31,20 @@ module AIGames
|
|
31
31
|
# The bot who fills this cell
|
32
32
|
attr_accessor :owner
|
33
33
|
|
34
|
+
# The neighbors of this cell
|
35
|
+
attr_accessor :neighbors
|
36
|
+
|
34
37
|
# Initializes the cell with the given position, and optionally the owner.
|
35
38
|
def initialize(row, column, owner = nil)
|
36
39
|
@row = row
|
37
40
|
@column = column
|
38
41
|
@owner = owner
|
42
|
+
@neighbors = []
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns all neighbors having the same owner.
|
46
|
+
def neighbors_with_same_owner
|
47
|
+
neighbors.select { |x| x.owner == owner }
|
39
48
|
end
|
40
49
|
end
|
41
50
|
end
|
@@ -47,6 +47,17 @@ module AIGames
|
|
47
47
|
@fields[row][column]
|
48
48
|
end
|
49
49
|
|
50
|
+
# Returns all cells in the given area. Pass in two ranges to limit area.
|
51
|
+
def get_cells(rows, columns)
|
52
|
+
cells = []
|
53
|
+
rows.each do |r|
|
54
|
+
columns.each do |c|
|
55
|
+
cells << @fields[r][c]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
cells
|
59
|
+
end
|
60
|
+
|
50
61
|
# Updates a cell. If the owner of the cell changes, all observers of the
|
51
62
|
# playing field get notified.
|
52
63
|
def set_cell(row, column, owner)
|
@@ -72,17 +83,43 @@ module AIGames
|
|
72
83
|
|
73
84
|
private
|
74
85
|
|
86
|
+
# Fills the playing field with cells.
|
87
|
+
def create_cells
|
88
|
+
(0...rows).each do |r|
|
89
|
+
(0...columns).each do |c|
|
90
|
+
@fields[r][c] = Cell.new(r, c)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# Find all neighbors of a cell.
|
96
|
+
def find_neighbors(cell)
|
97
|
+
min_r = [0, cell.row - 1].max
|
98
|
+
max_r = [cell.row + 1, rows - 1].min
|
99
|
+
min_c = [0, cell.column - 1].max
|
100
|
+
max_c = [cell.column + 1, columns - 1].min
|
101
|
+
|
102
|
+
get_cells(min_r..max_r, min_c..max_c).select { |x| x unless x == cell }
|
103
|
+
end
|
104
|
+
|
105
|
+
# Sets up neighbor relationships for all cells.
|
106
|
+
def link_cells
|
107
|
+
(0...rows).each do |r|
|
108
|
+
(0...columns).each do |c|
|
109
|
+
cell = @fields[r][c]
|
110
|
+
cell.neighbors = find_neighbors(cell)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
75
115
|
# Resizes the playing field. Each time this method gets called, a new
|
76
116
|
# array is created and filled with new cells.
|
77
117
|
def resize_field(rows, columns)
|
78
118
|
@fields = Array.new(rows) { Array.new(columns) }
|
79
119
|
@columns = columns
|
80
120
|
@rows = rows
|
81
|
-
|
82
|
-
|
83
|
-
@fields[r][c] = Cell.new(r, c)
|
84
|
-
end
|
85
|
-
end
|
121
|
+
create_cells
|
122
|
+
link_cells
|
86
123
|
end
|
87
124
|
end
|
88
125
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ai_games-four_in_a_row
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan David Nose
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ai_games-logger
|