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: 4e1e9b6d64ac0459f8d5464252c00b21448d05c3
4
- data.tar.gz: de6ba6b68141cbc0daab9dc3ee8a34a9f6198e18
3
+ metadata.gz: 78694aacaf1fee4e7f09ec115fdd059f692ee45f
4
+ data.tar.gz: 29e1558eb5acc4440b439b454d888636bde3eeaa
5
5
  SHA512:
6
- metadata.gz: 2dc3298df00dcd690d3bf3a0d21b9dfa8be83d37a6e8540c579a861b6a5ba2ac734328b195a0304a677e257595a83ffcd28874095c2736d278efcd293d3f95a5
7
- data.tar.gz: 5b8208f24fa26123f61fb1d19d2029e4b998f86d5d8008c56cfbc818e9463ff5796e4962fb8d4d997870af4d5f09f484405eee65fc07c6bdfe1d5f9b201a5985
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
- (0...rows).each do |r|
82
- (0...columns).each do |c|
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
@@ -1,5 +1,5 @@
1
1
  module AIGames
2
2
  module FourInARow
3
- VERSION = '0.3.0'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  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.3.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-26 00:00:00.000000000 Z
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