battlesnake 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -69,11 +69,13 @@
69
69
  <dl>
70
70
  <dt>Inherits:</dt>
71
71
  <dd>
72
- <span class="inheritName">Object</span>
72
+ <span class="inheritName"><span class='object_link'><a href="Base.html" title="Battlesnake::Base (class)">Base</a></span></span>
73
73
 
74
74
  <ul class="fullTree">
75
75
  <li>Object</li>
76
76
 
77
+ <li class="next"><span class='object_link'><a href="Base.html" title="Battlesnake::Base (class)">Base</a></span></li>
78
+
77
79
  <li class="next">Battlesnake::Location</li>
78
80
 
79
81
  </ul>
@@ -369,6 +371,17 @@
369
371
  </ul>
370
372
 
371
373
 
374
+
375
+
376
+
377
+
378
+
379
+
380
+
381
+
382
+
383
+ <h3 class="inherited">Methods inherited from <span class='object_link'><a href="Base.html" title="Battlesnake::Base (class)">Base</a></span></h3>
384
+ <p class="inherited"><span class='object_link'><a href="Base.html#==-instance_method" title="Battlesnake::Base#== (method)">#==</a></span></p>
372
385
  <div id="constructor_details" class="method_details_list">
373
386
  <h2>Constructor Details</h2>
374
387
 
@@ -995,7 +1008,7 @@
995
1008
  </div>
996
1009
 
997
1010
  <div id="footer">
998
- Generated on Fri Nov 4 19:07:50 2022 by
1011
+ Generated on Mon Nov 7 15:30:13 2022 by
999
1012
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
1000
1013
  0.9.28 (ruby-2.7.2).
1001
1014
  </div>
@@ -69,11 +69,13 @@
69
69
  <dl>
70
70
  <dt>Inherits:</dt>
71
71
  <dd>
72
- <span class="inheritName">Object</span>
72
+ <span class="inheritName"><span class='object_link'><a href="Base.html" title="Battlesnake::Base (class)">Base</a></span></span>
73
73
 
74
74
  <ul class="fullTree">
75
75
  <li>Object</li>
76
76
 
77
+ <li class="next"><span class='object_link'><a href="Base.html" title="Battlesnake::Base (class)">Base</a></span></li>
78
+
77
79
  <li class="next">Battlesnake::Snake</li>
78
80
 
79
81
  </ul>
@@ -502,6 +504,17 @@
502
504
  </ul>
503
505
 
504
506
 
507
+
508
+
509
+
510
+
511
+
512
+
513
+
514
+
515
+
516
+ <h3 class="inherited">Methods inherited from <span class='object_link'><a href="Base.html" title="Battlesnake::Base (class)">Base</a></span></h3>
517
+ <p class="inherited"><span class='object_link'><a href="Base.html#==-instance_method" title="Battlesnake::Base#== (method)">#==</a></span></p>
505
518
  <div id="constructor_details" class="method_details_list">
506
519
  <h2>Constructor Details</h2>
507
520
 
@@ -1339,7 +1352,7 @@
1339
1352
  </div>
1340
1353
 
1341
1354
  <div id="footer">
1342
- Generated on Fri Nov 4 19:07:50 2022 by
1355
+ Generated on Mon Nov 7 15:30:13 2022 by
1343
1356
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
1344
1357
  0.9.28 (ruby-2.7.2).
1345
1358
  </div>
@@ -79,7 +79,7 @@
79
79
  <dl>
80
80
  <dt>Defined in:</dt>
81
81
  <dd>lib/battlesnake.rb<span class="defines">,<br />
82
- lib/battlesnake/board.rb,<br /> lib/battlesnake/snake.rb,<br /> lib/battlesnake/version.rb,<br /> lib/battlesnake/location.rb</span>
82
+ lib/battlesnake/base.rb,<br /> lib/battlesnake/game.rb,<br /> lib/battlesnake/board.rb,<br /> lib/battlesnake/snake.rb,<br /> lib/battlesnake/version.rb,<br /> lib/battlesnake/location.rb</span>
83
83
  </dd>
84
84
  </dl>
85
85
 
@@ -102,7 +102,7 @@
102
102
 
103
103
 
104
104
 
105
- <strong class="classes">Classes:</strong> <span class='object_link'><a href="Battlesnake/Board.html" title="Battlesnake::Board (class)">Board</a></span>, <span class='object_link'><a href="Battlesnake/Error.html" title="Battlesnake::Error (class)">Error</a></span>, <span class='object_link'><a href="Battlesnake/Location.html" title="Battlesnake::Location (class)">Location</a></span>, <span class='object_link'><a href="Battlesnake/Snake.html" title="Battlesnake::Snake (class)">Snake</a></span>
105
+ <strong class="classes">Classes:</strong> <span class='object_link'><a href="Battlesnake/Base.html" title="Battlesnake::Base (class)">Base</a></span>, <span class='object_link'><a href="Battlesnake/Board.html" title="Battlesnake::Board (class)">Board</a></span>, <span class='object_link'><a href="Battlesnake/Error.html" title="Battlesnake::Error (class)">Error</a></span>, <span class='object_link'><a href="Battlesnake/Game.html" title="Battlesnake::Game (class)">Game</a></span>, <span class='object_link'><a href="Battlesnake/Location.html" title="Battlesnake::Location (class)">Location</a></span>, <span class='object_link'><a href="Battlesnake/Snake.html" title="Battlesnake::Snake (class)">Snake</a></span>
106
106
 
107
107
 
108
108
  </p>
@@ -129,7 +129,7 @@
129
129
 
130
130
  </div>
131
131
  </dt>
132
- <dd><pre class="code"><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>0.1.0</span><span class='tstring_end'>&quot;</span></span></pre></dd>
132
+ <dd><pre class="code"><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>0.1.2</span><span class='tstring_end'>&quot;</span></span></pre></dd>
133
133
 
134
134
  </dl>
135
135
 
@@ -145,7 +145,7 @@
145
145
  </div>
146
146
 
147
147
  <div id="footer">
148
- Generated on Fri Nov 4 19:07:49 2022 by
148
+ Generated on Mon Nov 7 15:30:13 2022 by
149
149
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
150
150
  0.9.28 (ruby-2.7.2).
151
151
  </div>
data/docs/_index.html CHANGED
@@ -80,6 +80,13 @@
80
80
  <li class="letter">B</li>
81
81
  <ul>
82
82
 
83
+ <li>
84
+ <span class='object_link'><a href="Battlesnake/Base.html" title="Battlesnake::Base (class)">Base</a></span>
85
+
86
+ <small>(Battlesnake)</small>
87
+
88
+ </li>
89
+
83
90
  <li>
84
91
  <span class='object_link'><a href="Battlesnake.html" title="Battlesnake (module)">Battlesnake</a></span>
85
92
 
@@ -111,6 +118,21 @@
111
118
  </ul>
112
119
 
113
120
 
121
+ <ul id="alpha_G" class="alpha">
122
+ <li class="letter">G</li>
123
+ <ul>
124
+
125
+ <li>
126
+ <span class='object_link'><a href="Battlesnake/Game.html" title="Battlesnake::Game (class)">Game</a></span>
127
+
128
+ <small>(Battlesnake)</small>
129
+
130
+ </li>
131
+
132
+ </ul>
133
+ </ul>
134
+
135
+
114
136
  <ul id="alpha_L" class="alpha">
115
137
  <li class="letter">L</li>
116
138
  <ul>
@@ -149,7 +171,7 @@
149
171
  </div>
150
172
 
151
173
  <div id="footer">
152
- Generated on Fri Nov 4 19:07:49 2022 by
174
+ Generated on Mon Nov 7 15:30:12 2022 by
153
175
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
154
176
  0.9.28 (ruby-2.7.2).
155
177
  </div>
data/docs/class_list.html CHANGED
@@ -43,7 +43,7 @@
43
43
 
44
44
  <ul id="full_list" class="class">
45
45
  <li id="object_" class="odd"><div class="item" style="padding-left:30px"><span class='object_link'><a href="top-level-namespace.html" title="Top Level Namespace (root)">Top Level Namespace</a></span></div></li>
46
- <li id='object_Battlesnake' class='even'><div class='item' style='padding-left:30px'><a class='toggle'></a> <span class='object_link'><a href="Battlesnake.html" title="Battlesnake (module)">Battlesnake</a></span><small class='search_info'>Top Level Namespace</small></div><ul><li id='object_Battlesnake::Board' class='collapsed odd'><div class='item' style='padding-left:45px'><span class='object_link'><a href="Battlesnake/Board.html" title="Battlesnake::Board (class)">Board</a></span> &lt; Object<small class='search_info'>Battlesnake</small></div></li><li id='object_Battlesnake::Error' class='collapsed even'><div class='item' style='padding-left:45px'><span class='object_link'><a href="Battlesnake/Error.html" title="Battlesnake::Error (class)">Error</a></span> &lt; StandardError<small class='search_info'>Battlesnake</small></div></li><li id='object_Battlesnake::Location' class='collapsed odd'><div class='item' style='padding-left:45px'><span class='object_link'><a href="Battlesnake/Location.html" title="Battlesnake::Location (class)">Location</a></span> &lt; Object<small class='search_info'>Battlesnake</small></div></li><li id='object_Battlesnake::Snake' class='collapsed even'><div class='item' style='padding-left:45px'><span class='object_link'><a href="Battlesnake/Snake.html" title="Battlesnake::Snake (class)">Snake</a></span> &lt; Object<small class='search_info'>Battlesnake</small></div></li></ul></li>
46
+ <li id='object_Battlesnake' class='even'><div class='item' style='padding-left:30px'><a class='toggle'></a> <span class='object_link'><a href="Battlesnake.html" title="Battlesnake (module)">Battlesnake</a></span><small class='search_info'>Top Level Namespace</small></div><ul><li id='object_Battlesnake::Base' class='collapsed odd'><div class='item' style='padding-left:45px'><span class='object_link'><a href="Battlesnake/Base.html" title="Battlesnake::Base (class)">Base</a></span> &lt; Object<small class='search_info'>Battlesnake</small></div></li><li id='object_Battlesnake::Board' class='collapsed even'><div class='item' style='padding-left:45px'><span class='object_link'><a href="Battlesnake/Board.html" title="Battlesnake::Board (class)">Board</a></span> &lt; Base<small class='search_info'>Battlesnake</small></div></li><li id='object_Battlesnake::Error' class='collapsed odd'><div class='item' style='padding-left:45px'><span class='object_link'><a href="Battlesnake/Error.html" title="Battlesnake::Error (class)">Error</a></span> &lt; StandardError<small class='search_info'>Battlesnake</small></div></li><li id='object_Battlesnake::Game' class='collapsed even'><div class='item' style='padding-left:45px'><span class='object_link'><a href="Battlesnake/Game.html" title="Battlesnake::Game (class)">Game</a></span> &lt; Base<small class='search_info'>Battlesnake</small></div></li><li id='object_Battlesnake::Location' class='collapsed odd'><div class='item' style='padding-left:45px'><span class='object_link'><a href="Battlesnake/Location.html" title="Battlesnake::Location (class)">Location</a></span> &lt; Base<small class='search_info'>Battlesnake</small></div></li><li id='object_Battlesnake::Snake' class='collapsed even'><div class='item' style='padding-left:45px'><span class='object_link'><a href="Battlesnake/Snake.html" title="Battlesnake::Snake (class)">Snake</a></span> &lt; Base<small class='search_info'>Battlesnake</small></div></li></ul></li>
47
47
 
48
48
  </ul>
49
49
  </div>
@@ -97,7 +97,7 @@
97
97
  </div></div>
98
98
 
99
99
  <div id="footer">
100
- Generated on Fri Nov 4 19:07:49 2022 by
100
+ Generated on Mon Nov 7 15:30:13 2022 by
101
101
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
102
102
  0.9.28 (ruby-2.7.2).
103
103
  </div>
data/docs/index.html CHANGED
@@ -97,7 +97,7 @@
97
97
  </div></div>
98
98
 
99
99
  <div id="footer">
100
- Generated on Fri Nov 4 19:07:49 2022 by
100
+ Generated on Mon Nov 7 15:30:12 2022 by
101
101
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
102
102
  0.9.28 (ruby-2.7.2).
103
103
  </div>
@@ -46,8 +46,8 @@
46
46
 
47
47
  <li class="odd ">
48
48
  <div class="item">
49
- <span class='object_link'><a href="Battlesnake/Snake.html#as_json-instance_method" title="Battlesnake::Snake#as_json (method)">#as_json</a></span>
50
- <small>Battlesnake::Snake</small>
49
+ <span class='object_link'><a href="Battlesnake/Base.html#==-instance_method" title="Battlesnake::Base#== (method)">#==</a></span>
50
+ <small>Battlesnake::Base</small>
51
51
  </div>
52
52
  </li>
53
53
 
@@ -60,6 +60,22 @@
60
60
  </li>
61
61
 
62
62
 
63
+ <li class="odd ">
64
+ <div class="item">
65
+ <span class='object_link'><a href="Battlesnake/Game.html#as_json-instance_method" title="Battlesnake::Game#as_json (method)">#as_json</a></span>
66
+ <small>Battlesnake::Game</small>
67
+ </div>
68
+ </li>
69
+
70
+
71
+ <li class="even ">
72
+ <div class="item">
73
+ <span class='object_link'><a href="Battlesnake/Snake.html#as_json-instance_method" title="Battlesnake::Snake#as_json (method)">#as_json</a></span>
74
+ <small>Battlesnake::Snake</small>
75
+ </div>
76
+ </li>
77
+
78
+
63
79
  <li class="odd ">
64
80
  <div class="item">
65
81
  <span class='object_link'><a href="Battlesnake/Board.html#as_json-instance_method" title="Battlesnake::Board#as_json (method)">#as_json</a></span>
@@ -68,6 +84,22 @@
68
84
  </li>
69
85
 
70
86
 
87
+ <li class="even ">
88
+ <div class="item">
89
+ <span class='object_link'><a href="Battlesnake/Board.html#available%3F-instance_method" title="Battlesnake::Board#available? (method)">#available?</a></span>
90
+ <small>Battlesnake::Board</small>
91
+ </div>
92
+ </li>
93
+
94
+
95
+ <li class="odd ">
96
+ <div class="item">
97
+ <span class='object_link'><a href="Battlesnake/Board.html#available_directions-instance_method" title="Battlesnake::Board#available_directions (method)">#available_directions</a></span>
98
+ <small>Battlesnake::Board</small>
99
+ </div>
100
+ </li>
101
+
102
+
71
103
  <li class="even ">
72
104
  <div class="item">
73
105
  <span class='object_link'><a href="Battlesnake/Snake.html#body-instance_method" title="Battlesnake::Snake#body (method)">#body</a></span>
@@ -166,13 +198,21 @@
166
198
 
167
199
  <li class="even ">
168
200
  <div class="item">
169
- <span class='object_link'><a href="Battlesnake/Location.html#initialize-instance_method" title="Battlesnake::Location#initialize (method)">#initialize</a></span>
170
- <small>Battlesnake::Location</small>
201
+ <span class='object_link'><a href="Battlesnake/Game.html#id-instance_method" title="Battlesnake::Game#id (method)">#id</a></span>
202
+ <small>Battlesnake::Game</small>
171
203
  </div>
172
204
  </li>
173
205
 
174
206
 
175
207
  <li class="odd ">
208
+ <div class="item">
209
+ <span class='object_link'><a href="Battlesnake/Snake.html#initialize-instance_method" title="Battlesnake::Snake#initialize (method)">#initialize</a></span>
210
+ <small>Battlesnake::Snake</small>
211
+ </div>
212
+ </li>
213
+
214
+
215
+ <li class="even ">
176
216
  <div class="item">
177
217
  <span class='object_link'><a href="Battlesnake/Board.html#initialize-instance_method" title="Battlesnake::Board#initialize (method)">#initialize</a></span>
178
218
  <small>Battlesnake::Board</small>
@@ -180,10 +220,18 @@
180
220
  </li>
181
221
 
182
222
 
223
+ <li class="odd ">
224
+ <div class="item">
225
+ <span class='object_link'><a href="Battlesnake/Location.html#initialize-instance_method" title="Battlesnake::Location#initialize (method)">#initialize</a></span>
226
+ <small>Battlesnake::Location</small>
227
+ </div>
228
+ </li>
229
+
230
+
183
231
  <li class="even ">
184
232
  <div class="item">
185
- <span class='object_link'><a href="Battlesnake/Snake.html#initialize-instance_method" title="Battlesnake::Snake#initialize (method)">#initialize</a></span>
186
- <small>Battlesnake::Snake</small>
233
+ <span class='object_link'><a href="Battlesnake/Game.html#initialize-instance_method" title="Battlesnake::Game#initialize (method)">#initialize</a></span>
234
+ <small>Battlesnake::Game</small>
187
235
  </div>
188
236
  </li>
189
237
 
@@ -205,6 +253,14 @@
205
253
 
206
254
 
207
255
  <li class="odd ">
256
+ <div class="item">
257
+ <span class='object_link'><a href="Battlesnake/Game.html#map-instance_method" title="Battlesnake::Game#map (method)">#map</a></span>
258
+ <small>Battlesnake::Game</small>
259
+ </div>
260
+ </li>
261
+
262
+
263
+ <li class="even ">
208
264
  <div class="item">
209
265
  <span class='object_link'><a href="Battlesnake/Location.html#move-instance_method" title="Battlesnake::Location#move (method)">#move</a></span>
210
266
  <small>Battlesnake::Location</small>
@@ -212,7 +268,7 @@
212
268
  </li>
213
269
 
214
270
 
215
- <li class="even ">
271
+ <li class="odd ">
216
272
  <div class="item">
217
273
  <span class='object_link'><a href="Battlesnake/Snake.html#name-instance_method" title="Battlesnake::Snake#name (method)">#name</a></span>
218
274
  <small>Battlesnake::Snake</small>
@@ -220,6 +276,46 @@
220
276
  </li>
221
277
 
222
278
 
279
+ <li class="even ">
280
+ <div class="item">
281
+ <span class='object_link'><a href="Battlesnake/Board.html#occupied%3F-instance_method" title="Battlesnake::Board#occupied? (method)">#occupied?</a></span>
282
+ <small>Battlesnake::Board</small>
283
+ </div>
284
+ </li>
285
+
286
+
287
+ <li class="odd ">
288
+ <div class="item">
289
+ <span class='object_link'><a href="Battlesnake/Board.html#occupied_locations-instance_method" title="Battlesnake::Board#occupied_locations (method)">#occupied_locations</a></span>
290
+ <small>Battlesnake::Board</small>
291
+ </div>
292
+ </li>
293
+
294
+
295
+ <li class="even ">
296
+ <div class="item">
297
+ <span class='object_link'><a href="Battlesnake/Board.html#on_board%3F-instance_method" title="Battlesnake::Board#on_board? (method)">#on_board?</a></span>
298
+ <small>Battlesnake::Board</small>
299
+ </div>
300
+ </li>
301
+
302
+
303
+ <li class="odd ">
304
+ <div class="item">
305
+ <span class='object_link'><a href="Battlesnake/Board.html#paths-instance_method" title="Battlesnake::Board#paths (method)">#paths</a></span>
306
+ <small>Battlesnake::Board</small>
307
+ </div>
308
+ </li>
309
+
310
+
311
+ <li class="even ">
312
+ <div class="item">
313
+ <span class='object_link'><a href="Battlesnake/Game.html#ruleset-instance_method" title="Battlesnake::Game#ruleset (method)">#ruleset</a></span>
314
+ <small>Battlesnake::Game</small>
315
+ </div>
316
+ </li>
317
+
318
+
223
319
  <li class="odd ">
224
320
  <div class="item">
225
321
  <span class='object_link'><a href="Battlesnake/Snake.html#shout-instance_method" title="Battlesnake::Snake#shout (method)">#shout</a></span>
@@ -237,6 +333,14 @@
237
333
 
238
334
 
239
335
  <li class="odd ">
336
+ <div class="item">
337
+ <span class='object_link'><a href="Battlesnake/Game.html#source-instance_method" title="Battlesnake::Game#source (method)">#source</a></span>
338
+ <small>Battlesnake::Game</small>
339
+ </div>
340
+ </li>
341
+
342
+
343
+ <li class="even ">
240
344
  <div class="item">
241
345
  <span class='object_link'><a href="Battlesnake/Snake.html#squad-instance_method" title="Battlesnake::Snake#squad (method)">#squad</a></span>
242
346
  <small>Battlesnake::Snake</small>
@@ -244,6 +348,14 @@
244
348
  </li>
245
349
 
246
350
 
351
+ <li class="odd ">
352
+ <div class="item">
353
+ <span class='object_link'><a href="Battlesnake/Game.html#timeout-instance_method" title="Battlesnake::Game#timeout (method)">#timeout</a></span>
354
+ <small>Battlesnake::Game</small>
355
+ </div>
356
+ </li>
357
+
358
+
247
359
  <li class="even ">
248
360
  <div class="item">
249
361
  <span class='object_link'><a href="Battlesnake/Board.html#width-instance_method" title="Battlesnake::Board#width (method)">#width</a></span>
@@ -100,7 +100,7 @@
100
100
  </div>
101
101
 
102
102
  <div id="footer">
103
- Generated on Fri Nov 4 19:07:49 2022 by
103
+ Generated on Mon Nov 7 15:30:13 2022 by
104
104
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
105
105
  0.9.28 (ruby-2.7.2).
106
106
  </div>
@@ -0,0 +1,15 @@
1
+ module Battlesnake
2
+ ##
3
+ # Base class for "model" classes - _Game, Board, Snake, Location,_ etc.
4
+ class Base
5
+ ##
6
+ # Whether both locations have the same coordinates.
7
+ #
8
+ # @param other [Location]
9
+ #
10
+ # @return [Boolean] true if location objects have the same coordinates.
11
+ def ==(other)
12
+ other.is_a?(self.class) && self.as_json == other.as_json
13
+ end
14
+ end
15
+ end
@@ -3,7 +3,7 @@ require 'json'
3
3
  module Battlesnake
4
4
  ##
5
5
  # Represents a single iteration (turn) of a Battlesnake board during gameplay.
6
- class Board
6
+ class Board < Base
7
7
  # @return [Hash] board as a data structure usable by other objects.
8
8
  attr_reader :as_json
9
9
 
@@ -39,5 +39,180 @@ module Battlesnake
39
39
  @food = data['food'].map{ |attrs| Location.new(attrs) }
40
40
  @hazards = data['hazards'].map{ |attrs| Location.new(attrs) }
41
41
  end
42
+
43
+ ##
44
+ # List of all occupied locations on the board; snakes, food, hazards, etc
45
+ #
46
+ # @return [Array<Location>] list of occupied locations
47
+ def occupied_locations
48
+ return @occupied_locations if defined?(@occupied_locations)
49
+ @occupied_locations = snakes.map(&:body).flatten + food + hazards
50
+ end
51
+
52
+ ##
53
+ # Whether the supplied location is occupied.
54
+ #
55
+ # @param [Location] location being checked for occupancy.
56
+ #
57
+ # @return [Boolean] true if location is occupied by snakes, food, hazards, etc.
58
+ def occupied?(location)
59
+ occupied_locations.include?(location)
60
+ end
61
+
62
+ ##
63
+ # Where the supplied location falls within the boundaries of the board.
64
+ #
65
+ # @param [Location] location being tested.
66
+ #
67
+ # @return [Boolean] true if location is within the boundaries of the board.
68
+ def on_board?(location)
69
+ location.x >= 0 && location.y >= 0 && location.x < width && location.y < height
70
+ end
71
+
72
+ ##
73
+ # Whether the supplied location is available (unoccupied).
74
+ #
75
+ # @param [Location] location being tested for availability.
76
+ #
77
+ # @return [Boolean] true if location is available (unoccupied by snakes, food, hazards, etc).
78
+ def available?(location)
79
+ on_board?(location) && !occupied?(location)
80
+ end
81
+
82
+ ##
83
+ # List of directions (up, down, left, right) available for moving from given _Location_.
84
+ #
85
+ # @param [Location] location from which moving is desired.
86
+ #
87
+ # @return [Array<String>] list of direction strings ("up", "down", "left", "right")
88
+ def available_directions(location)
89
+ Location::DIRECTIONS.select do |direction|
90
+ available?(location.move(direction))
91
+ end
92
+ end
93
+
94
+ ##
95
+ # List of valid, consecutive paths from one location to the next. Paths may not:
96
+ #
97
+ # - wander outside board boundaries.
98
+ # - use the same location more than once.
99
+ # - contain occupied locations, EXCEPT the start/end locations.
100
+ #
101
+ # The exception for start/end locations allows us to generate paths, for example, from a snake
102
+ # to a food location, without having to calulate the starting/ending permutations ourselves.
103
+ #
104
+ # @param from [Location] starting location, may be occupied
105
+ # @param to [Location] starting location, may be occupied
106
+ #
107
+ # @return [Array<Path>] a list of paths, which themselves are lists of consecutive, valid locations.
108
+ def find_path(from, to, max_distance: nil)
109
+ @paths = []
110
+ @ideal_path_size = from.distance(to) + 1
111
+ @shortest_path_size = max_distance || @ideal_path_size
112
+ @ideal_path_size_found = false
113
+
114
+ recursive_paths(from, to, [from])
115
+
116
+ @paths.select{ |path| path.size == @shortest_path_size }.first
117
+ end
118
+
119
+ def recursive_paths(from, to, path)
120
+ head = path.last
121
+
122
+ # give up if path is too long already.
123
+ return [] if path.size > @shortest_path_size || @ideal_path_size_found
124
+
125
+ # if we've made it to "to", we have a successful candidate path.
126
+ if head.as_json == to.as_json
127
+ @paths << path
128
+ @shortest_path_size = [@shortest_path_size, path.size].min
129
+ @ideal_path_size_found = true if path.size == @ideal_path_size
130
+
131
+ return path
132
+ end
133
+
134
+ available_directions(head).sort_by do |direction|
135
+ # prefer to continue in same direction
136
+ neck = path[-2]
137
+
138
+ if neck && neck.direction(head) == direction
139
+ 0
140
+ else
141
+ rand
142
+ end
143
+ end.map do |direction|
144
+ # convert direction string to a location
145
+ head.move(direction)
146
+ end.map do |location|
147
+ # convert location to a full path
148
+ path + [location]
149
+ end.select do |candidate|
150
+ # don't allow paths that overlap themselves
151
+ candidate.size == candidate.uniq(&:as_json).size
152
+ end.each do |candidate|
153
+ # recurse into remaining candidate paths
154
+ recursive_paths(from, to, candidate)
155
+ end
156
+ end
157
+
158
+ def shorty(location)
159
+ "#{location.x}:#{location.y}"
160
+ end
161
+
162
+ def shorties(list)
163
+ case list.first
164
+ when Array
165
+ list.map{ |l| shorties(l) }
166
+ when Location
167
+ list.map{|x| shorty(x)}.join(' ')
168
+ end
169
+ end
170
+
171
+ def print_grid(path, prefix: ' ')
172
+ max_x = path.map(&:x).max
173
+ max_y = path.map(&:y).max
174
+
175
+ (0..max_y).each do |row|
176
+ y = max_y - row
177
+
178
+ cols = (0..max_x).map do |x|
179
+ loc = Location.new(x, y)
180
+
181
+ if path.include?(loc)
182
+ after = path.index{ |l| l.as_json == loc.as_json} + 1
183
+
184
+ if after >= path.size
185
+ "\u00d7"
186
+ elsif loc.as_json == path.first.as_json
187
+ case loc.direction(path[after])
188
+ when 'up'
189
+ "\u21a5"
190
+ when 'down'
191
+ "\u21a7"
192
+ when 'left'
193
+ "\u21a4"
194
+ when 'right'
195
+ "\u21a6"
196
+ end
197
+ else
198
+ case loc.direction(path[after])
199
+ when 'up'
200
+ "\u2191"
201
+ when 'down'
202
+ "\u2193"
203
+ when 'left'
204
+ "\u2190"
205
+ when 'right'
206
+ "\u2192"
207
+ end
208
+ end
209
+ else
210
+ 'O'
211
+ end
212
+ end.join(' ')
213
+
214
+ puts "#{prefix} #{cols}"
215
+ end
216
+ end
42
217
  end
43
218
  end
@@ -0,0 +1,44 @@
1
+ require 'json'
2
+
3
+ module Battlesnake
4
+ ##
5
+ # Represents a Battlesnake game.
6
+ class Game < Base
7
+ # @return [Hash] snake as a data structure usable by other objects.
8
+ attr_reader :as_json
9
+
10
+ # @return [String] unique identifier for this game.
11
+ attr_reader :id
12
+
13
+ # @return [Hash] information about the ruleset being used to run this game.
14
+ attr_reader :ruleset
15
+
16
+ # @return [String] name of the map used to populate the game board with snakes, food, and hazards.
17
+ attr_reader :map
18
+
19
+ # @return [Integer] how much time player APIs have to respond to requests for this game.
20
+ attr_reader :timeout
21
+
22
+ # @return [String] source of this game.
23
+ attr_reader :source
24
+
25
+ ##
26
+ # Returns a new instance of Game.
27
+ #
28
+ # @param json_or_hash [String,Hash] can be a hash of attributes, or a JSON string which
29
+ # represents such a structure.
30
+ #
31
+ # @return [Game]
32
+ def initialize(json_or_hash)
33
+ data = json_or_hash.is_a?(String) ? JSON.parse(json_or_hash) : json_or_hash
34
+
35
+ @as_json = data
36
+
37
+ @id = data['id']
38
+ @ruleset = data['ruleset']
39
+ @map = data['map']
40
+ @timeout = data['timeout']
41
+ @source = data['source']
42
+ end
43
+ end
44
+ end