codenjoy-client 0.1.006 → 0.1.007
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/console +1 -0
- data/exe/codenjoy-ruby-client +20 -7
- data/lib/codenjoy/base_board.rb +61 -0
- data/lib/codenjoy/client/version.rb +1 -1
- data/lib/codenjoy/games/battlecity/board.rb +5 -59
- data/lib/codenjoy/games/icancode/board.rb +223 -0
- data/lib/codenjoy/games/loderunner/board.rb +190 -0
- data/lib/codenjoy/games/snake/board.rb +89 -0
- data/lib/codenjoy/games/tetris/board.rb +29 -50
- data/lib/codenjoy/utils.rb +27 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a4294e66ffc1a27e53301dd0f5e35988cdb569a6f6302251221d40bdcdbb2095
|
4
|
+
data.tar.gz: d0764a9de7c1fae7fcd4aa6c60a53a18cd919a6f92fd87edebc8b91e642193c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f236bd121dfe6c3b75411f39e665a71ff0ab6bad931e480958bd01122913829220bbc4008e5b2b08b7e302e28223152b09a0f2d36a758828992a799d88477d2
|
7
|
+
data.tar.gz: 5d009ade6891ac661abc53e086318106e1c28b0ac92f670408cd4d03eea336563e19a6cbf5dbba24bc993073313067e8b611f723265f94e8c6cdfa85c61743af
|
data/bin/console
CHANGED
data/exe/codenjoy-ruby-client
CHANGED
@@ -1,16 +1,29 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
|
4
|
-
require
|
3
|
+
require 'fileutils'
|
4
|
+
require 'codenjoy/client'
|
5
5
|
|
6
6
|
game = Codenjoy::Client::Game::new
|
7
7
|
sourse = "#{game.source_path}/game_base_client.rb"
|
8
8
|
dest = "#{Dir.pwd}/game_base_client.rb"
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
puts "codenjoy-ruby-client util for develop ruby client for https://dojorena.io/"
|
11
|
+
puts "--game common cases"
|
12
|
+
puts "--dev for dev mode"
|
13
|
+
|
14
|
+
# def add_dev_line
|
15
|
+
# end
|
16
|
+
|
17
|
+
if ['--dev', '--game'].include?(ARGV[0])
|
18
|
+
p "#{sourse} ==> #{dest}"
|
19
|
+
if File.exist?(dest)
|
20
|
+
p "File already exist"
|
21
|
+
else
|
22
|
+
FileUtils.cp(sourse, dest)
|
23
|
+
if '--dev' == ARGV[0]
|
24
|
+
text = "require 'bundler/setup'\n#{File.read(dest)}"
|
25
|
+
File.open(dest, "w") { |file| file.write(text) }
|
26
|
+
end
|
27
|
+
end
|
15
28
|
end
|
16
29
|
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require "codenjoy/utils"
|
2
|
+
|
3
|
+
class BaseBoard
|
4
|
+
def xyl
|
5
|
+
@xyl ||= LengthToXY.new(size);
|
6
|
+
end
|
7
|
+
|
8
|
+
def size
|
9
|
+
@size ||= Math.sqrt(@raw.length);
|
10
|
+
end
|
11
|
+
|
12
|
+
def board_to_s
|
13
|
+
Array.new(size).each_with_index.map{ |e, n| @raw[(n * size)..((n + 1) * size - 1)]}.join("\n")
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_at(x, y)
|
17
|
+
return false if Point.new(x, y).out_of?(size)
|
18
|
+
@raw[xyl.getLength(x, y)]
|
19
|
+
end
|
20
|
+
|
21
|
+
def any_of_at?(x, y, elements = [])
|
22
|
+
return false if Point.new(x, y).out_of?(size)
|
23
|
+
elements.each do |e|
|
24
|
+
return true if at?(x, y, e)
|
25
|
+
end
|
26
|
+
false;
|
27
|
+
end
|
28
|
+
|
29
|
+
def near?(x, y, element)
|
30
|
+
return false if Point.new(x, y).out_of?(size)
|
31
|
+
at?(x + 1, y, element) || at?(x - 1, y, element) || at?(x, y + 1, element) || at?(x, y - 1, element)
|
32
|
+
end
|
33
|
+
|
34
|
+
def at?(x, y, element)
|
35
|
+
return false if Point.new(x, y).out_of?(size)
|
36
|
+
get_at(x, y) == element;
|
37
|
+
end
|
38
|
+
|
39
|
+
def find_all(element)
|
40
|
+
result = []
|
41
|
+
@raw.length.times do |i|
|
42
|
+
point = xyl.getXY(i);
|
43
|
+
result.push(point) if at?(point.x, point.y, element)
|
44
|
+
end
|
45
|
+
result;
|
46
|
+
end
|
47
|
+
|
48
|
+
def find_by_list(list)
|
49
|
+
list.map{ |e| find_all(e) }.flatten.map{ |e| [e.x, e.y] }
|
50
|
+
end
|
51
|
+
|
52
|
+
def count_near(x, y, element)
|
53
|
+
return 0 if Point.new(x, y).out_of?(size)
|
54
|
+
count = 0
|
55
|
+
count += 1 if at?(x - 1, y , element)
|
56
|
+
count += 1 if at?(x + 1, y , element)
|
57
|
+
count += 1 if at?(x , y - 1, element)
|
58
|
+
count += 1 if at?(x , y + 1, element)
|
59
|
+
count
|
60
|
+
end
|
61
|
+
end
|
@@ -21,34 +21,7 @@
|
|
21
21
|
###
|
22
22
|
|
23
23
|
require "codenjoy/utils"
|
24
|
-
require
|
25
|
-
|
26
|
-
class LengthToXY
|
27
|
-
def initialize(board_size)
|
28
|
-
@board_size = board_size
|
29
|
-
end
|
30
|
-
|
31
|
-
def inversionY(y)
|
32
|
-
@board_size - 1 - y;
|
33
|
-
end
|
34
|
-
|
35
|
-
def inversionX(x)
|
36
|
-
x;
|
37
|
-
end
|
38
|
-
|
39
|
-
def getXY(length)
|
40
|
-
return nil if (length == -1)
|
41
|
-
x = inversionX(length % @board_size);
|
42
|
-
y = inversionY((length / @board_size).floor);
|
43
|
-
return Point.new(x, y);
|
44
|
-
end
|
45
|
-
|
46
|
-
def getLength(x, y)
|
47
|
-
xx = inversionX(x);
|
48
|
-
yy = inversionY(y);
|
49
|
-
yy * @board_size + xx;
|
50
|
-
end
|
51
|
-
end
|
24
|
+
require "codenjoy/base_board"
|
52
25
|
|
53
26
|
module Codenjoy
|
54
27
|
module Client
|
@@ -59,7 +32,7 @@ module Codenjoy
|
|
59
32
|
end
|
60
33
|
end
|
61
34
|
|
62
|
-
class Codenjoy::Client::Games::Battlecity::Board
|
35
|
+
class Codenjoy::Client::Games::Battlecity::Board < BaseBoard
|
63
36
|
|
64
37
|
ELEMENTS = {
|
65
38
|
NONE: ' ',
|
@@ -147,33 +120,10 @@ class Codenjoy::Client::Games::Battlecity::Board
|
|
147
120
|
@raw = data
|
148
121
|
end
|
149
122
|
|
150
|
-
def size
|
151
|
-
@size ||= Math.sqrt(@raw.length);
|
152
|
-
end
|
153
|
-
|
154
123
|
def xyl
|
155
124
|
@xyl ||= LengthToXY.new(size);
|
156
125
|
end
|
157
126
|
|
158
|
-
def getAt(x, y)
|
159
|
-
return false if Point.new(x, y).out_of?(size)
|
160
|
-
@raw[xyl.getLength(x, y)];
|
161
|
-
end
|
162
|
-
|
163
|
-
def at?(x, y, element)
|
164
|
-
return false if Point.new(x, y).out_of?(size)
|
165
|
-
getAt(x, y) == element;
|
166
|
-
end
|
167
|
-
|
168
|
-
def findAll(element)
|
169
|
-
result = []
|
170
|
-
@raw.length.times do |i|
|
171
|
-
point = xyl.getXY(i);
|
172
|
-
result.push(point) if at?(point.x, point.y, element)
|
173
|
-
end
|
174
|
-
result;
|
175
|
-
end
|
176
|
-
|
177
127
|
def get_me
|
178
128
|
me = find_by_list(TANK)
|
179
129
|
return nil if me.nil?
|
@@ -181,7 +131,7 @@ class Codenjoy::Client::Games::Battlecity::Board
|
|
181
131
|
end
|
182
132
|
|
183
133
|
def find_by_list(list)
|
184
|
-
result = list.map{ |e|
|
134
|
+
result = list.map{ |e| find_all(e) }.flatten.map{ |e| [e.x, e.y] }
|
185
135
|
return nil if (result.length == 0)
|
186
136
|
result
|
187
137
|
end
|
@@ -200,7 +150,7 @@ class Codenjoy::Client::Games::Battlecity::Board
|
|
200
150
|
(-1..1).each do |dx|
|
201
151
|
(-1..1).each do |dy|
|
202
152
|
next if (dx == 0 && dy == 0)
|
203
|
-
result.push(
|
153
|
+
result.push(get_at(x + dx, y + dy))
|
204
154
|
end
|
205
155
|
end
|
206
156
|
result;
|
@@ -223,7 +173,7 @@ class Codenjoy::Client::Games::Battlecity::Board
|
|
223
173
|
|
224
174
|
def bullet_at?(x, y)
|
225
175
|
return false if Point.new(x, y).out_of?(size)
|
226
|
-
|
176
|
+
get_at(x, y) == ELEMENTS[:BULLET]
|
227
177
|
end
|
228
178
|
|
229
179
|
def any_of_at?(x, y, elements = [])
|
@@ -238,10 +188,6 @@ class Codenjoy::Client::Games::Battlecity::Board
|
|
238
188
|
get_me.nil?;
|
239
189
|
end
|
240
190
|
|
241
|
-
def board_to_s
|
242
|
-
Array.new(size).each_with_index.map{ |e, n| @raw[(n * size)..((n + 1) * size - 1)]}.join("\n")
|
243
|
-
end
|
244
|
-
|
245
191
|
def get_barriers
|
246
192
|
find_by_list(BARRIERS)
|
247
193
|
end
|
@@ -0,0 +1,223 @@
|
|
1
|
+
###
|
2
|
+
# #%L
|
3
|
+
# Codenjoy - it's a dojo-like platform from developers to developers.
|
4
|
+
# %%
|
5
|
+
# Copyright (C) 2018 Codenjoy
|
6
|
+
# %%
|
7
|
+
# This program is free software: you can redistribute it and/or modify
|
8
|
+
# it under the terms of the GNU General Public License as
|
9
|
+
# published by the Free Software Foundation, either version 3 of the
|
10
|
+
# License, or (at your option) any later version.
|
11
|
+
#
|
12
|
+
# This program is distributed in the hope that it will be useful,
|
13
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
+
# GNU General Public License for more details.
|
16
|
+
#
|
17
|
+
# You should have received a copy of the GNU General Public
|
18
|
+
# License along with this program. If not, see
|
19
|
+
# <http://www.gnu.org/licenses/gpl-3.0.html>.
|
20
|
+
# #L%
|
21
|
+
###
|
22
|
+
|
23
|
+
require "codenjoy/utils"
|
24
|
+
require "codenjoy/base_board"
|
25
|
+
|
26
|
+
module Codenjoy
|
27
|
+
module Client
|
28
|
+
module Games
|
29
|
+
module Icancode
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class Layer < BaseBoard
|
36
|
+
def initialize(data)
|
37
|
+
@raw = data
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Codenjoy::Client::Games::Icancode::Board
|
42
|
+
|
43
|
+
ELEMENT = {
|
44
|
+
EMPTY: '-',
|
45
|
+
FLOOR: '.',
|
46
|
+
|
47
|
+
ANGLE_IN_LEFT: '╔',
|
48
|
+
WALL_FRONT: '═',
|
49
|
+
ANGLE_IN_RIGHT: '┐',
|
50
|
+
WALL_RIGHT: '│',
|
51
|
+
ANGLE_BACK_RIGHT: '┘',
|
52
|
+
WALL_BACK: '─',
|
53
|
+
ANGLE_BACK_LEFT: '└',
|
54
|
+
WALL_LEFT: '║',
|
55
|
+
WALL_BACK_ANGLE_LEFT: '┌',
|
56
|
+
WALL_BACK_ANGLE_RIGHT: '╗',
|
57
|
+
ANGLE_OUT_RIGHT: '╝',
|
58
|
+
ANGLE_OUT_LEFT: '╚',
|
59
|
+
SPACE: ' ',
|
60
|
+
|
61
|
+
LASER_MACHINE_CHARGING_LEFT: '˂',
|
62
|
+
LASER_MACHINE_CHARGING_RIGHT: '˃',
|
63
|
+
LASER_MACHINE_CHARGING_UP: '˄',
|
64
|
+
LASER_MACHINE_CHARGING_DOWN: '˅',
|
65
|
+
|
66
|
+
LASER_MACHINE_READY_LEFT: '◄',
|
67
|
+
LASER_MACHINE_READY_RIGHT: '►',
|
68
|
+
LASER_MACHINE_READY_UP: '▲',
|
69
|
+
LASER_MACHINE_READY_DOWN: '▼',
|
70
|
+
|
71
|
+
START: 'S',
|
72
|
+
EXIT: 'E',
|
73
|
+
HOLE: 'O',
|
74
|
+
BOX: 'B',
|
75
|
+
ZOMBIE_START: 'Z',
|
76
|
+
GOLD: '$',
|
77
|
+
|
78
|
+
ROBOT: '☺',
|
79
|
+
ROBOT_FALLING: 'o',
|
80
|
+
ROBOT_FLYING: '*',
|
81
|
+
ROBOT_LASER: '☻',
|
82
|
+
|
83
|
+
ROBOT_OTHER: 'X',
|
84
|
+
ROBOT_OTHER_FALLING: 'x',
|
85
|
+
ROBOT_OTHER_FLYING: '^',
|
86
|
+
ROBOT_OTHER_LASER: '&',
|
87
|
+
|
88
|
+
LASER_LEFT: '←',
|
89
|
+
LASER_RIGHT: '→',
|
90
|
+
LASER_UP: '↑',
|
91
|
+
LASER_DOWN: '↓',
|
92
|
+
|
93
|
+
FEMALE_ZOMBIE: '♀',
|
94
|
+
MALE_ZOMBIE: '♂',
|
95
|
+
ZOMBIE_DIE: '✝'
|
96
|
+
}
|
97
|
+
|
98
|
+
WALLS = [
|
99
|
+
:ANGLE_IN_LEFT, :WALL_FRONT, :ANGLE_IN_RIGHT, :WALL_RIGHT, :ANGLE_BACK_RIGHT,
|
100
|
+
:WALL_BACK, :ANGLE_BACK_LEFT, :WALL_LEFT, :WALL_BACK_ANGLE_LEFT, :WALL_BACK_ANGLE_RIGHT,
|
101
|
+
:ANGLE_OUT_RIGHT, :ANGLE_OUT_LEFT, :SPACE
|
102
|
+
]
|
103
|
+
|
104
|
+
attr_accessor :l1, :l2, :l3 # short names for layers
|
105
|
+
|
106
|
+
def command
|
107
|
+
# 'up' // you can move
|
108
|
+
# 'down'
|
109
|
+
# 'left'
|
110
|
+
# 'right'
|
111
|
+
# 'act(1)' // jump
|
112
|
+
# 'act(2)' // pull box
|
113
|
+
# 'act(3)' // fire
|
114
|
+
# 'act(0)' // die
|
115
|
+
end
|
116
|
+
|
117
|
+
def process(data)
|
118
|
+
@data = JSON.parse(data)
|
119
|
+
@raw = @data["layers"][0]
|
120
|
+
@l1 = Layer.new(@data["layers"][0])
|
121
|
+
@l2 = Layer.new(@data["layers"][1])
|
122
|
+
@l3 = Layer.new(@data["layers"][2])
|
123
|
+
end
|
124
|
+
|
125
|
+
def nline(i)
|
126
|
+
return " 0" if 0 == i
|
127
|
+
format("%02.0d", i)
|
128
|
+
end
|
129
|
+
|
130
|
+
def numberinline(length)
|
131
|
+
Array.new(length) { |i| (i % 10).to_s }.join
|
132
|
+
end
|
133
|
+
|
134
|
+
def headfoot
|
135
|
+
Array.new(3).map{ |i| " " + numberinline(size) }.join
|
136
|
+
end
|
137
|
+
|
138
|
+
def headlayers
|
139
|
+
spaces = ([' '] * (size - 6)).join()
|
140
|
+
@data["layers"].each_with_index.map{ |e, n| " Layers#{n + 1}#{spaces}" }.join('')
|
141
|
+
end
|
142
|
+
|
143
|
+
def get_me
|
144
|
+
[@data['heroPosition']['x'], @data['heroPosition']['y']]
|
145
|
+
end
|
146
|
+
|
147
|
+
def get_other_heroes
|
148
|
+
l2.find_by_list([
|
149
|
+
:ROBOT_OTHER, :ROBOT_OTHER_FALLING, :ROBOT_OTHER_LASER
|
150
|
+
].map{ |e| ELEMENT[e] }) + l3.find_by_list([ELEMENT[:ROBOT_OTHER_FLYING]])
|
151
|
+
end
|
152
|
+
|
153
|
+
def get_gold
|
154
|
+
l1.find_by_list([ELEMENT[:GOLD]])
|
155
|
+
end
|
156
|
+
|
157
|
+
def get_starts
|
158
|
+
l1.find_by_list([ELEMENT[:START]])
|
159
|
+
end
|
160
|
+
|
161
|
+
def get_exits
|
162
|
+
l1.find_by_list([ELEMENT[:EXIT]])
|
163
|
+
end
|
164
|
+
|
165
|
+
def get_boxes
|
166
|
+
l2.find_by_list([ELEMENT[:BOX]])
|
167
|
+
end
|
168
|
+
|
169
|
+
def get_holes
|
170
|
+
l1.find_by_list([ELEMENT[:HOLE]])
|
171
|
+
end
|
172
|
+
|
173
|
+
def get_laser_machines
|
174
|
+
l1.find_by_list([:LASER_MACHINE_CHARGING_LEFT, :LASER_MACHINE_CHARGING_RIGHT,
|
175
|
+
:LASER_MACHINE_CHARGING_UP, :LASER_MACHINE_CHARGING_DOWN,
|
176
|
+
:LASER_MACHINE_READY_LEFT, :LASER_MACHINE_READY_RIGHT,
|
177
|
+
:LASER_MACHINE_READY_UP, :LASER_MACHINE_READY_DOWN].map{ |e| ELEMENT[e] })
|
178
|
+
end
|
179
|
+
|
180
|
+
def get_lasers
|
181
|
+
l2.find_by_list([:LASER_LEFT, :LASER_RIGHT, :LASER_UP, :LASER_DOWN].map{ |e| ELEMENT[e] })
|
182
|
+
end
|
183
|
+
|
184
|
+
def get_zombies
|
185
|
+
l2.find_by_list([:FEMALE_ZOMBIE, :MALE_ZOMBIE, :ZOMBIE_DIE].map{ |e| ELEMENT[e] })
|
186
|
+
end
|
187
|
+
|
188
|
+
def get_walls
|
189
|
+
l1.find_by_list(WALLS.map{ |e| ELEMENT[e] })
|
190
|
+
end
|
191
|
+
|
192
|
+
def wall_at?(x, y)
|
193
|
+
WALLS.map{ |e| ELEMENT[e] }.include?(l1.get_at(x,y))
|
194
|
+
end
|
195
|
+
|
196
|
+
def infoline(n)
|
197
|
+
res = [
|
198
|
+
" Robots: #{get_me}, #{get_other_heroes}",
|
199
|
+
" Gold: #{get_gold}",
|
200
|
+
" Starts: #{get_starts}",
|
201
|
+
" Exits: #{get_exits}",
|
202
|
+
" Boxes: #{get_boxes}",
|
203
|
+
" Holes: #{get_holes}",
|
204
|
+
" LaserMachine: #{get_laser_machines}",
|
205
|
+
" Lasers: #{get_lasers}",
|
206
|
+
" Zombies: #{get_zombies}"
|
207
|
+
]
|
208
|
+
d = 1
|
209
|
+
return res[n - d] if d <= n && n < res.size + d
|
210
|
+
""
|
211
|
+
end
|
212
|
+
|
213
|
+
def board_to_s
|
214
|
+
([headlayers, headfoot] +
|
215
|
+
Array.new(size).each_with_index.map do |e, n|
|
216
|
+
@data["layers"].map{ |l| nline(size - n - 1) + l[(n * size)..((n + 1) * size - 1)] }.join + infoline(n)
|
217
|
+
end + [headfoot]).join("\n")
|
218
|
+
end
|
219
|
+
|
220
|
+
def to_s
|
221
|
+
board_to_s
|
222
|
+
end
|
223
|
+
end
|
@@ -0,0 +1,190 @@
|
|
1
|
+
###
|
2
|
+
# #%L
|
3
|
+
# Codenjoy - it's a dojo-like platform from developers to developers.
|
4
|
+
# %%
|
5
|
+
# Copyright (C) 2018 Codenjoy
|
6
|
+
# %%
|
7
|
+
# This program is free software: you can redistribute it and/or modify
|
8
|
+
# it under the terms of the GNU General Public License as
|
9
|
+
# published by the Free Software Foundation, either version 3 of the
|
10
|
+
# License, or (at your option) any later version.
|
11
|
+
#
|
12
|
+
# This program is distributed in the hope that it will be useful,
|
13
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
+
# GNU General Public License for more details.
|
16
|
+
#
|
17
|
+
# You should have received a copy of the GNU General Public
|
18
|
+
# License along with this program. If not, see
|
19
|
+
# <http://www.gnu.org/licenses/gpl-3.0.html>.
|
20
|
+
# #L%
|
21
|
+
###
|
22
|
+
|
23
|
+
require "codenjoy/utils"
|
24
|
+
require "codenjoy/base_board"
|
25
|
+
|
26
|
+
module Codenjoy
|
27
|
+
module Client
|
28
|
+
module Games
|
29
|
+
module Loderunner
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class Codenjoy::Client::Games::Loderunner::Board < BaseBoard
|
36
|
+
|
37
|
+
ELEMENTS = {
|
38
|
+
# a void
|
39
|
+
NONE: ' ',
|
40
|
+
|
41
|
+
# walls
|
42
|
+
BRICK: '#',
|
43
|
+
PIT_FILL_1: '1',
|
44
|
+
PIT_FILL_2: '2',
|
45
|
+
PIT_FILL_3: '3',
|
46
|
+
PIT_FILL_4: '4',
|
47
|
+
UNDESTROYABLE_WALL: '☼',
|
48
|
+
|
49
|
+
DRILL_PIT: '*',
|
50
|
+
|
51
|
+
# this is enemy
|
52
|
+
ENEMY_LADDER: 'Q',
|
53
|
+
ENEMY_LEFT: '«',
|
54
|
+
ENEMY_RIGHT: '»',
|
55
|
+
ENEMY_PIPE_LEFT: '<',
|
56
|
+
ENEMY_PIPE_RIGHT: '>',
|
57
|
+
ENEMY_PIT: 'X',
|
58
|
+
|
59
|
+
# gold ;)
|
60
|
+
GOLD: '$',
|
61
|
+
|
62
|
+
# this is you
|
63
|
+
HERO_DIE: 'Ѡ',
|
64
|
+
HERO_DRILL_LEFT: 'Я',
|
65
|
+
HERO_DRILL_RIGHT: 'R',
|
66
|
+
HERO_LADDER: 'Y',
|
67
|
+
HERO_LEFT: '◄',
|
68
|
+
HERO_RIGHT: '►',
|
69
|
+
HERO_FALL_LEFT: ']',
|
70
|
+
HERO_FALL_RIGHT: '[',
|
71
|
+
HERO_PIPE_LEFT: '{',
|
72
|
+
HERO_PIPE_RIGHT: '}',
|
73
|
+
|
74
|
+
# this is other players
|
75
|
+
OTHER_HERO_DIE: 'Z',
|
76
|
+
OTHER_HERO_LEFT: ')',
|
77
|
+
OTHER_HERO_RIGHT: '(',
|
78
|
+
OTHER_HERO_LADDER: 'U',
|
79
|
+
OTHER_HERO_PIPE_LEFT: 'Э',
|
80
|
+
OTHER_HERO_PIPE_RIGHT: 'Є',
|
81
|
+
|
82
|
+
# ladder and pipe - you can walk
|
83
|
+
LADDER: 'H',
|
84
|
+
PIPE: '~'
|
85
|
+
};
|
86
|
+
|
87
|
+
HERO = [
|
88
|
+
:HERO_DIE, :HERO_DRILL_LEFT, :HERO_DRILL_RIGHT,
|
89
|
+
:HERO_FALL_RIGHT, :HERO_FALL_LEFT, :HERO_LADDER,
|
90
|
+
:HERO_LEFT, :HERO_RIGHT, :HERO_PIPE_LEFT, :HERO_PIPE_RIGHT
|
91
|
+
]
|
92
|
+
|
93
|
+
OTHER_HERO = [
|
94
|
+
:OTHER_HERO_LEFT, :OTHER_HERO_RIGHT, :OTHER_HERO_LADDER, :OTHER_HERO_PIPE_LEFT, :OTHER_HERO_PIPE_RIGHT
|
95
|
+
]
|
96
|
+
|
97
|
+
ENEMY = [
|
98
|
+
:ENEMY_LADDER, :ENEMY_LADDER, :ENEMY_LEFT, :ENEMY_PIPE_LEFT, :ENEMY_PIPE_RIGHT, :ENEMY_RIGHT, :ENEMY_PIT
|
99
|
+
]
|
100
|
+
|
101
|
+
PIPE = [
|
102
|
+
:PIPE, :HERO_PIPE_LEFT, :HERO_PIPE_RIGHT, :OTHER_HERO_PIPE_LEFT, :OTHER_HERO_PIPE_RIGHT
|
103
|
+
]
|
104
|
+
|
105
|
+
def process(data)
|
106
|
+
@raw = data
|
107
|
+
end
|
108
|
+
|
109
|
+
def to_s
|
110
|
+
[
|
111
|
+
"Board:",
|
112
|
+
board_to_s,
|
113
|
+
"Me at: #{get_me}",
|
114
|
+
"Other heroes at: #{get_other_heroes}",
|
115
|
+
"Enemies at: #{get_enemies}",
|
116
|
+
"Gold at: #{get_gold}"
|
117
|
+
].join("\n")
|
118
|
+
end
|
119
|
+
|
120
|
+
def get_me
|
121
|
+
find_by_list(HERO.map{ |e| ELEMENTS[e] })
|
122
|
+
end
|
123
|
+
|
124
|
+
def get_other_heroes
|
125
|
+
find_by_list(OTHER_HERO.map{ |e| ELEMENTS[e] })
|
126
|
+
end
|
127
|
+
|
128
|
+
def get_enemies
|
129
|
+
find_by_list(ENEMY.map{ |e| ELEMENTS[e] })
|
130
|
+
end
|
131
|
+
|
132
|
+
def get_gold
|
133
|
+
find_by_list([ELEMENTS[:GOLD]])
|
134
|
+
end
|
135
|
+
|
136
|
+
def get_walls
|
137
|
+
find_by_list([ELEMENTS[:BRICK], ELEMENTS[:UNDESTROYABLE_WALL]])
|
138
|
+
end
|
139
|
+
|
140
|
+
def get_ladders
|
141
|
+
find_by_list([ELEMENTS[:LADDER], ELEMENTS[:HERO_LADDER], ELEMENTS[:ENEMY_LADDER]])
|
142
|
+
end
|
143
|
+
|
144
|
+
def get_pipes
|
145
|
+
find_by_list(PIPE.map{ |e| ELEMENTS[e] })
|
146
|
+
end
|
147
|
+
|
148
|
+
def game_over?
|
149
|
+
!@raw.index(ELEMENTS[:HERO_DIE]).nil?
|
150
|
+
end
|
151
|
+
|
152
|
+
def get_barriers
|
153
|
+
[get_enemies, get_other_heroes, get_walls].flatten(1)
|
154
|
+
end
|
155
|
+
|
156
|
+
def barrier_at?(x, y)
|
157
|
+
return false if Point.new(x, y).out_of?(size)
|
158
|
+
!get_barriers.index([x, y]).nil?
|
159
|
+
end
|
160
|
+
|
161
|
+
def enemy_at?(x, y)
|
162
|
+
return false if Point.new(x, y).out_of?(size)
|
163
|
+
any_of_at?(x, y, ENEMY.map{ |e| ELEMENTS[e] })
|
164
|
+
end
|
165
|
+
|
166
|
+
def other_hero_at?(x, y)
|
167
|
+
return false if Point.new(x, y).out_of?(size)
|
168
|
+
any_of_at?(x, y, OTHER_HERO.map{ |e| ELEMENTS[e] })
|
169
|
+
end
|
170
|
+
|
171
|
+
def wall_at?(x, y)
|
172
|
+
return false if Point.new(x, y).out_of?(size)
|
173
|
+
any_of_at?(x, y, [ELEMENTS[:BRICK], ELEMENTS[:UNDESTROYABLE_WALL]])
|
174
|
+
end
|
175
|
+
|
176
|
+
def ladder_at?(x, y)
|
177
|
+
return false if Point.new(x, y).out_of?(size)
|
178
|
+
any_of_at?(x, y, [ELEMENTS[:LADDER], ELEMENTS[:HERO_LADDER], ELEMENTS[:ENEMY_LADDER]])
|
179
|
+
end
|
180
|
+
|
181
|
+
def gold_at?(x, y)
|
182
|
+
return false if Point.new(x, y).out_of?(size)
|
183
|
+
at?(x, y, ELEMENTS[:GOLD])
|
184
|
+
end
|
185
|
+
|
186
|
+
def pipe_at?(x, y)
|
187
|
+
return false if Point.new(x, y).out_of?(size)
|
188
|
+
any_of_at?(x, y, PIPE.map{ |e| ELEMENTS[e] })
|
189
|
+
end
|
190
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require "codenjoy/utils"
|
2
|
+
require "codenjoy/base_board"
|
3
|
+
|
4
|
+
module Codenjoy
|
5
|
+
module Client
|
6
|
+
module Games
|
7
|
+
module Snake
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Codenjoy::Client::Games::Snake::Board < BaseBoard
|
14
|
+
ELEMENTS = {
|
15
|
+
BAD_APPLE: '☻',
|
16
|
+
GOOD_APPLE: '☺',
|
17
|
+
|
18
|
+
BREAK: '☼',
|
19
|
+
|
20
|
+
HEAD_DOWN: '▼',
|
21
|
+
HEAD_LEFT: '◄',
|
22
|
+
HEAD_RIGHT: '►',
|
23
|
+
HEAD_UP: '▲',
|
24
|
+
|
25
|
+
TAIL_END_DOWN: '╙',
|
26
|
+
TAIL_END_LEFT: '╘',
|
27
|
+
TAIL_END_UP: '╓',
|
28
|
+
TAIL_END_RIGHT: '╕',
|
29
|
+
TAIL_HORIZONTAL: '═',
|
30
|
+
TAIL_VERTICAL: '║',
|
31
|
+
TAIL_LEFT_DOWN: '╗',
|
32
|
+
TAIL_LEFT_UP: '╝',
|
33
|
+
TAIL_RIGHT_DOWN: '╔',
|
34
|
+
TAIL_RIGHT_UP: '╚',
|
35
|
+
|
36
|
+
NONE: ' '
|
37
|
+
}
|
38
|
+
|
39
|
+
def process(str)
|
40
|
+
puts "-------------------------------------------------------------------------------------------"
|
41
|
+
puts str
|
42
|
+
@raw = str
|
43
|
+
end
|
44
|
+
|
45
|
+
def get_my_head
|
46
|
+
find_by_list([:HEAD_DOWN, :HEAD_UP, :HEAD_RIGHT, :HEAD_LEFT].map{ |e| ELEMENTS[e] })
|
47
|
+
end
|
48
|
+
|
49
|
+
def get_my_body
|
50
|
+
find_by_list([
|
51
|
+
:HEAD_DOWN, :HEAD_LEFT, :HEAD_RIGHT, :HEAD_UP,
|
52
|
+
:TAIL_END_DOWN, :TAIL_END_LEFT, :TAIL_END_UP,
|
53
|
+
:TAIL_END_RIGHT, :TAIL_HORIZONTAL, :TAIL_VERTICAL,
|
54
|
+
:TAIL_LEFT_DOWN, :TAIL_LEFT_UP, :TAIL_RIGHT_DOWN, :TAIL_RIGHT_UP
|
55
|
+
].map{ |e| ELEMENTS[e] })
|
56
|
+
end
|
57
|
+
|
58
|
+
def get_apple
|
59
|
+
find_by_list([ELEMENTS[:GOOD_APPLE]])
|
60
|
+
end
|
61
|
+
|
62
|
+
def get_stone
|
63
|
+
find_by_list([ELEMENTS[:BAD_APPLE]])
|
64
|
+
end
|
65
|
+
|
66
|
+
def get_walls
|
67
|
+
find_by_list([ELEMENTS[:BREAK]])
|
68
|
+
end
|
69
|
+
|
70
|
+
def get_barriers
|
71
|
+
find_by_list([ELEMENTS[:BREAK], ELEMENTS[:BAD_APPLE]])
|
72
|
+
end
|
73
|
+
|
74
|
+
def barrier_at?(x, y)
|
75
|
+
return false if Point.new(x, y).out_of?(size)
|
76
|
+
get_barriers.include?([x.to_f, y.to_f]);
|
77
|
+
end
|
78
|
+
|
79
|
+
def to_s
|
80
|
+
[
|
81
|
+
"Board:",
|
82
|
+
board_to_s,
|
83
|
+
"My head at: #{get_my_head}",
|
84
|
+
"My body at: #{get_my_body}",
|
85
|
+
"Apple at: #{get_apple}",
|
86
|
+
"Stone at: #{get_stone}"
|
87
|
+
].join("\n")
|
88
|
+
end
|
89
|
+
end
|
@@ -22,54 +22,6 @@
|
|
22
22
|
require "codenjoy/utils"
|
23
23
|
require 'json'
|
24
24
|
|
25
|
-
##################################### ELEMENTS TYPES #########################################################
|
26
|
-
|
27
|
-
ELEMENTS = Hash.new
|
28
|
-
|
29
|
-
# This is glass content
|
30
|
-
ELEMENTS[:I_BLUE] = 'I'
|
31
|
-
ELEMENTS[:J_CYAN] = 'J'
|
32
|
-
ELEMENTS[:L_ORANGE] = 'L'
|
33
|
-
ELEMENTS[:O_YELLOW] = 'O'
|
34
|
-
ELEMENTS[:S_GREEN] = 'S'
|
35
|
-
ELEMENTS[:T_PURPLE] = 'T'
|
36
|
-
ELEMENTS[:Z_RED] = 'Z'
|
37
|
-
ELEMENTS[:NONE] = '.'
|
38
|
-
|
39
|
-
# List of figures
|
40
|
-
FIGURES = [
|
41
|
-
ELEMENTS[:I_BLUE],
|
42
|
-
ELEMENTS[:J_CYAN],
|
43
|
-
ELEMENTS[:L_ORANGE],
|
44
|
-
ELEMENTS[:O_YELLOW],
|
45
|
-
ELEMENTS[:S_GREEN],
|
46
|
-
ELEMENTS[:T_PURPLE],
|
47
|
-
ELEMENTS[:Z_RED]
|
48
|
-
]
|
49
|
-
|
50
|
-
##################################### END OF ELEMENTS TYPES #########################################################
|
51
|
-
|
52
|
-
# Return list of indexes of char +char+ in string +s+ ("STR".index returns only first char/string appear)
|
53
|
-
#
|
54
|
-
# @param [String] s string to search in
|
55
|
-
# @param [String] char substring to search
|
56
|
-
# @return [Array] list of indexes
|
57
|
-
def indexes(s, char)
|
58
|
-
(0 ... s.length).find_all { |i| s[i,1] == char }
|
59
|
-
end
|
60
|
-
|
61
|
-
def compare(pt1, pt2)
|
62
|
-
if (pt1.x <=> pt2.x) != 0
|
63
|
-
pt1.x <=> pt2.x
|
64
|
-
else
|
65
|
-
pt1.y <=> pt2.y
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
def sort(array)
|
70
|
-
array.sort { |pt1, pt2| compare(pt1, pt2) }
|
71
|
-
end
|
72
|
-
|
73
25
|
module Codenjoy
|
74
26
|
module Client
|
75
27
|
module Games
|
@@ -80,12 +32,39 @@ module Codenjoy
|
|
80
32
|
end
|
81
33
|
|
82
34
|
class Codenjoy::Client::Games::Tetris::Board
|
35
|
+
# This is glass content
|
36
|
+
ELEMENTS = {
|
37
|
+
:I_BLUE => 'I',
|
38
|
+
:J_CYAN => 'J',
|
39
|
+
:L_ORANGE => 'L',
|
40
|
+
:O_YELLOW => 'O',
|
41
|
+
:S_GREEN => 'S',
|
42
|
+
:T_PURPLE => 'T',
|
43
|
+
:Z_RED => 'Z',
|
44
|
+
:NONE => '.',
|
45
|
+
}
|
46
|
+
|
47
|
+
FIGURES = [:I_BLUE, :J_CYAN, :L_ORANGE, :O_YELLOW, :S_GREEN, :T_PURPLE, :Z_RE]
|
48
|
+
.map{ |e| Codenjoy::Client::Games::Tetris::Board::ELEMENTS[e] }
|
49
|
+
|
83
50
|
attr_accessor :board
|
84
51
|
attr_accessor :size
|
85
52
|
attr_accessor :current_figure_type
|
86
53
|
attr_accessor :future_figures
|
87
54
|
attr_accessor :current_figure_point
|
88
55
|
|
56
|
+
def compare(pt1, pt2)
|
57
|
+
if (pt1.x <=> pt2.x) != 0
|
58
|
+
pt1.x <=> pt2.x
|
59
|
+
else
|
60
|
+
pt1.y <=> pt2.y
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def sort(array)
|
65
|
+
array.sort { |pt1, pt2| compare(pt1, pt2) }
|
66
|
+
end
|
67
|
+
|
89
68
|
def process(str)
|
90
69
|
puts "-------------------------------------------------------------------------------------------"
|
91
70
|
json = JSON.parse(str)
|
@@ -212,7 +191,7 @@ class Codenjoy::Client::Games::Tetris::Board
|
|
212
191
|
#
|
213
192
|
# @return [Array[Point]] array of walls positions
|
214
193
|
def get_free_space
|
215
|
-
get(ELEMENTS[:NONE])
|
194
|
+
get(Codenjoy::Client::Games::Tetris::Board::ELEMENTS[:NONE])
|
216
195
|
end
|
217
196
|
|
218
197
|
# How far specified element from position (strait direction)
|
@@ -238,7 +217,7 @@ class Codenjoy::Client::Games::Tetris::Board
|
|
238
217
|
)
|
239
218
|
)
|
240
219
|
|
241
|
-
return size if element == ELEMENTS[:WALL]
|
220
|
+
return size if element == Codenjoy::Client::Games::Tetris::Board::ELEMENTS[:WALL]
|
242
221
|
return distance if element == el
|
243
222
|
end
|
244
223
|
|
data/lib/codenjoy/utils.rb
CHANGED
@@ -45,3 +45,30 @@ class Point
|
|
45
45
|
x >= board_size || y >= board_size || x < 0 || y < 0;
|
46
46
|
end
|
47
47
|
end
|
48
|
+
|
49
|
+
class LengthToXY
|
50
|
+
def initialize(board_size)
|
51
|
+
@board_size = board_size
|
52
|
+
end
|
53
|
+
|
54
|
+
def inversionY(y)
|
55
|
+
@board_size - 1 - y;
|
56
|
+
end
|
57
|
+
|
58
|
+
def inversionX(x)
|
59
|
+
x;
|
60
|
+
end
|
61
|
+
|
62
|
+
def getXY(length)
|
63
|
+
return nil if (length == -1)
|
64
|
+
x = inversionX(length % @board_size);
|
65
|
+
y = inversionY((length / @board_size).floor);
|
66
|
+
return Point.new(x, y);
|
67
|
+
end
|
68
|
+
|
69
|
+
def getLength(x, y)
|
70
|
+
xx = inversionX(x);
|
71
|
+
yy = inversionY(y);
|
72
|
+
yy * @board_size + xx;
|
73
|
+
end
|
74
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: codenjoy-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.007
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- vgulaev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faye-websocket
|
@@ -58,10 +58,14 @@ files:
|
|
58
58
|
- bin/setup
|
59
59
|
- codenjoy-client.gemspec
|
60
60
|
- exe/codenjoy-ruby-client
|
61
|
+
- lib/codenjoy/base_board.rb
|
61
62
|
- lib/codenjoy/client.rb
|
62
63
|
- lib/codenjoy/client/version.rb
|
63
64
|
- lib/codenjoy/game_base_client.rb
|
64
65
|
- lib/codenjoy/games/battlecity/board.rb
|
66
|
+
- lib/codenjoy/games/icancode/board.rb
|
67
|
+
- lib/codenjoy/games/loderunner/board.rb
|
68
|
+
- lib/codenjoy/games/snake/board.rb
|
65
69
|
- lib/codenjoy/games/tetris/board.rb
|
66
70
|
- lib/codenjoy/utils.rb
|
67
71
|
homepage: https://github.com/vgulaev/codenjoy-client
|