berlin-ai 0.0.12 → 0.0.13
Sign up to get free protection for your applications and to get access to all the features.
- data/berlin-ai.gemspec +1 -0
- data/lib/ai/game.rb +15 -7
- data/lib/ai/map.rb +0 -2
- data/lib/ai/node.rb +10 -0
- data/lib/ai/player.rb +9 -0
- data/lib/berlin-ai.rb +10 -5
- data/lib/version.rb +1 -1
- data/test/test_ai.rb +5 -9
- metadata +5 -4
data/berlin-ai.gemspec
CHANGED
data/lib/ai/game.rb
CHANGED
@@ -2,7 +2,7 @@ module Berlin
|
|
2
2
|
module AI
|
3
3
|
# Game keeps track of current games played by the server, indexing them on their uniq id.
|
4
4
|
class Game
|
5
|
-
attr_reader :id, :map
|
5
|
+
attr_reader :id, :map, :moves, :player_id, :number_of_players, :time_limit_per_turn
|
6
6
|
|
7
7
|
# Keep track of all current games
|
8
8
|
@@games = {}
|
@@ -39,18 +39,26 @@ module Berlin
|
|
39
39
|
|
40
40
|
def initialize id, map, infos
|
41
41
|
@id = id
|
42
|
+
|
43
|
+
# Extract usefull informations
|
44
|
+
@player_id = infos['player_id']
|
45
|
+
@time_limit_per_turn = infos['time_limit_per_turn']
|
46
|
+
@number_of_players = infos['number_of_players']
|
47
|
+
|
48
|
+
# Keep track of the player moves
|
49
|
+
@moves = []
|
50
|
+
|
51
|
+
# Create the map
|
42
52
|
@map = Berlin::AI::Map.new map, infos
|
43
53
|
end
|
54
|
+
|
55
|
+
def add_move from, to, number_of_soldiers
|
56
|
+
@moves << {:from=>from.to_i, :to=>to.to_i, :number_of_soldiers=>number_of_soldiers.to_i}
|
57
|
+
end
|
44
58
|
|
45
59
|
def update state
|
46
60
|
@map.update state
|
47
61
|
end
|
48
|
-
|
49
|
-
# This method must be overritten with yours. The return value of this method will be returned
|
50
|
-
# in a json format to Berlin and be interpreted as the moves you'd like to do.
|
51
|
-
def turn_moves
|
52
|
-
raise "Please... overwrite me!"
|
53
|
-
end
|
54
62
|
end
|
55
63
|
end
|
56
64
|
end
|
data/lib/ai/map.rb
CHANGED
@@ -4,8 +4,6 @@ module Berlin
|
|
4
4
|
# nodes, points, soldiers, etc. Game will then be able to pick any information
|
5
5
|
# it wants from map to decide what are the best moves to do.
|
6
6
|
class Map
|
7
|
-
attr_reader :player_id
|
8
|
-
|
9
7
|
def initialize map, infos
|
10
8
|
@player_id = infos['player_id']
|
11
9
|
@nodes = {}
|
data/lib/ai/node.rb
CHANGED
@@ -16,6 +16,11 @@ module Berlin
|
|
16
16
|
@player_id = 0
|
17
17
|
@links = []
|
18
18
|
end
|
19
|
+
|
20
|
+
# Somewhat useful
|
21
|
+
def to_i
|
22
|
+
@id.to_i
|
23
|
+
end
|
19
24
|
|
20
25
|
# Registers a given node as an adjacent one.
|
21
26
|
def link_to other_node
|
@@ -41,6 +46,11 @@ module Berlin
|
|
41
46
|
def adjacent_nodes
|
42
47
|
@links.dup
|
43
48
|
end
|
49
|
+
|
50
|
+
# Returns a list of all adjacent nodes, plus self
|
51
|
+
def adjacent_nodes_and_self
|
52
|
+
adjacent_nodes.push( self )
|
53
|
+
end
|
44
54
|
end
|
45
55
|
end
|
46
56
|
end
|
data/lib/ai/player.rb
ADDED
data/lib/berlin-ai.rb
CHANGED
@@ -11,10 +11,11 @@ OptionParser.new do |opts|
|
|
11
11
|
opts.on("-p N", "--port=N", Integer, "Set running port to N") do |p|
|
12
12
|
set :port, p
|
13
13
|
end
|
14
|
-
|
15
|
-
opts.on("--debug", "Run in debug mode (reloads code at each request)") do |
|
16
|
-
if
|
17
|
-
|
14
|
+
|
15
|
+
opts.on("--debug", "Run in debug mode (reloads code at each request)") do |d|
|
16
|
+
if d
|
17
|
+
require 'sinatra/reloader'
|
18
|
+
|
18
19
|
configure do |c|
|
19
20
|
c.also_reload $0
|
20
21
|
end
|
@@ -32,7 +33,11 @@ post '/' do
|
|
32
33
|
game = Berlin::AI::Game.create_or_update params[:action], params[:infos], params[:map], params[:state]
|
33
34
|
|
34
35
|
if ['ping', 'turn'].include? params[:action]
|
35
|
-
|
36
|
+
# Let the player decides his moves
|
37
|
+
Berlin::AI::Player.on_turn( game )
|
38
|
+
|
39
|
+
# Return the response to Berlin
|
40
|
+
return game.moves.to_json
|
36
41
|
end
|
37
42
|
else
|
38
43
|
p params.inspect
|
data/lib/version.rb
CHANGED
data/test/test_ai.rb
CHANGED
@@ -1,14 +1,10 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'berlin-ai'
|
3
3
|
|
4
|
-
class Berlin::AI::
|
5
|
-
def
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
:to => node.adjacent_nodes.sample.id,
|
10
|
-
:number_of_soldiers => rand(node.number_of_soldiers + 1)
|
11
|
-
}
|
12
|
-
end
|
4
|
+
class Berlin::AI::Player
|
5
|
+
def self.on_turn( game )
|
6
|
+
|
7
|
+
|
8
|
+
game.add_move( 1, 2, 12 )
|
13
9
|
end
|
14
10
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: berlin-ai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 5
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 13
|
10
|
+
version: 0.0.13
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Christian Blais
|
@@ -17,7 +17,7 @@ autorequire:
|
|
17
17
|
bindir: bin
|
18
18
|
cert_chain: []
|
19
19
|
|
20
|
-
date: 2011-05-
|
20
|
+
date: 2011-05-24 00:00:00 -04:00
|
21
21
|
default_executable:
|
22
22
|
dependencies:
|
23
23
|
- !ruby/object:Gem::Dependency
|
@@ -88,6 +88,7 @@ files:
|
|
88
88
|
- lib/ai/game.rb
|
89
89
|
- lib/ai/map.rb
|
90
90
|
- lib/ai/node.rb
|
91
|
+
- lib/ai/player.rb
|
91
92
|
- test/test_ai.rb
|
92
93
|
has_rdoc: true
|
93
94
|
homepage: http://github.com/christianblais/berlin-ai
|