berlin-ai 0.0.27 → 0.0.28

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ai/game.rb CHANGED
@@ -51,7 +51,14 @@ module Berlin
51
51
  end
52
52
 
53
53
  def add_move from, to, number_of_soldiers
54
- @moves << {:from=>from.to_i, :to=>to.to_i, :number_of_soldiers=>number_of_soldiers.to_i}
54
+ # remove moving soldiers from from node
55
+ from.available_soldiers -= number_of_soldiers
56
+
57
+ # adding incoming soldiers to next node
58
+ to.incoming_soldiers += number_of_soldiers
59
+
60
+ # add move
61
+ @moves << {:from => from.to_i, :to => to.to_i, :number_of_soldiers => number_of_soldiers.to_i}
55
62
  end
56
63
 
57
64
  def update infos, state
@@ -63,8 +70,9 @@ module Berlin
63
70
  @map.update state
64
71
  end
65
72
 
66
- def clear_moves
73
+ def reset!
67
74
  @moves = []
75
+ @map.nodes.each(&:reset!)
68
76
  end
69
77
  end
70
78
  end
data/lib/ai/map.rb CHANGED
@@ -42,7 +42,28 @@ module Berlin
42
42
  # Returns an array of all owned nodes
43
43
  def owned_nodes
44
44
  nodes.select do |node|
45
- node.owned_by? @player_id
45
+ node.owned_by?(@player_id)
46
+ end
47
+ end
48
+
49
+ # Returns an array of all enemy nodes
50
+ def enemy_nodes
51
+ nodes.select do |node|
52
+ node.owned? && !owned_by?(@player_id)
53
+ end
54
+ end
55
+
56
+ # Returns an array of all free nodes
57
+ def free_nodes
58
+ nodes.select do |node|
59
+ node.free?
60
+ end
61
+ end
62
+
63
+ # Returns an array of all nodes that we don't owned
64
+ def foreign_nodes
65
+ nodes.reject do |node|
66
+ node.owned_by?(@player_id)
46
67
  end
47
68
  end
48
69
 
data/lib/ai/node.rb CHANGED
@@ -4,7 +4,7 @@ module Berlin
4
4
  # We'll be able to use it in order to know if two
5
5
  # nodes are adjacent, how much points worth a node, etc.
6
6
  class Node
7
- attr_accessor :id, :player_id, :number_of_soldiers, :type
7
+ attr_accessor :id, :player_id, :number_of_soldiers, :incoming_soldiers, :available_soldiers, :type
8
8
  attr_reader :soldiers_per_turn, :points
9
9
 
10
10
  def initialize node, type
@@ -13,22 +13,33 @@ module Berlin
13
13
  @points = type['points']
14
14
  @soldiers_per_turn = type['soldiers_per_turn']
15
15
  @number_of_soldiers = 0
16
- @player_id = 0
16
+ @player_id = nil
17
17
  @links = []
18
18
  end
19
+
20
+ # Reset information for new turn
21
+ def reset!
22
+ self.incoming_soldiers = 0
23
+ self.available_soldiers = self.number_of_soldiers
24
+ end
19
25
 
20
26
  # Somewhat useful
21
27
  def to_i
22
28
  @id.to_i
23
29
  end
24
30
 
31
+ # Used to compare if two nodes are the same
32
+ def ==(other)
33
+ other.id == @id
34
+ end
35
+
25
36
  # Registers a given node as an adjacent one.
26
- def link_to other_node
37
+ def link_to(other_node)
27
38
  @links << other_node
28
39
  end
29
40
 
30
41
  # Returns true if other_node is adjacent to self
31
- def adjacent? other_node
42
+ def adjacent?(other_node)
32
43
  @links.include? other_node
33
44
  end
34
45
 
@@ -36,9 +47,19 @@ module Berlin
36
47
  def occupied?
37
48
  @number_of_soldiers > 0
38
49
  end
50
+
51
+ # Returns true if owned by any player
52
+ def owned?
53
+ !!@player_id
54
+ end
55
+
56
+ # Returns true if no one on the node
57
+ def free?
58
+ !owned?
59
+ end
39
60
 
40
61
  # Returns true if node owned by provided player id
41
- def owned_by? player_id
62
+ def owned_by?(player_id)
42
63
  @player_id == player_id
43
64
  end
44
65
 
data/lib/ai/player.rb CHANGED
@@ -2,7 +2,7 @@ module Berlin
2
2
  module AI
3
3
  class Player
4
4
  def self.on_turn
5
- raise "Overwrite me."
5
+ raise NotImplementedError
6
6
  end
7
7
  end
8
8
  end
data/lib/berlin-ai.rb CHANGED
@@ -65,7 +65,7 @@ post '/' do
65
65
 
66
66
  if ['ping', 'turn'].include?(params[:action])
67
67
  # Clear old moves
68
- game.clear_moves
68
+ game.reset!
69
69
 
70
70
  # Let the player decides his moves
71
71
  Berlin::AI::Player.on_turn(game)
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 = 27
5
+ BUILD = 28
6
6
 
7
7
  VERSION = "#{MAJOR}.#{MINOR}.#{BUILD}"
8
8
  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.27
4
+ version: 0.0.28
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: