gembots 0.0.02 → 0.0.03
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/gembots/arena.rb +16 -7
- data/lib/gembots/bot.rb +4 -3
- data/test/test_arena.rb +20 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 73f2ffdf6ffebd87c85a2327669a78eae9295ed4
|
4
|
+
data.tar.gz: a6c7d582df047435d304959ccbe1c727e09251a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2127a038e87c65d91488b74e602be071d58ece17e4e80bb1e8f26854939c93f0e415c6e2856adf7988b3434880afb94a7e62fa92d44fe4074fa47c5f97c365b
|
7
|
+
data.tar.gz: 19143de086c3a7fafd7f7578c463f78aa7271450579cb98cf3470164f99f46eb62c71e06b2afe0af837cc247a6bfef6e9538e749145aaaabd053e08b000f8968
|
data/lib/gembots/arena.rb
CHANGED
@@ -5,13 +5,16 @@ class Gembots::Arena
|
|
5
5
|
# This is a hash table containing the ids of each object.
|
6
6
|
attr_reader :objects
|
7
7
|
|
8
|
+
# Same as objects, but it contains and older copy to detect changes during a update_bot.
|
9
|
+
attr_reader :objects_pre
|
10
|
+
|
8
11
|
# 2-dimensional array of the current arena.
|
9
12
|
# By default this is a 20x20 board
|
10
13
|
attr_reader :board
|
11
14
|
|
12
15
|
def initialize *bots
|
13
|
-
@objects
|
14
|
-
@board
|
16
|
+
@objects = Hash.new
|
17
|
+
@board = Array.new(20) { Array.new(20) { [] } }
|
15
18
|
|
16
19
|
# define each bots' update function and add to players hash
|
17
20
|
bots.each do |bot|
|
@@ -19,15 +22,21 @@ class Gembots::Arena
|
|
19
22
|
@objects[bot.id] = bot
|
20
23
|
@board[bot.x_pos][bot.y_pos] << bot.id
|
21
24
|
|
22
|
-
def bot.update arena
|
23
|
-
arena.update_bot self
|
25
|
+
def bot.update arena, x_old = nil, y_old = nil
|
26
|
+
arena.update_bot self, x_old, y_old
|
24
27
|
end
|
25
28
|
end
|
26
29
|
end
|
27
30
|
|
28
|
-
# Used for
|
29
|
-
def update_bot robot
|
30
|
-
|
31
|
+
# Used for updating the board based on changes in robot.
|
32
|
+
def update_bot robot, x_old, y_old
|
33
|
+
if x_old != nil and y_old != nil
|
34
|
+
# remove id from board
|
35
|
+
@board[x_old][y_old].delete robot.id
|
36
|
+
|
37
|
+
# set new pos
|
38
|
+
@board[robot.x_pos][robot.y_pos] << robot.id
|
39
|
+
end
|
31
40
|
end
|
32
41
|
|
33
42
|
# Spawn object into board and objects array.
|
data/lib/gembots/bot.rb
CHANGED
@@ -53,6 +53,7 @@ class Gembots::Robot
|
|
53
53
|
# To move backward just use a negative value.
|
54
54
|
# Currently it only supports movement along 8 directions.
|
55
55
|
def move dist = 1
|
56
|
+
y_old, x_old = @y_pos, @x_pos
|
56
57
|
# Eventually some math using rotation_matrix will be here, in order to calculate all 360 directions.
|
57
58
|
# For now I'm only implementing 8 directions.
|
58
59
|
directions = [
|
@@ -65,10 +66,10 @@ class Gembots::Robot
|
|
65
66
|
[0, -1], # 270
|
66
67
|
[1, -1] # 315
|
67
68
|
]
|
68
|
-
@y_pos += dist * directions[360 / @angle -
|
69
|
-
@x_pos += dist * directions[360 / @angle -
|
69
|
+
@y_pos += dist * directions[360 / @angle - 2][0]
|
70
|
+
@x_pos += dist * directions[360 / @angle - 2][1]
|
70
71
|
|
71
|
-
self.update @arena
|
72
|
+
self.update @arena, x_old, y_old
|
72
73
|
end
|
73
74
|
|
74
75
|
# Rotates angle in degrees clockwise.
|
data/test/test_arena.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'gembots'
|
3
|
+
require 'gembots/arena'
|
4
|
+
require 'gembots/bot'
|
5
|
+
|
6
|
+
class ArenaTest < Test::Unit::TestCase
|
7
|
+
def test_has_bot
|
8
|
+
bot = Gembots::Robot.new
|
9
|
+
arena = Gembots::Arena.new bot
|
10
|
+
assert_equal bot, arena.objects[bot.id]
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_board_movement
|
14
|
+
bot = Gembots::Robot.new
|
15
|
+
arena = Gembots::Arena.new bot
|
16
|
+
bot.turn 90
|
17
|
+
bot.move 1
|
18
|
+
assert_equal bot.id, arena.board[1][0][0]
|
19
|
+
end
|
20
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gembots
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.03
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- L8D
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-06-
|
11
|
+
date: 2013-06-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -47,6 +47,7 @@ extra_rdoc_files: []
|
|
47
47
|
files:
|
48
48
|
- lib/gembots/arena.rb
|
49
49
|
- lib/gembots/bot.rb
|
50
|
+
- test/test_arena.rb
|
50
51
|
- test/test_bot.rb
|
51
52
|
homepage: http://github.com/L8D/gembots
|
52
53
|
licenses:
|
@@ -73,4 +74,5 @@ signing_key:
|
|
73
74
|
specification_version: 4
|
74
75
|
summary: Create your own gembots and battle them in a cli arena
|
75
76
|
test_files:
|
77
|
+
- test/test_arena.rb
|
76
78
|
- test/test_bot.rb
|