berlin-ai 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/berlin-ai.gemspec +2 -2
- data/lib/ai/game.rb +8 -8
- data/lib/ai/map.rb +1 -1
- data/lib/ai/node.rb +4 -4
- data/lib/berlin-ai.rb +7 -0
- data/lib/version.rb +1 -1
- data/test/test_ai.rb +8 -14
- metadata +13 -9
data/berlin-ai.gemspec
CHANGED
@@ -14,8 +14,8 @@ Gem::Specification.new do |s|
|
|
14
14
|
s.summary = "Berlin Artificial Intelligence"
|
15
15
|
s.description = "Berlin Artificial Intelligence"
|
16
16
|
|
17
|
-
s.add_dependency 'sinatra'
|
18
|
-
s.add_dependency 'yajl-ruby'
|
17
|
+
s.add_dependency 'sinatra', '>=1.2.6'
|
18
|
+
s.add_dependency 'yajl-ruby', '>=0.8.2'
|
19
19
|
|
20
20
|
s.files = [
|
21
21
|
'LICENSE',
|
data/lib/ai/game.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
module Berlin
|
2
2
|
module AI
|
3
|
+
# Game keeps track of current games played by the server, indexing them on their uniq id.
|
3
4
|
class Game
|
4
5
|
attr_reader :id
|
5
6
|
|
7
|
+
# Keep track of all current games
|
6
8
|
@@games = {}
|
7
9
|
|
8
10
|
def self.create_or_update action, infos, map, state
|
9
11
|
# Check for params and quit on errors
|
10
|
-
return
|
12
|
+
return if action.nil? || infos.nil? || map.nil? || state.nil?
|
11
13
|
|
12
14
|
# First, we parse the received request
|
13
15
|
infos = JSON.parse( infos )
|
@@ -21,7 +23,7 @@ module Berlin
|
|
21
23
|
|
22
24
|
if action == "game_over"
|
23
25
|
# Release the game to avoid memory leaks
|
24
|
-
@@games
|
26
|
+
@@games.delete game_id
|
25
27
|
elsif state
|
26
28
|
# Now, we want to update the current state of the game with the new content
|
27
29
|
game.update state
|
@@ -30,19 +32,17 @@ module Berlin
|
|
30
32
|
game
|
31
33
|
end
|
32
34
|
|
33
|
-
# @id = Uniq game ID (params[:game])
|
34
|
-
# @map = Current state of the game (params[:json])
|
35
35
|
def initialize id, map, infos
|
36
|
-
@id
|
37
|
-
@map
|
36
|
+
@id = id
|
37
|
+
@map = Berlin::AI::Map.new map, infos
|
38
38
|
end
|
39
39
|
|
40
|
-
# Let's update the map with the latest state
|
41
40
|
def update state
|
42
41
|
@map.update state
|
43
42
|
end
|
44
43
|
|
45
|
-
#
|
44
|
+
# This method must be overritten with yours. The return value of this method will be returned
|
45
|
+
# in a json format to Berlin and be interpreted as the moves you'd like to do.
|
46
46
|
def turn_moves
|
47
47
|
raise "Please... overwrite me!"
|
48
48
|
end
|
data/lib/ai/map.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Berlin
|
2
2
|
module AI
|
3
|
-
# Map
|
3
|
+
# Map keeps track of all the useful information needed to play, such as
|
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
|
data/lib/ai/node.rb
CHANGED
@@ -13,22 +13,22 @@ module Berlin
|
|
13
13
|
@links = []
|
14
14
|
end
|
15
15
|
|
16
|
-
#
|
16
|
+
# Registers a given node as an adjacent one.
|
17
17
|
def link_to other_node
|
18
18
|
@links << other_node
|
19
19
|
end
|
20
20
|
|
21
|
-
#
|
21
|
+
# Returns true if other_node is adjacent to self
|
22
22
|
def adjacent? other_node
|
23
23
|
@links.include? other_node
|
24
24
|
end
|
25
25
|
|
26
|
-
#
|
26
|
+
# Returns true if self has more than zero soldier
|
27
27
|
def occupied?
|
28
28
|
@number_of_soldiers > 0
|
29
29
|
end
|
30
30
|
|
31
|
-
#
|
31
|
+
# Returns a list of all adjacent nodes
|
32
32
|
def adjacent_nodes
|
33
33
|
@links.dup
|
34
34
|
end
|
data/lib/berlin-ai.rb
CHANGED
@@ -1,9 +1,16 @@
|
|
1
|
+
require 'sinatra'
|
2
|
+
require 'yajl/json_gem'
|
3
|
+
|
1
4
|
%w(game map node).each do |file|
|
2
5
|
require File.expand_path( File.dirname( __FILE__ ) ) + "/ai/#{file}"
|
3
6
|
end
|
4
7
|
|
8
|
+
# Tell Sinatra to use ai file as root file
|
9
|
+
set :app_file, $0
|
10
|
+
|
5
11
|
post '/' do
|
6
12
|
begin
|
13
|
+
# Check if it's one of the four Berlin keywords
|
7
14
|
if ['ping', 'turn', 'game_start', 'game_over'].include? params[:action]
|
8
15
|
game = Berlin::AI::Game.create_or_update params[:action], params[:infos], params[:map], params[:state]
|
9
16
|
|
data/lib/version.rb
CHANGED
data/test/test_ai.rb
CHANGED
@@ -1,20 +1,14 @@
|
|
1
1
|
require 'rubygems'
|
2
|
-
require 'sinatra'
|
3
|
-
require 'yajl/json_gem'
|
4
2
|
require 'berlin-ai'
|
5
3
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
:number_of_soldiers => rand(node.number_of_soldiers + 1)
|
15
|
-
}
|
16
|
-
end
|
17
|
-
end
|
4
|
+
class Berlin::AI::Game
|
5
|
+
def turn_moves
|
6
|
+
@map.controlled_nodes.map do |node|
|
7
|
+
{
|
8
|
+
:from => node.id,
|
9
|
+
:to => node.adjacent_nodes.sample.id,
|
10
|
+
:number_of_soldiers => rand(node.number_of_soldiers + 1)
|
11
|
+
}
|
18
12
|
end
|
19
13
|
end
|
20
14
|
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: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 6
|
10
|
+
version: 0.0.6
|
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-20 00:00:00 -04:00
|
21
21
|
default_executable:
|
22
22
|
dependencies:
|
23
23
|
- !ruby/object:Gem::Dependency
|
@@ -28,10 +28,12 @@ dependencies:
|
|
28
28
|
requirements:
|
29
29
|
- - ">="
|
30
30
|
- !ruby/object:Gem::Version
|
31
|
-
hash:
|
31
|
+
hash: 19
|
32
32
|
segments:
|
33
|
-
-
|
34
|
-
|
33
|
+
- 1
|
34
|
+
- 2
|
35
|
+
- 6
|
36
|
+
version: 1.2.6
|
35
37
|
type: :runtime
|
36
38
|
version_requirements: *id001
|
37
39
|
- !ruby/object:Gem::Dependency
|
@@ -42,10 +44,12 @@ dependencies:
|
|
42
44
|
requirements:
|
43
45
|
- - ">="
|
44
46
|
- !ruby/object:Gem::Version
|
45
|
-
hash:
|
47
|
+
hash: 59
|
46
48
|
segments:
|
47
49
|
- 0
|
48
|
-
|
50
|
+
- 8
|
51
|
+
- 2
|
52
|
+
version: 0.8.2
|
49
53
|
type: :runtime
|
50
54
|
version_requirements: *id002
|
51
55
|
description: Berlin Artificial Intelligence
|