ai_games-four_in_a_row 0.2.0 → 0.3.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: 2c4eb792df08b0da9aa7d4d6ca4882270229a766
4
- data.tar.gz: c12e84f4fbc52ee5e55794312c91dcf606e83584
3
+ metadata.gz: 4e1e9b6d64ac0459f8d5464252c00b21448d05c3
4
+ data.tar.gz: de6ba6b68141cbc0daab9dc3ee8a34a9f6198e18
5
5
  SHA512:
6
- metadata.gz: af8fccec454c11da2e45ba043aff9f3c2451730527f0f6bf74a31c10ee39d1c67d7613490aa4f8129e621204aad979b2424a96a7e4e6d7c3bc78935dfd261650
7
- data.tar.gz: 02e7f94a13794cdf051ad58d0447d498fd55087797ac135491f6097aca97beae4693ad2e1be3e37c67773a6de650bc813658af27a75a4924235b854a4bc148ce
6
+ metadata.gz: 2dc3298df00dcd690d3bf3a0d21b9dfa8be83d37a6e8540c579a861b6a5ba2ac734328b195a0304a677e257595a83ffcd28874095c2736d278efcd293d3f95a5
7
+ data.tar.gz: 5b8208f24fa26123f61fb1d19d2029e4b998f86d5d8008c56cfbc818e9463ff5796e4962fb8d4d997870af4d5f09f484405eee65fc07c6bdfe1d5f9b201a5985
@@ -49,7 +49,7 @@ module AIGames
49
49
  # Handles changes on the playing field. Whenever the "owner" of a cell
50
50
  # changes, the bot gets notified about it via this method. Overwrite this
51
51
  # method to add your own logic.
52
- def update(row, column, new_owner)
52
+ def update(cell, new_owner)
53
53
  end
54
54
 
55
55
  # Starts this bot.
@@ -0,0 +1,42 @@
1
+ # Copyright (c) 2015 Jan David Nose
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+ module AIGames
21
+ module FourInARow
22
+ # The playing field consists of cells. Each cell is located in a certain row
23
+ # and column, and can belong to one of the players.
24
+ class Cell
25
+ # The row the cell is in
26
+ attr_reader :row
27
+
28
+ # The column the cell is in
29
+ attr_reader :column
30
+
31
+ # The bot who fills this cell
32
+ attr_accessor :owner
33
+
34
+ # Initializes the cell with the given position, and optionally the owner.
35
+ def initialize(row, column, owner = nil)
36
+ @row = row
37
+ @column = column
38
+ @owner = owner
39
+ end
40
+ end
41
+ end
42
+ end
@@ -17,6 +17,7 @@
17
17
  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
18
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
19
  # THE SOFTWARE.
20
+ require 'ai_games/four_in_a_row/cell'
20
21
  require 'observer'
21
22
 
22
23
  module AIGames
@@ -41,7 +42,7 @@ module AIGames
41
42
  resize_field rows, columns
42
43
  end
43
44
 
44
- # Returns the owner of a cell. If the cell has no owner, nil is returned.
45
+ # Returns the cell at the given position.
45
46
  def get_cell(row, column)
46
47
  @fields[row][column]
47
48
  end
@@ -49,11 +50,12 @@ module AIGames
49
50
  # Updates a cell. If the owner of the cell changes, all observers of the
50
51
  # playing field get notified.
51
52
  def set_cell(row, column, owner)
52
- current_owner = @fields[row][column]
53
+ cell = @fields[row][column]
54
+ current_owner = cell.owner
53
55
  return if current_owner == owner
54
56
 
55
- @fields[row][column] = owner
56
- notify_observers(row, column, owner)
57
+ cell.owner = owner
58
+ notify_observers(cell, owner)
57
59
  end
58
60
 
59
61
  # Sets number of rows. If the new number of rows is greater than the
@@ -70,10 +72,17 @@ module AIGames
70
72
 
71
73
  private
72
74
 
75
+ # Resizes the playing field. Each time this method gets called, a new
76
+ # array is created and filled with new cells.
73
77
  def resize_field(rows, columns)
78
+ @fields = Array.new(rows) { Array.new(columns) }
74
79
  @columns = columns
75
80
  @rows = rows
76
- @fields = Array.new(rows) { Array.new(columns) { nil } }
81
+ (0...rows).each do |r|
82
+ (0...columns).each do |c|
83
+ @fields[r][c] = Cell.new(r, c)
84
+ end
85
+ end
77
86
  end
78
87
  end
79
88
  end
@@ -1,5 +1,5 @@
1
1
  module AIGames
2
2
  module FourInARow
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan David Nose
@@ -117,6 +117,7 @@ files:
117
117
  - bin/setup
118
118
  - lib/ai_games/four_in_a_row.rb
119
119
  - lib/ai_games/four_in_a_row/bot.rb
120
+ - lib/ai_games/four_in_a_row/cell.rb
120
121
  - lib/ai_games/four_in_a_row/match.rb
121
122
  - lib/ai_games/four_in_a_row/parser.rb
122
123
  - lib/ai_games/four_in_a_row/playing_field.rb