ai_games-four_in_a_row 0.1.3 → 0.2.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: 42e0ba8c31008881456778cc02689ea8f2190797
4
- data.tar.gz: db7cf4a7f7b110ee8391a64c5f24fcbd4f78ff5b
3
+ metadata.gz: 2c4eb792df08b0da9aa7d4d6ca4882270229a766
4
+ data.tar.gz: c12e84f4fbc52ee5e55794312c91dcf606e83584
5
5
  SHA512:
6
- metadata.gz: bd539d8c3b9af4aedc054c5a2a7d14f7a7dc6a0b8a3c8324bcc6bf8688f2b2074a92362e17440879a54a218829738c68233e3020536692d20f5ff7bf6736cd18
7
- data.tar.gz: a4d05e02c5f0463933dc50b9dfb2149780f68bbaf2e7cd3cb22f6fb2b32fb149bbc7ffcd524e83e5173df9680769c6175ea19e919fb3c344481deec709643b06
6
+ metadata.gz: af8fccec454c11da2e45ba043aff9f3c2451730527f0f6bf74a31c10ee39d1c67d7613490aa4f8129e621204aad979b2424a96a7e4e6d7c3bc78935dfd261650
7
+ data.tar.gz: 02e7f94a13794cdf051ad58d0447d498fd55087797ac135491f6097aca97beae4693ad2e1be3e37c67773a6de650bc813658af27a75a4924235b854a4bc148ce
@@ -46,6 +46,12 @@ module AIGames
46
46
  rand(0...match.playing_field.columns)
47
47
  end
48
48
 
49
+ # Handles changes on the playing field. Whenever the "owner" of a cell
50
+ # changes, the bot gets notified about it via this method. Overwrite this
51
+ # method to add your own logic.
52
+ def update(row, column, new_owner)
53
+ end
54
+
49
55
  # Starts this bot.
50
56
  def run
51
57
  @match.parser.run
@@ -52,7 +52,7 @@ module AIGames
52
52
  attr_reader :parser
53
53
 
54
54
  # The instance of the playing field
55
- attr_accessor :playing_field
55
+ attr_reader :playing_field
56
56
 
57
57
  # Initializes a new match instance. If a custom bot is given, it is used
58
58
  # as the player's bot, else the default bot is used for both the player
@@ -114,7 +114,7 @@ module AIGames
114
114
  field_string.split(';').each do |r|
115
115
  j = 0
116
116
  r.split(',').each do |c|
117
- match.playing_field[i][j] = match.bots[c.to_i]
117
+ match.playing_field.set_cell(i, j, match.bots[c.to_i])
118
118
  j += 1
119
119
  end
120
120
  i += 1
@@ -17,6 +17,8 @@
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 'observer'
21
+
20
22
  module AIGames
21
23
  module FourInARow
22
24
  # This class represents the playing field of the game. It has a given number
@@ -24,24 +26,34 @@ module AIGames
24
26
  # field is filled, the name of the player whose chip fills the field is
25
27
  # stored in it.
26
28
  class PlayingField
29
+ # Implement observer pattern to notify bots when the owner of a cell
30
+ # changes.
31
+ include Observable
32
+
27
33
  # The number of columns of the playing field
28
34
  attr_reader :columns
29
35
 
30
36
  # The number of rows of the playing field
31
37
  attr_reader :rows
32
38
 
33
- # A two-dimensional array containing all the fields of the playing field
34
- attr_reader :fields
35
-
36
39
  # Initialize the playing field with the given number of rows and columns.
37
40
  def initialize(rows = 0, columns = 0)
38
41
  resize_field rows, columns
39
42
  end
40
43
 
41
- # Shortcut to the field elements, using the playing field itself like the
42
- # underlying array structure.
43
- def [](index)
44
- @fields[index]
44
+ # Returns the owner of a cell. If the cell has no owner, nil is returned.
45
+ def get_cell(row, column)
46
+ @fields[row][column]
47
+ end
48
+
49
+ # Updates a cell. If the owner of the cell changes, all observers of the
50
+ # playing field get notified.
51
+ def set_cell(row, column, owner)
52
+ current_owner = @fields[row][column]
53
+ return if current_owner == owner
54
+
55
+ @fields[row][column] = owner
56
+ notify_observers(row, column, owner)
45
57
  end
46
58
 
47
59
  # Sets number of rows. If the new number of rows is greater than the
@@ -1,5 +1,5 @@
1
1
  module AIGames
2
2
  module FourInARow
3
- VERSION = '0.1.3'
3
+ VERSION = '0.2.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.1.3
4
+ version: 0.2.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-25 00:00:00.000000000 Z
11
+ date: 2015-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ai_games-logger