berlin-ai 0.0.33 → 0.0.34

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.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- berlin-ai (0.0.29)
4
+ berlin-ai (0.0.33)
5
5
  rainbow (~> 1.1.4)
6
6
  sinatra (= 1.4.3)
7
7
  sinatra-contrib (= 1.4.1)
@@ -17,7 +17,7 @@ GEM
17
17
  eventmachine (1.0.3)
18
18
  method_source (0.8.2)
19
19
  minitest (4.7.5)
20
- multi_json (1.7.9)
20
+ multi_json (1.8.0)
21
21
  pry (0.9.12.2)
22
22
  coderay (~> 1.0.5)
23
23
  method_source (~> 0.8)
data/README.md CHANGED
@@ -64,7 +64,7 @@ Helper methods to work with nodes.
64
64
  ```ruby
65
65
  map.directed? # Determine if a path from A to B is A -> B (directed) or A <-> B (not directed)
66
66
  map.nodes # All the nodes.
67
- map.owned_nodes # Owned nodes, including those with 0 soldiers.
67
+ map.owned_nodes # Your nodes, including those with 0 soldiers.
68
68
  map.enemy_nodes # Enemy nodes.
69
69
  map.free_nodes # Nodes not controlled by anyone.
70
70
  map.foreign_nodes # Nodes not owned (i.e. enemy nodes and free nodes).
@@ -77,6 +77,7 @@ Node objects obtained when querying the map.
77
77
 
78
78
  ```ruby
79
79
  node.id # Id of the node.
80
+ node.map # The map to which the node belongs to
80
81
  node.type # Type of node.
81
82
  node.player_id # Owner of the node.
82
83
  node.number_of_soldiers # Number of soldiers on the node.
@@ -85,7 +86,8 @@ node.available_soldiers # Owned remaining soldiers on this node (result fr
85
86
  node.==(other) # Check if two nodes are the same.
86
87
  node.adjacent?(other_node) # Check if two nodes are adjacents.
87
88
  node.occupied? # Check if some soldiers are on the node.
88
- node.owned? # Check if you own the node.
89
+ node.owned? # Check if a player (not only you) owns the node.
90
+ node.mine? # Check if you own the node.
89
91
  node.free? # Check if no one own the node.
90
92
  node.owned_by?(player_id) # Check if the node is owned by a given player.
91
93
  node.adjacent_nodes # Get a list of adjacent nodes.
data/bin/berlin-ai ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'erb'
4
+ require 'optparse'
5
+
6
+ options = {}
7
+ parser = OptionParser.new do |opts|
8
+ opts.banner = 'Usage: berlin-ai [options]'
9
+
10
+ opts.on("--init=NAME") do |name|
11
+ options[:name] = name
12
+ end
13
+ end.parse!
14
+
15
+ require File.expand_path('../../lib/version', __FILE__)
16
+
17
+ name = options[:name]
18
+
19
+ Dir.mkdir name
20
+
21
+ %w(Gemfile config.ru ai.rb).each do |file|
22
+ path = File.expand_path("../../data/#{file}", __FILE__)
23
+ template = ERB.new(File.read(path))
24
+
25
+ File.open(File.expand_path("./#{name}/#{file}"), "w") do |f|
26
+ f.puts template.result
27
+ end
28
+ end
29
+
data/data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+ ruby "1.9.3"
3
+
4
+ gem 'berlin-ai', '<%= Berlin::AI::VERSION %>'
5
+ gem 'thin'
6
+
7
+ group :development do
8
+ gem 'shotgun'
9
+ end
data/data/ai.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'berlin-ai'
2
+
3
+ class Berlin::AI::Player
4
+ def self.on_turn(game)
5
+ # Here goes magic
6
+ end
7
+ end
data/data/config.ru ADDED
@@ -0,0 +1,3 @@
1
+ require './ai'
2
+
3
+ run Sinatra::Application
data/lib/ai/map.rb CHANGED
@@ -15,37 +15,27 @@ module Berlin
15
15
 
16
16
  # Returns an array of all owned nodes
17
17
  def owned_nodes
18
- nodes.select do |node|
19
- node.owned_by?(@player_id)
20
- end
18
+ nodes.select{ |n| n.mine? }
21
19
  end
22
20
 
23
21
  # Returns an array of all enemy nodes
24
22
  def enemy_nodes
25
- nodes.select do |node|
26
- node.owned? && !owned_by?(@player_id)
27
- end
23
+ nodes.select{ |n| n.enemy? }
28
24
  end
29
25
 
30
26
  # Returns an array of all free nodes
31
27
  def free_nodes
32
- nodes.select do |node|
33
- node.free?
34
- end
28
+ nodes.select{ |n| n.free? }
35
29
  end
36
30
 
37
31
  # Returns an array of all nodes that we don't owned
38
32
  def foreign_nodes
39
- nodes.reject do |node|
40
- node.owned_by?(@player_id)
41
- end
33
+ nodes.reject{ |n| n.mine? }
42
34
  end
43
35
 
44
36
  # We can now loop on our owned nodes in order to find our controlled nodes.
45
37
  def controlled_nodes
46
- owned_nodes.select do |node|
47
- node.occupied?
48
- end
38
+ owned_nodes.select{ |n| n.occupied? }
49
39
  end
50
40
 
51
41
  # Is the map directed?
@@ -21,7 +21,8 @@ module Berlin
21
21
  # the node and how many soldiers there is. We'll get back to that later.
22
22
  # map['nodes'] => [{:id => STRING}, ...]
23
23
  data['nodes'].each do |node|
24
- map.nodes_hash[node['id']] = Berlin::AI::Node.parse(node.merge(types[node['type']]))
24
+ node_data = node.merge(types[node['type']]).merge(:map => map)
25
+ map.nodes_hash[node['id']] = Berlin::AI::Node.parse(node_data)
25
26
  end
26
27
 
27
28
  # Same thing here, with paths.
data/lib/ai/node.rb CHANGED
@@ -6,12 +6,12 @@ module Berlin
6
6
  class Node
7
7
  include Internal
8
8
 
9
- attr_accessor :id, :player_id, :number_of_soldiers, :incoming_soldiers,
9
+ attr_accessor :id, :map, :player_id, :number_of_soldiers, :incoming_soldiers,
10
10
  :available_soldiers, :type, :soldiers_per_turn, :points
11
11
 
12
12
  # Returns true if other_node is adjacent to self
13
13
  def adjacent?(other_node)
14
- @links.include? other_node
14
+ @links.include?(other_node)
15
15
  end
16
16
 
17
17
  # Returns true if self has more than zero soldier
@@ -24,6 +24,16 @@ module Berlin
24
24
  !!@player_id
25
25
  end
26
26
 
27
+ # Returns true if yours
28
+ def mine?
29
+ owned_by?(@map.player_id)
30
+ end
31
+
32
+ # Returns true if owned by somebody else than you
33
+ def enemy?
34
+ owned? && !mine?
35
+ end
36
+
27
37
  # Returns true if no one on the node
28
38
  def free?
29
39
  !owned?
@@ -41,7 +51,7 @@ module Berlin
41
51
 
42
52
  # Returns a list of all adjacent nodes, plus self
43
53
  def adjacent_nodes_and_self
44
- adjacent_nodes.push( self )
54
+ adjacent_nodes.push(self)
45
55
  end
46
56
  end
47
57
  end
data/lib/version.rb CHANGED
@@ -2,7 +2,7 @@ module Berlin
2
2
  module AI
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- BUILD = 33
5
+ BUILD = 34
6
6
 
7
7
  VERSION = "#{MAJOR}.#{MINOR}.#{BUILD}"
8
8
  end
data/test/map_test.rb CHANGED
@@ -9,15 +9,16 @@ require "test/unit"
9
9
  class MapTest < Test::Unit::TestCase
10
10
 
11
11
  def setup
12
- @node1 = Berlin::AI::Node.new(:id => 1, :player_id => 1)
13
- @node2 = Berlin::AI::Node.new(:id => 2, :player_id => 1)
14
- @node3 = Berlin::AI::Node.new(:id => 3, :player_id => nil)
15
- @node4 = Berlin::AI::Node.new(:id => 4, :player_id => 2)
16
- @node5 = Berlin::AI::Node.new(:id => 5, :player_id => 3)
17
-
18
12
  @map = Berlin::AI::Map.new
19
13
  @map.player_id = 1
20
- @map.nodes = {1 => @node1, 2 => @node2, 3 => @node3, 4 => @node4, 5 => @node5}
14
+
15
+ @node1 = Berlin::AI::Node.new(:id => 1, :map => @map, :player_id => 1, :number_of_soldiers => 0)
16
+ @node2 = Berlin::AI::Node.new(:id => 2, :map => @map, :player_id => 1, :number_of_soldiers => 3)
17
+ @node3 = Berlin::AI::Node.new(:id => 3, :map => @map, :player_id => nil)
18
+ @node4 = Berlin::AI::Node.new(:id => 4, :map => @map, :player_id => 2)
19
+ @node5 = Berlin::AI::Node.new(:id => 5, :map => @map, :player_id => 3)
20
+
21
+ @map.nodes_hash = {1 => @node1, 2 => @node2, 3 => @node3, 4 => @node4, 5 => @node5}
21
22
  end
22
23
 
23
24
  def test_nodes_returns_an_array_of_all_nodes
@@ -28,8 +29,20 @@ class MapTest < Test::Unit::TestCase
28
29
  assert_equal [@node1, @node2], @map.owned_nodes
29
30
  end
30
31
 
31
- def test_foreign_nodes_returns_and_array_of_nodes_that_the_current_player_does_not_owned
32
+ def test_foreign_nodes_returns_an_array_of_nodes_that_the_current_player_does_not_owned
32
33
  assert_equal [@node3, @node4, @node5], @map.foreign_nodes
33
34
  end
34
35
 
36
+ def test_enemy_nodes_returns_an_array_of_nodes_owned_by_other_players
37
+ assert_equal [@node4, @node5], @map.enemy_nodes
38
+ end
39
+
40
+ def test_free_nodes_returns_an_array_of_free_nodes
41
+ assert_equal [@node3], @map.free_nodes
42
+ end
43
+
44
+ def test_controlled_nodes_returns_an_array_of_owned_nodes_with_at_least_one_soldier
45
+ assert_equal [@node2], @map.controlled_nodes
46
+ end
47
+
35
48
  end
data/test/node_test.rb CHANGED
@@ -18,7 +18,9 @@ class NodeTest < Test::Unit::TestCase
18
18
 
19
19
  def test_reset_resets_all_turn_relative_data
20
20
  node = Berlin::AI::Node.new(:number_of_soldiers => 2, :incoming_soldiers => 3, :available_soldiers => 4)
21
+
21
22
  node.reset!
23
+
22
24
  assert_equal 0, node.incoming_soldiers
23
25
  assert_equal node.number_of_soldiers, node.available_soldiers
24
26
  end
@@ -34,7 +36,7 @@ class NodeTest < Test::Unit::TestCase
34
36
  assert !node1.adjacent?(node3)
35
37
  end
36
38
 
37
- def test_occupied_returns_true_if_the_node_has_at_least_one_soldiers
39
+ def test_occupied_returns_true_if_the_node_has_at_least_one_soldier
38
40
  node = Berlin::AI::Node.new
39
41
 
40
42
  assert !node.occupied?
@@ -73,5 +75,18 @@ class NodeTest < Test::Unit::TestCase
73
75
 
74
76
  assert node.owned?
75
77
  end
78
+
79
+ def test_mine_returns_true_if_owned_by_current_player
80
+ Struct.new('Map', :player_id)
81
+ node = Berlin::AI::Node.new
82
+
83
+ node.map = Struct::Map.new(1)
84
+
85
+ assert !node.mine?
86
+
87
+ node.player_id = 1
88
+
89
+ assert node.mine?
90
+ end
76
91
 
77
92
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: berlin-ai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.33
4
+ version: 0.0.34
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-09-13 00:00:00.000000000 Z
14
+ date: 2013-09-17 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: sinatra
@@ -139,6 +139,10 @@ files:
139
139
  - LICENSE
140
140
  - README.md
141
141
  - berlin-ai.gemspec
142
+ - bin/berlin-ai
143
+ - data/Gemfile
144
+ - data/ai.rb
145
+ - data/config.ru
142
146
  - lib/ai/fake.rb
143
147
  - lib/ai/game.rb
144
148
  - lib/ai/game_internal.rb