codenjoy-client 0.1.0 → 0.1.001

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e8e19231d991f1c5ddf905cef5739a34d35b04c1c4dc20b187b0c923a650daf1
4
- data.tar.gz: a3a41cc11ffa2c37b29c6e6422ca59c1bb0b7a421e7b9a27823603020652bbf7
3
+ metadata.gz: 41ec2318a826d4c2c7cce14b6f76168535d98b5e31cde82f06d16638519d7e6c
4
+ data.tar.gz: 4f055a21ea138a67c7af9d0842f0b4d8d958962e59e1f6c88e3ccca5dd5650b9
5
5
  SHA512:
6
- metadata.gz: e96087232a15768b123673f3b7bd5395afbf59cc18d2e33e81972187506da61fbacff8ecc6f42979b48f34a5704dd9d21f26ad96b5b3bcdf56fea6eae57e3740
7
- data.tar.gz: 716876da7c2341032c8bb4c07e7e8fb3405932c2283de5accc1e53a792cbb8d108579ad191b48ec83b135381d0df091cf8e91cb903f6bcf4345f1d67a5c9f404
6
+ metadata.gz: 2a862542b7161d1b263eda0cc9dde1b3db48663e6b19b41173524b4c6b0d8bf6a85810d73a11cf74aaa9b75191eef5d5d96c69a0aa39dccc4617a4d1e61e4e3a
7
+ data.tar.gz: 3b985416180cb9e3832b09eea98d0c6c1f88c18cd24b9cef1ef5cfa3728ae51f018f6a717b79908a39300407cf5cfcda6dec68541e08fde5bcdf60cfa9e76371
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ hw.rb
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- codenjoy-client (0.1.0)
4
+ codenjoy-client (0.1.001)
5
5
  faye-websocket
6
6
 
7
7
  GEM
data/game_base_client.rb CHANGED
@@ -1,14 +1,40 @@
1
1
  # require_relative "codenjoy/client"
2
2
  require "bundler/setup"
3
3
  require "codenjoy/client"
4
+ require "codenjoy/games/battlecity/board"
5
+
6
+ class YourSolver
7
+ # UP: 'up', // you can move
8
+ # DOWN: 'down',
9
+ # LEFT: 'left',
10
+ # RIGHT:'right',
11
+ # ACT: 'act', // fire
12
+ # STOP: '' // stay
13
+ def get_answer(board)
14
+
15
+ #######################################################################
16
+ #
17
+ # YOUR ALGORITHM HERE
18
+ #
19
+ #######################################################################
20
+
21
+ return 'act'
22
+ end
23
+
24
+ end
4
25
 
5
26
  game = Codenjoy::Client::Game.new
27
+ board = Codenjoy::Client::Games::Battlecity::Board.new
6
28
 
7
29
  url = "https://dojorena.io/codenjoy-contest/board/player/70xewv6o7ddy9yphm1u0?code=2603484461919438773&gameName=battlecity"
8
30
  count = 0
9
31
 
32
+
10
33
  game.play(url) do |ws, msg|
11
- p msg
34
+ json = msg[6..-1].force_encoding('UTF-8')
35
+ board.process(json)
36
+ puts board.to_s
37
+
12
38
  ws.send('Up')
13
39
  p count
14
40
  count += 1
@@ -1,5 +1,5 @@
1
1
  module Codenjoy
2
2
  module Client
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.001"
4
4
  end
5
5
  end
@@ -0,0 +1,305 @@
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 'json'
24
+
25
+ # Point class
26
+ class Point
27
+ attr_accessor :x
28
+ attr_accessor :y
29
+
30
+ # Coords (1,1) - upper left side of field
31
+ #
32
+ # @param [Integer] x X coord
33
+ # @param [Integer] y Y coord
34
+ def initialize(x, y)
35
+ @x = x
36
+ @y = y
37
+ end
38
+
39
+ # Override of compare method for Point
40
+ def == (other_object)
41
+ other_object.x == @x && other_object.y == @y
42
+ end
43
+
44
+ # For better +.inspect+ output
45
+ def to_s
46
+ "[#{@x},#{@y}]"
47
+ end
48
+
49
+ # Position of point above current
50
+ def up
51
+ Point.new(@x, @y + 1)
52
+ end
53
+
54
+ # Position of point below current
55
+ def down
56
+ Point.new(@x, @y - 1)
57
+ end
58
+
59
+ # Position of point on the left side
60
+ def left
61
+ Point.new(@x - 1, @y)
62
+ end
63
+
64
+ # Position of point on the right side
65
+ def right
66
+ Point.new(@x + 1, @y)
67
+ end
68
+
69
+ def out_of?(board_size)
70
+ x >= board_size || y >= board_size || x < 0 || y < 0;
71
+ end
72
+ end
73
+
74
+ class LengthToXY
75
+ def initialize(board_size)
76
+ @board_size = board_size
77
+ end
78
+
79
+ def inversionY(y)
80
+ @board_size - 1 - y;
81
+ end
82
+
83
+ def inversionX(x)
84
+ x;
85
+ end
86
+
87
+ def getXY(length)
88
+ return nil if (length == -1)
89
+ x = inversionX(length % @board_size);
90
+ y = inversionY((length / @board_size).floor);
91
+ return Point.new(x, y);
92
+ end
93
+
94
+ def getLength(x, y)
95
+ xx = inversionX(x);
96
+ yy = inversionY(y);
97
+ yy * @board_size + xx;
98
+ end
99
+ end
100
+
101
+ module Codenjoy
102
+ module Client
103
+ module Games
104
+ end
105
+ end
106
+ end
107
+
108
+ module Codenjoy::Client::Games::Battlecity
109
+ class Board
110
+
111
+ ELEMENTS = {
112
+ NONE: ' ',
113
+ BATTLE_WALL: '☼',
114
+ BANG: 'Ѡ',
115
+
116
+ CONSTRUCTION: '╬',
117
+
118
+ CONSTRUCTION_DESTROYED_DOWN: '╩',
119
+ CONSTRUCTION_DESTROYED_UP: '╦',
120
+ CONSTRUCTION_DESTROYED_LEFT: '╠',
121
+ CONSTRUCTION_DESTROYED_RIGHT: '╣',
122
+
123
+ CONSTRUCTION_DESTROYED_DOWN_TWICE: '╨',
124
+ CONSTRUCTION_DESTROYED_UP_TWICE: '╥',
125
+ CONSTRUCTION_DESTROYED_LEFT_TWICE: '╞',
126
+ CONSTRUCTION_DESTROYED_RIGHT_TWICE: '╡',
127
+
128
+ CONSTRUCTION_DESTROYED_LEFT_RIGHT: '│',
129
+ CONSTRUCTION_DESTROYED_UP_DOWN: '─',
130
+
131
+ CONSTRUCTION_DESTROYED_UP_LEFT: '┌',
132
+ CONSTRUCTION_DESTROYED_RIGHT_UP: '┐',
133
+ CONSTRUCTION_DESTROYED_DOWN_LEFT: '└',
134
+ CONSTRUCTION_DESTROYED_DOWN_RIGHT: '┘',
135
+
136
+ CONSTRUCTION_DESTROYED: ' ',
137
+
138
+ BULLET: '•',
139
+
140
+ TANK_UP: '▲',
141
+ TANK_RIGHT: '►',
142
+ TANK_DOWN: '▼',
143
+ TANK_LEFT: '◄',
144
+
145
+ OTHER_TANK_UP: '˄',
146
+ OTHER_TANK_RIGHT: '˃',
147
+ OTHER_TANK_DOWN: '˅',
148
+ OTHER_TANK_LEFT: '˂',
149
+
150
+ AI_TANK_UP: '?',
151
+ AI_TANK_RIGHT: '»',
152
+ AI_TANK_DOWN: '¿',
153
+ AI_TANK_LEFT: '«'
154
+ }
155
+
156
+ ENEMIES = [
157
+ ELEMENTS[:AI_TANK_UP],
158
+ ELEMENTS[:AI_TANK_DOWN],
159
+ ELEMENTS[:AI_TANK_LEFT],
160
+ ELEMENTS[:AI_TANK_RIGHT],
161
+ ELEMENTS[:OTHER_TANK_UP],
162
+ ELEMENTS[:OTHER_TANK_DOWN],
163
+ ELEMENTS[:OTHER_TANK_LEFT],
164
+ ELEMENTS[:OTHER_TANK_RIGHT]
165
+ ]
166
+
167
+ TANK = [
168
+ ELEMENTS[:TANK_UP],
169
+ ELEMENTS[:TANK_DOWN],
170
+ ELEMENTS[:TANK_LEFT],
171
+ ELEMENTS[:TANK_RIGHT]
172
+ ]
173
+
174
+ BARRIERS = [
175
+ ELEMENTS[:BATTLE_WALL],
176
+ ELEMENTS[:CONSTRUCTION],
177
+ ELEMENTS[:CONSTRUCTION_DESTROYED_DOWN],
178
+ ELEMENTS[:CONSTRUCTION_DESTROYED_UP],
179
+ ELEMENTS[:CONSTRUCTION_DESTROYED_LEFT],
180
+ ELEMENTS[:CONSTRUCTION_DESTROYED_RIGHT],
181
+ ELEMENTS[:CONSTRUCTION_DESTROYED_DOWN_TWICE],
182
+ ELEMENTS[:CONSTRUCTION_DESTROYED_UP_TWICE],
183
+ ELEMENTS[:CONSTRUCTION_DESTROYED_LEFT_TWICE],
184
+ ELEMENTS[:CONSTRUCTION_DESTROYED_RIGHT_TWICE],
185
+ ELEMENTS[:CONSTRUCTION_DESTROYED_LEFT_RIGHT],
186
+ ELEMENTS[:CONSTRUCTION_DESTROYED_UP_DOWN],
187
+ ELEMENTS[:CONSTRUCTION_DESTROYED_UP_LEFT],
188
+ ELEMENTS[:CONSTRUCTION_DESTROYED_RIGHT_UP],
189
+ ELEMENTS[:CONSTRUCTION_DESTROYED_DOWN_LEFT],
190
+ ELEMENTS[:CONSTRUCTION_DESTROYED_DOWN_RIGHT]
191
+ ]
192
+
193
+ def process(data)
194
+ @raw = data
195
+ end
196
+
197
+ def size
198
+ @size ||= Math.sqrt(@raw.length);
199
+ end
200
+
201
+ def xyl
202
+ @xyl ||= LengthToXY.new(size);
203
+ end
204
+
205
+ def getAt(x, y)
206
+ return false if Point.new(x, y).out_of?(size)
207
+ @raw[xyl.getLength(x, y)];
208
+ end
209
+
210
+ def at?(x, y, element)
211
+ return false if Point.new(x, y).out_of?(size)
212
+ getAt(x, y) == element;
213
+ end
214
+
215
+ def findAll(element)
216
+ result = []
217
+ @raw.length.times do |i|
218
+ point = xyl.getXY(i);
219
+ result.push(point) if at?(point.x, point.y, element)
220
+ end
221
+ result;
222
+ end
223
+
224
+ def get_me
225
+ me = find_by_list(TANK)
226
+ return nil if me.nil?
227
+ find_by_list(TANK).flatten
228
+ end
229
+
230
+ def find_by_list(list)
231
+ result = list.map{ |e| findAll(e) }.flatten.map{ |e| [e.x, e.y] }
232
+ return nil if (result.length == 0)
233
+ result
234
+ end
235
+
236
+ def get_enemies
237
+ find_by_list(ENEMIES)
238
+ end
239
+
240
+ def get_bullets
241
+ find_by_list([ELEMENTS[:BULLET]])
242
+ end
243
+
244
+ def get_near(x, y)
245
+ return false if Point.new(x, y).out_of?(size)
246
+ result = []
247
+ (-1..1).each do |dx|
248
+ (-1..1).each do |dy|
249
+ next if (dx == 0 && dy == 0)
250
+ result.push(getAt(x + dx, y + dy))
251
+ end
252
+ end
253
+ result;
254
+ end
255
+
256
+ def barrier_at?(x, y)
257
+ return false if Point.new(x, y).out_of?(size)
258
+ get_barriers.include?([x.to_f, y.to_f]);
259
+ end
260
+
261
+ def count_near(x, y, element)
262
+ get_near(x, y).select{ |e| e == element}.size
263
+ end
264
+
265
+ def near?(x, y, element)
266
+ n = get_near(x, y)
267
+ return false if !n
268
+ n.include?(element);
269
+ end
270
+
271
+ def bullet_at?(x, y)
272
+ return false if Point.new(x, y).out_of?(size)
273
+ getAt(x, y) == ELEMENTS[:BULLET]
274
+ end
275
+
276
+ def any_of_at?(x, y, elements = [])
277
+ return false if Point.new(x, y).out_of?(size)
278
+ elements.each do |e|
279
+ return true if at?(x, y, e)
280
+ end
281
+ false;
282
+ end
283
+
284
+ def game_over?
285
+ get_me.nil?;
286
+ end
287
+
288
+ def board_to_s
289
+ Array.new(size).each_with_index.map{ |e, n| @raw[(n * size)..((n + 1) * size - 1)]}.join("\n")
290
+ end
291
+
292
+ def get_barriers
293
+ find_by_list(BARRIERS)
294
+ end
295
+
296
+ def to_s
297
+ [
298
+ "Board:\n#{board_to_s}",
299
+ "My tank at: #{get_me}",
300
+ "Enemies at: #{get_enemies}",
301
+ "Bullets at: #{get_bullets}"
302
+ ].join("\n")
303
+ end
304
+ end
305
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codenjoy-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.001
5
5
  platform: ruby
6
6
  authors:
7
7
  - vgulaev
@@ -60,6 +60,7 @@ files:
60
60
  - game_base_client.rb
61
61
  - lib/codenjoy/client.rb
62
62
  - lib/codenjoy/client/version.rb
63
+ - lib/codenjoy/games/battlecity/board.rb
63
64
  homepage: https://github.com/vgulaev/codenjoy-client
64
65
  licenses:
65
66
  - MIT