battlesnake 0.1.2 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +27 -0
- data/Gemfile.lock +1 -1
- data/docs/Battlesnake/Base.html +1 -1
- data/docs/Battlesnake/Board.html +257 -30
- data/docs/Battlesnake/Error.html +1 -1
- data/docs/Battlesnake/Game.html +1 -1
- data/docs/Battlesnake/Location.html +1 -1
- data/docs/Battlesnake/Snake.html +1 -1
- data/docs/Battlesnake.html +1 -1
- data/docs/_index.html +1 -1
- data/docs/file.README.html +1 -1
- data/docs/index.html +1 -1
- data/docs/method_list.html +24 -8
- data/docs/top-level-namespace.html +1 -1
- data/lib/battlesnake/board.rb +152 -12
- data/lib/battlesnake/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc1006d3ed555bc10587302b1d0bd9323e92f2a194bc2ce6673ac44e4b502f74
|
4
|
+
data.tar.gz: e5a12dcd3a84f68b6d70735448d115a007909018b27010597753c7dff41aba84
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66184f4a9b2474337f1d25a76a4ffd7cd2b63beeb390a7a3261e2ff51911b7c2dcabd453f7510018b785869ab25736d3e31e35e031111cbd4c8ef39966f1182c
|
7
|
+
data.tar.gz: 9ebae26dac464f629359f325f871500d0634a5bdf381ec416b8581ff358777ae94267973c5c937921279afc5dc45490ad63bc8e4357f4f2dce61cde08f0537ca
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,32 @@
|
|
1
1
|
# Battlesnake
|
2
2
|
|
3
|
+
## 0.1.4 (2023-01-03)
|
4
|
+
|
5
|
+
### Added
|
6
|
+
|
7
|
+
- Board
|
8
|
+
- #food?(location) returns true if location is food.
|
9
|
+
|
10
|
+
### Changed
|
11
|
+
|
12
|
+
- Board
|
13
|
+
- #occupied_locations no longer includes food.
|
14
|
+
|
15
|
+
## 0.1.3 (2022-11-07)
|
16
|
+
|
17
|
+
### Added
|
18
|
+
|
19
|
+
- Board
|
20
|
+
- #on_board?(location) returns true if location is within board boundaries.
|
21
|
+
- #paths(from, to) returns all valid paths from one location to the next.
|
22
|
+
|
23
|
+
### Changed
|
24
|
+
|
25
|
+
- Board
|
26
|
+
- #occupied?(location) now only accepts a location object.
|
27
|
+
- #available?(location) now only accepts a location object.
|
28
|
+
- locations must be both unoccupied AND on_board to be considered available.
|
29
|
+
|
3
30
|
## 0.1.2 (2022-11-05)
|
4
31
|
|
5
32
|
### Added
|
data/Gemfile.lock
CHANGED
data/docs/Battlesnake/Base.html
CHANGED
@@ -241,7 +241,7 @@
|
|
241
241
|
</div>
|
242
242
|
|
243
243
|
<div id="footer">
|
244
|
-
Generated on
|
244
|
+
Generated on Mon Nov 7 15:30:13 2022 by
|
245
245
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
246
246
|
0.9.28 (ruby-2.7.2).
|
247
247
|
</div>
|
data/docs/Battlesnake/Board.html
CHANGED
@@ -309,7 +309,7 @@
|
|
309
309
|
<li class="public ">
|
310
310
|
<span class="summary_signature">
|
311
311
|
|
312
|
-
<a href="#available%3F-instance_method" title="#available? (instance method)">#<strong>available?</strong>(
|
312
|
+
<a href="#available%3F-instance_method" title="#available? (instance method)">#<strong>available?</strong>(location) ⇒ Boolean </a>
|
313
313
|
|
314
314
|
|
315
315
|
|
@@ -383,7 +383,7 @@
|
|
383
383
|
<li class="public ">
|
384
384
|
<span class="summary_signature">
|
385
385
|
|
386
|
-
<a href="#occupied%3F-instance_method" title="#occupied? (instance method)">#<strong>occupied?</strong>(
|
386
|
+
<a href="#occupied%3F-instance_method" title="#occupied? (instance method)">#<strong>occupied?</strong>(location) ⇒ Boolean </a>
|
387
387
|
|
388
388
|
|
389
389
|
|
@@ -425,6 +425,54 @@
|
|
425
425
|
<p>List of all occupied locations on the board; snakes, food, hazards, etc.</p>
|
426
426
|
</div></span>
|
427
427
|
|
428
|
+
</li>
|
429
|
+
|
430
|
+
|
431
|
+
<li class="public ">
|
432
|
+
<span class="summary_signature">
|
433
|
+
|
434
|
+
<a href="#on_board%3F-instance_method" title="#on_board? (instance method)">#<strong>on_board?</strong>(location) ⇒ Boolean </a>
|
435
|
+
|
436
|
+
|
437
|
+
|
438
|
+
</span>
|
439
|
+
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
|
448
|
+
<span class="summary_desc"><div class='inline'>
|
449
|
+
<p>Where the supplied location falls within the boundaries of the board.</p>
|
450
|
+
</div></span>
|
451
|
+
|
452
|
+
</li>
|
453
|
+
|
454
|
+
|
455
|
+
<li class="public ">
|
456
|
+
<span class="summary_signature">
|
457
|
+
|
458
|
+
<a href="#paths-instance_method" title="#paths (instance method)">#<strong>paths</strong>(from, to) ⇒ Array<Path> </a>
|
459
|
+
|
460
|
+
|
461
|
+
|
462
|
+
</span>
|
463
|
+
|
464
|
+
|
465
|
+
|
466
|
+
|
467
|
+
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
|
472
|
+
<span class="summary_desc"><div class='inline'>
|
473
|
+
<p>List of valid, consecutive paths from one location to the next.</p>
|
474
|
+
</div></span>
|
475
|
+
|
428
476
|
</li>
|
429
477
|
|
430
478
|
|
@@ -901,7 +949,7 @@
|
|
901
949
|
<div class="method_details first">
|
902
950
|
<h3 class="signature first" id="available?-instance_method">
|
903
951
|
|
904
|
-
#<strong>available?</strong>(
|
952
|
+
#<strong>available?</strong>(location) ⇒ <tt>Boolean</tt>
|
905
953
|
|
906
954
|
|
907
955
|
|
@@ -921,16 +969,16 @@
|
|
921
969
|
|
922
970
|
<li>
|
923
971
|
|
924
|
-
<span class='name'
|
972
|
+
<span class='name'>location</span>
|
925
973
|
|
926
974
|
|
927
|
-
<span class='type'>(<tt><span class='object_link'><a href="Location.html" title="Battlesnake::Location (class)">Location</a></span></tt
|
975
|
+
<span class='type'>(<tt><span class='object_link'><a href="Location.html" title="Battlesnake::Location (class)">Location</a></span></tt>)</span>
|
928
976
|
|
929
977
|
|
930
978
|
|
931
979
|
—
|
932
980
|
<div class='inline'>
|
933
|
-
<p>
|
981
|
+
<p>being tested for availability.</p>
|
934
982
|
</div>
|
935
983
|
|
936
984
|
</li>
|
@@ -962,15 +1010,15 @@
|
|
962
1010
|
<pre class="lines">
|
963
1011
|
|
964
1012
|
|
965
|
-
|
966
|
-
|
967
|
-
|
1013
|
+
78
|
1014
|
+
79
|
1015
|
+
80</pre>
|
968
1016
|
</td>
|
969
1017
|
<td>
|
970
|
-
<pre class="code"><span class="info file"># File 'lib/battlesnake/board.rb', line
|
1018
|
+
<pre class="code"><span class="info file"># File 'lib/battlesnake/board.rb', line 78</span>
|
971
1019
|
|
972
|
-
<span class='kw'>def</span> <span class='id identifier rubyid_available?'>available?</span><span class='lparen'>(</span><span class='
|
973
|
-
<span class='
|
1020
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_available?'>available?</span><span class='lparen'>(</span><span class='id identifier rubyid_location'>location</span><span class='rparen'>)</span>
|
1021
|
+
<span class='id identifier rubyid_on_board?'>on_board?</span><span class='lparen'>(</span><span class='id identifier rubyid_location'>location</span><span class='rparen'>)</span> <span class='op'>&&</span> <span class='op'>!</span><span class='id identifier rubyid_occupied?'>occupied?</span><span class='lparen'>(</span><span class='id identifier rubyid_location'>location</span><span class='rparen'>)</span>
|
974
1022
|
<span class='kw'>end</span></pre>
|
975
1023
|
</td>
|
976
1024
|
</tr>
|
@@ -1041,14 +1089,14 @@
|
|
1041
1089
|
<pre class="lines">
|
1042
1090
|
|
1043
1091
|
|
1044
|
-
|
1045
|
-
|
1046
|
-
|
1047
|
-
|
1048
|
-
|
1092
|
+
88
|
1093
|
+
89
|
1094
|
+
90
|
1095
|
+
91
|
1096
|
+
92</pre>
|
1049
1097
|
</td>
|
1050
1098
|
<td>
|
1051
|
-
<pre class="code"><span class="info file"># File 'lib/battlesnake/board.rb', line
|
1099
|
+
<pre class="code"><span class="info file"># File 'lib/battlesnake/board.rb', line 88</span>
|
1052
1100
|
|
1053
1101
|
<span class='kw'>def</span> <span class='id identifier rubyid_available_directions'>available_directions</span><span class='lparen'>(</span><span class='id identifier rubyid_location'>location</span><span class='rparen'>)</span>
|
1054
1102
|
<span class='const'><span class='object_link'><a href="Location.html" title="Battlesnake::Location (class)">Location</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Location.html#DIRECTIONS-constant" title="Battlesnake::Location::DIRECTIONS (constant)">DIRECTIONS</a></span></span><span class='period'>.</span><span class='id identifier rubyid_select'>select</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_direction'>direction</span><span class='op'>|</span>
|
@@ -1063,7 +1111,7 @@
|
|
1063
1111
|
<div class="method_details ">
|
1064
1112
|
<h3 class="signature " id="occupied?-instance_method">
|
1065
1113
|
|
1066
|
-
#<strong>occupied?</strong>(
|
1114
|
+
#<strong>occupied?</strong>(location) ⇒ <tt>Boolean</tt>
|
1067
1115
|
|
1068
1116
|
|
1069
1117
|
|
@@ -1083,16 +1131,16 @@
|
|
1083
1131
|
|
1084
1132
|
<li>
|
1085
1133
|
|
1086
|
-
<span class='name'
|
1134
|
+
<span class='name'>location</span>
|
1087
1135
|
|
1088
1136
|
|
1089
|
-
<span class='type'>(<tt><span class='object_link'><a href="Location.html" title="Battlesnake::Location (class)">Location</a></span></tt
|
1137
|
+
<span class='type'>(<tt><span class='object_link'><a href="Location.html" title="Battlesnake::Location (class)">Location</a></span></tt>)</span>
|
1090
1138
|
|
1091
1139
|
|
1092
1140
|
|
1093
1141
|
—
|
1094
1142
|
<div class='inline'>
|
1095
|
-
<p>
|
1143
|
+
<p>being checked for occupancy.</p>
|
1096
1144
|
</div>
|
1097
1145
|
|
1098
1146
|
</li>
|
@@ -1124,16 +1172,14 @@
|
|
1124
1172
|
<pre class="lines">
|
1125
1173
|
|
1126
1174
|
|
1127
|
-
|
1128
|
-
|
1129
|
-
|
1130
|
-
63</pre>
|
1175
|
+
58
|
1176
|
+
59
|
1177
|
+
60</pre>
|
1131
1178
|
</td>
|
1132
1179
|
<td>
|
1133
|
-
<pre class="code"><span class="info file"># File 'lib/battlesnake/board.rb', line
|
1180
|
+
<pre class="code"><span class="info file"># File 'lib/battlesnake/board.rb', line 58</span>
|
1134
1181
|
|
1135
|
-
<span class='kw'>def</span> <span class='id identifier rubyid_occupied?'>occupied?</span><span class='lparen'>(</span><span class='
|
1136
|
-
<span class='id identifier rubyid_location'>location</span> <span class='op'>=</span> <span class='id identifier rubyid_coordinates'>coordinates</span><span class='period'>.</span><span class='id identifier rubyid_first'>first</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'><span class='object_link'><a href="Location.html" title="Battlesnake::Location (class)">Location</a></span></span><span class='rparen'>)</span> <span class='op'>?</span> <span class='id identifier rubyid_coordinates'>coordinates</span><span class='period'>.</span><span class='id identifier rubyid_first'>first</span> <span class='op'>:</span> <span class='const'><span class='object_link'><a href="Location.html" title="Battlesnake::Location (class)">Location</a></span></span><span class='period'>.</span><span class='id identifier rubyid_new'><span class='object_link'><a href="Location.html#initialize-instance_method" title="Battlesnake::Location#initialize (method)">new</a></span></span><span class='lparen'>(</span><span class='op'>*</span><span class='id identifier rubyid_coordinates'>coordinates</span><span class='rparen'>)</span>
|
1182
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_occupied?'>occupied?</span><span class='lparen'>(</span><span class='id identifier rubyid_location'>location</span><span class='rparen'>)</span>
|
1137
1183
|
<span class='id identifier rubyid_occupied_locations'>occupied_locations</span><span class='period'>.</span><span class='id identifier rubyid_include?'>include?</span><span class='lparen'>(</span><span class='id identifier rubyid_location'>location</span><span class='rparen'>)</span>
|
1138
1184
|
<span class='kw'>end</span></pre>
|
1139
1185
|
</td>
|
@@ -1196,6 +1242,187 @@
|
|
1196
1242
|
<span class='kw'>def</span> <span class='id identifier rubyid_occupied_locations'>occupied_locations</span>
|
1197
1243
|
<span class='kw'>return</span> <span class='ivar'>@occupied_locations</span> <span class='kw'>if</span> <span class='kw'>defined?</span><span class='lparen'>(</span><span class='ivar'>@occupied_locations</span><span class='rparen'>)</span>
|
1198
1244
|
<span class='ivar'>@occupied_locations</span> <span class='op'>=</span> <span class='id identifier rubyid_snakes'>snakes</span><span class='period'>.</span><span class='id identifier rubyid_map'>map</span><span class='lparen'>(</span><span class='op'>&</span><span class='symbol'>:body</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_flatten'>flatten</span> <span class='op'>+</span> <span class='id identifier rubyid_food'>food</span> <span class='op'>+</span> <span class='id identifier rubyid_hazards'>hazards</span>
|
1245
|
+
<span class='kw'>end</span></pre>
|
1246
|
+
</td>
|
1247
|
+
</tr>
|
1248
|
+
</table>
|
1249
|
+
</div>
|
1250
|
+
|
1251
|
+
<div class="method_details ">
|
1252
|
+
<h3 class="signature " id="on_board?-instance_method">
|
1253
|
+
|
1254
|
+
#<strong>on_board?</strong>(location) ⇒ <tt>Boolean</tt>
|
1255
|
+
|
1256
|
+
|
1257
|
+
|
1258
|
+
|
1259
|
+
|
1260
|
+
</h3><div class="docstring">
|
1261
|
+
<div class="discussion">
|
1262
|
+
|
1263
|
+
<p>Where the supplied location falls within the boundaries of the board.</p>
|
1264
|
+
|
1265
|
+
|
1266
|
+
</div>
|
1267
|
+
</div>
|
1268
|
+
<div class="tags">
|
1269
|
+
<p class="tag_title">Parameters:</p>
|
1270
|
+
<ul class="param">
|
1271
|
+
|
1272
|
+
<li>
|
1273
|
+
|
1274
|
+
<span class='name'>location</span>
|
1275
|
+
|
1276
|
+
|
1277
|
+
<span class='type'>(<tt><span class='object_link'><a href="Location.html" title="Battlesnake::Location (class)">Location</a></span></tt>)</span>
|
1278
|
+
|
1279
|
+
|
1280
|
+
|
1281
|
+
—
|
1282
|
+
<div class='inline'>
|
1283
|
+
<p>being tested.</p>
|
1284
|
+
</div>
|
1285
|
+
|
1286
|
+
</li>
|
1287
|
+
|
1288
|
+
</ul>
|
1289
|
+
|
1290
|
+
<p class="tag_title">Returns:</p>
|
1291
|
+
<ul class="return">
|
1292
|
+
|
1293
|
+
<li>
|
1294
|
+
|
1295
|
+
|
1296
|
+
<span class='type'>(<tt>Boolean</tt>)</span>
|
1297
|
+
|
1298
|
+
|
1299
|
+
|
1300
|
+
—
|
1301
|
+
<div class='inline'>
|
1302
|
+
<p>true if location is within the boundaries of the board.</p>
|
1303
|
+
</div>
|
1304
|
+
|
1305
|
+
</li>
|
1306
|
+
|
1307
|
+
</ul>
|
1308
|
+
|
1309
|
+
</div><table class="source_code">
|
1310
|
+
<tr>
|
1311
|
+
<td>
|
1312
|
+
<pre class="lines">
|
1313
|
+
|
1314
|
+
|
1315
|
+
68
|
1316
|
+
69
|
1317
|
+
70</pre>
|
1318
|
+
</td>
|
1319
|
+
<td>
|
1320
|
+
<pre class="code"><span class="info file"># File 'lib/battlesnake/board.rb', line 68</span>
|
1321
|
+
|
1322
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_on_board?'>on_board?</span><span class='lparen'>(</span><span class='id identifier rubyid_location'>location</span><span class='rparen'>)</span>
|
1323
|
+
<span class='id identifier rubyid_location'>location</span><span class='period'>.</span><span class='id identifier rubyid_x'>x</span> <span class='op'>>=</span> <span class='int'>0</span> <span class='op'>&&</span> <span class='id identifier rubyid_location'>location</span><span class='period'>.</span><span class='id identifier rubyid_y'>y</span> <span class='op'>>=</span> <span class='int'>0</span> <span class='op'>&&</span> <span class='id identifier rubyid_location'>location</span><span class='period'>.</span><span class='id identifier rubyid_x'>x</span> <span class='op'><</span> <span class='id identifier rubyid_width'>width</span> <span class='op'>&&</span> <span class='id identifier rubyid_location'>location</span><span class='period'>.</span><span class='id identifier rubyid_y'>y</span> <span class='op'><</span> <span class='id identifier rubyid_height'>height</span>
|
1324
|
+
<span class='kw'>end</span></pre>
|
1325
|
+
</td>
|
1326
|
+
</tr>
|
1327
|
+
</table>
|
1328
|
+
</div>
|
1329
|
+
|
1330
|
+
<div class="method_details ">
|
1331
|
+
<h3 class="signature " id="paths-instance_method">
|
1332
|
+
|
1333
|
+
#<strong>paths</strong>(from, to) ⇒ <tt>Array<Path></tt>
|
1334
|
+
|
1335
|
+
|
1336
|
+
|
1337
|
+
|
1338
|
+
|
1339
|
+
</h3><div class="docstring">
|
1340
|
+
<div class="discussion">
|
1341
|
+
|
1342
|
+
<p>List of valid, consecutive paths from one location to the next. Paths may not:</p>
|
1343
|
+
|
1344
|
+
<pre class="code ruby"><code class="ruby">- wander outside board boundaries.
|
1345
|
+
- use the same location more than once.
|
1346
|
+
- contain occupied locations, EXCEPT the start/end locations.
|
1347
|
+
</code></pre>
|
1348
|
+
|
1349
|
+
<p>The exception for start/end locations allows us to generate paths, for example, from a snake to a food location, without having to calulate the starting/ending permutations ourselves.</p>
|
1350
|
+
|
1351
|
+
|
1352
|
+
</div>
|
1353
|
+
</div>
|
1354
|
+
<div class="tags">
|
1355
|
+
<p class="tag_title">Parameters:</p>
|
1356
|
+
<ul class="param">
|
1357
|
+
|
1358
|
+
<li>
|
1359
|
+
|
1360
|
+
<span class='name'>from</span>
|
1361
|
+
|
1362
|
+
|
1363
|
+
<span class='type'>(<tt><span class='object_link'><a href="Location.html" title="Battlesnake::Location (class)">Location</a></span></tt>)</span>
|
1364
|
+
|
1365
|
+
|
1366
|
+
|
1367
|
+
—
|
1368
|
+
<div class='inline'>
|
1369
|
+
<p>starting location, may be occupied</p>
|
1370
|
+
</div>
|
1371
|
+
|
1372
|
+
</li>
|
1373
|
+
|
1374
|
+
<li>
|
1375
|
+
|
1376
|
+
<span class='name'>to</span>
|
1377
|
+
|
1378
|
+
|
1379
|
+
<span class='type'>(<tt><span class='object_link'><a href="Location.html" title="Battlesnake::Location (class)">Location</a></span></tt>)</span>
|
1380
|
+
|
1381
|
+
|
1382
|
+
|
1383
|
+
—
|
1384
|
+
<div class='inline'>
|
1385
|
+
<p>starting location, may be occupied</p>
|
1386
|
+
</div>
|
1387
|
+
|
1388
|
+
</li>
|
1389
|
+
|
1390
|
+
</ul>
|
1391
|
+
|
1392
|
+
<p class="tag_title">Returns:</p>
|
1393
|
+
<ul class="return">
|
1394
|
+
|
1395
|
+
<li>
|
1396
|
+
|
1397
|
+
|
1398
|
+
<span class='type'>(<tt>Array<Path></tt>)</span>
|
1399
|
+
|
1400
|
+
|
1401
|
+
|
1402
|
+
—
|
1403
|
+
<div class='inline'>
|
1404
|
+
<p>a list of paths, which themselves are lists of consecutive, valid locations.</p>
|
1405
|
+
</div>
|
1406
|
+
|
1407
|
+
</li>
|
1408
|
+
|
1409
|
+
</ul>
|
1410
|
+
|
1411
|
+
</div><table class="source_code">
|
1412
|
+
<tr>
|
1413
|
+
<td>
|
1414
|
+
<pre class="lines">
|
1415
|
+
|
1416
|
+
|
1417
|
+
108
|
1418
|
+
109
|
1419
|
+
110</pre>
|
1420
|
+
</td>
|
1421
|
+
<td>
|
1422
|
+
<pre class="code"><span class="info file"># File 'lib/battlesnake/board.rb', line 108</span>
|
1423
|
+
|
1424
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_paths'>paths</span><span class='lparen'>(</span><span class='id identifier rubyid_from'>from</span><span class='comma'>,</span> <span class='id identifier rubyid_to'>to</span><span class='rparen'>)</span>
|
1425
|
+
|
1199
1426
|
<span class='kw'>end</span></pre>
|
1200
1427
|
</td>
|
1201
1428
|
</tr>
|
@@ -1207,7 +1434,7 @@
|
|
1207
1434
|
</div>
|
1208
1435
|
|
1209
1436
|
<div id="footer">
|
1210
|
-
Generated on
|
1437
|
+
Generated on Mon Nov 7 15:30:13 2022 by
|
1211
1438
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
1212
1439
|
0.9.28 (ruby-2.7.2).
|
1213
1440
|
</div>
|
data/docs/Battlesnake/Error.html
CHANGED
@@ -125,7 +125,7 @@
|
|
125
125
|
</div>
|
126
126
|
|
127
127
|
<div id="footer">
|
128
|
-
Generated on
|
128
|
+
Generated on Mon Nov 7 15:30:13 2022 by
|
129
129
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
130
130
|
0.9.28 (ruby-2.7.2).
|
131
131
|
</div>
|
data/docs/Battlesnake/Game.html
CHANGED
@@ -803,7 +803,7 @@
|
|
803
803
|
</div>
|
804
804
|
|
805
805
|
<div id="footer">
|
806
|
-
Generated on
|
806
|
+
Generated on Mon Nov 7 15:30:13 2022 by
|
807
807
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
808
808
|
0.9.28 (ruby-2.7.2).
|
809
809
|
</div>
|
@@ -1008,7 +1008,7 @@
|
|
1008
1008
|
</div>
|
1009
1009
|
|
1010
1010
|
<div id="footer">
|
1011
|
-
Generated on
|
1011
|
+
Generated on Mon Nov 7 15:30:13 2022 by
|
1012
1012
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
1013
1013
|
0.9.28 (ruby-2.7.2).
|
1014
1014
|
</div>
|
data/docs/Battlesnake/Snake.html
CHANGED
@@ -1352,7 +1352,7 @@
|
|
1352
1352
|
</div>
|
1353
1353
|
|
1354
1354
|
<div id="footer">
|
1355
|
-
Generated on
|
1355
|
+
Generated on Mon Nov 7 15:30:13 2022 by
|
1356
1356
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
1357
1357
|
0.9.28 (ruby-2.7.2).
|
1358
1358
|
</div>
|
data/docs/Battlesnake.html
CHANGED
@@ -145,7 +145,7 @@
|
|
145
145
|
</div>
|
146
146
|
|
147
147
|
<div id="footer">
|
148
|
-
Generated on
|
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
@@ -171,7 +171,7 @@
|
|
171
171
|
</div>
|
172
172
|
|
173
173
|
<div id="footer">
|
174
|
-
Generated on
|
174
|
+
Generated on Mon Nov 7 15:30:12 2022 by
|
175
175
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
176
176
|
0.9.28 (ruby-2.7.2).
|
177
177
|
</div>
|
data/docs/file.README.html
CHANGED
@@ -97,7 +97,7 @@
|
|
97
97
|
</div></div>
|
98
98
|
|
99
99
|
<div id="footer">
|
100
|
-
Generated on
|
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
|
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>
|
data/docs/method_list.html
CHANGED
@@ -62,16 +62,16 @@
|
|
62
62
|
|
63
63
|
<li class="odd ">
|
64
64
|
<div class="item">
|
65
|
-
<span class='object_link'><a href="Battlesnake/
|
66
|
-
<small>Battlesnake::
|
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
67
|
</div>
|
68
68
|
</li>
|
69
69
|
|
70
70
|
|
71
71
|
<li class="even ">
|
72
72
|
<div class="item">
|
73
|
-
<span class='object_link'><a href="Battlesnake/
|
74
|
-
<small>Battlesnake::
|
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
75
|
</div>
|
76
76
|
</li>
|
77
77
|
|
@@ -214,16 +214,16 @@
|
|
214
214
|
|
215
215
|
<li class="even ">
|
216
216
|
<div class="item">
|
217
|
-
<span class='object_link'><a href="Battlesnake/
|
218
|
-
<small>Battlesnake::
|
217
|
+
<span class='object_link'><a href="Battlesnake/Board.html#initialize-instance_method" title="Battlesnake::Board#initialize (method)">#initialize</a></span>
|
218
|
+
<small>Battlesnake::Board</small>
|
219
219
|
</div>
|
220
220
|
</li>
|
221
221
|
|
222
222
|
|
223
223
|
<li class="odd ">
|
224
224
|
<div class="item">
|
225
|
-
<span class='object_link'><a href="Battlesnake/
|
226
|
-
<small>Battlesnake::
|
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
227
|
</div>
|
228
228
|
</li>
|
229
229
|
|
@@ -292,6 +292,22 @@
|
|
292
292
|
</li>
|
293
293
|
|
294
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
|
+
|
295
311
|
<li class="even ">
|
296
312
|
<div class="item">
|
297
313
|
<span class='object_link'><a href="Battlesnake/Game.html#ruleset-instance_method" title="Battlesnake::Game#ruleset (method)">#ruleset</a></span>
|
@@ -100,7 +100,7 @@
|
|
100
100
|
</div>
|
101
101
|
|
102
102
|
<div id="footer">
|
103
|
-
Generated on
|
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>
|
data/lib/battlesnake/board.rb
CHANGED
@@ -41,37 +41,53 @@ module Battlesnake
|
|
41
41
|
end
|
42
42
|
|
43
43
|
##
|
44
|
-
# List of all occupied locations on the board; snakes,
|
44
|
+
# List of all occupied locations on the board; snakes, hazards, etc.
|
45
|
+
# Does NOT include food, since we don't want to avoid that.
|
45
46
|
#
|
46
47
|
# @return [Array<Location>] list of occupied locations
|
47
48
|
def occupied_locations
|
48
49
|
return @occupied_locations if defined?(@occupied_locations)
|
49
|
-
@occupied_locations = snakes.map(&:body).flatten +
|
50
|
+
@occupied_locations = snakes.map(&:body).flatten + hazards
|
50
51
|
end
|
51
52
|
|
52
53
|
##
|
53
54
|
# Whether the supplied location is occupied.
|
54
55
|
#
|
55
|
-
# @param
|
56
|
-
# hash containing x/y keys, JSON string of such a hash, or a pair of x,y coordinates expressed
|
57
|
-
# as a 2-element array or two separate parameters.
|
56
|
+
# @param [Location] location being checked for occupancy.
|
58
57
|
#
|
59
58
|
# @return [Boolean] true if location is occupied by snakes, food, hazards, etc.
|
60
|
-
def occupied?(
|
61
|
-
location = coordinates.first.is_a?(Location) ? coordinates.first : Location.new(*coordinates)
|
59
|
+
def occupied?(location)
|
62
60
|
occupied_locations.include?(location)
|
63
61
|
end
|
64
62
|
|
63
|
+
##
|
64
|
+
# Where the supplied location falls within the boundaries of the board.
|
65
|
+
#
|
66
|
+
# @param [Location] location being tested.
|
67
|
+
#
|
68
|
+
# @return [Boolean] true if location is within the boundaries of the board.
|
69
|
+
def on_board?(location)
|
70
|
+
location.x >= 0 && location.y >= 0 && location.x < width && location.y < height
|
71
|
+
end
|
72
|
+
|
65
73
|
##
|
66
74
|
# Whether the supplied location is available (unoccupied).
|
67
75
|
#
|
68
|
-
# @param
|
69
|
-
# hash containing x/y keys, JSON string of such a hash, or a pair of x,y coordinates expressed
|
70
|
-
# as a 2-element array or two separate parameters.
|
76
|
+
# @param [Location] location being tested for availability.
|
71
77
|
#
|
72
78
|
# @return [Boolean] true if location is available (unoccupied by snakes, food, hazards, etc).
|
73
|
-
def available?(
|
74
|
-
!occupied?(
|
79
|
+
def available?(location)
|
80
|
+
on_board?(location) && !occupied?(location)
|
81
|
+
end
|
82
|
+
|
83
|
+
##
|
84
|
+
# Whether the supplied location is food.
|
85
|
+
#
|
86
|
+
# @param [Location] location being tested for availability.
|
87
|
+
#
|
88
|
+
# @return [Boolean] true if location is food.
|
89
|
+
def food?(location)
|
90
|
+
food.include?(location)
|
75
91
|
end
|
76
92
|
|
77
93
|
##
|
@@ -85,5 +101,129 @@ module Battlesnake
|
|
85
101
|
available?(location.move(direction))
|
86
102
|
end
|
87
103
|
end
|
104
|
+
|
105
|
+
##
|
106
|
+
# List of valid, consecutive paths from one location to the next. Paths may not:
|
107
|
+
#
|
108
|
+
# - wander outside board boundaries.
|
109
|
+
# - use the same location more than once.
|
110
|
+
# - contain occupied locations, EXCEPT the start/end locations.
|
111
|
+
#
|
112
|
+
# The exception for start/end locations allows us to generate paths, for example, from a snake
|
113
|
+
# to a food location, without having to calulate the starting/ending permutations ourselves.
|
114
|
+
#
|
115
|
+
# @param from [Location] starting location, may be occupied
|
116
|
+
# @param to [Location] starting location, may be occupied
|
117
|
+
#
|
118
|
+
# @return [Array<Path>] a list of paths, which themselves are lists of consecutive, valid locations.
|
119
|
+
def find_path(from, to, max_distance: nil)
|
120
|
+
@paths = []
|
121
|
+
@ideal_path_size = from.distance(to) + 1
|
122
|
+
@shortest_path_size = max_distance || @ideal_path_size
|
123
|
+
@ideal_path_size_found = false
|
124
|
+
|
125
|
+
recursive_paths(from, to, [from])
|
126
|
+
|
127
|
+
@paths.select{ |path| path.size == @shortest_path_size }.first
|
128
|
+
end
|
129
|
+
|
130
|
+
def recursive_paths(from, to, path)
|
131
|
+
head = path.last
|
132
|
+
|
133
|
+
# give up if path is too long already.
|
134
|
+
return [] if path.size > @shortest_path_size || @ideal_path_size_found
|
135
|
+
|
136
|
+
# if we've made it to "to", we have a successful candidate path.
|
137
|
+
if head.as_json == to.as_json
|
138
|
+
@paths << path
|
139
|
+
@shortest_path_size = [@shortest_path_size, path.size].min
|
140
|
+
@ideal_path_size_found = true if path.size == @ideal_path_size
|
141
|
+
|
142
|
+
return path
|
143
|
+
end
|
144
|
+
|
145
|
+
available_directions(head).sort_by do |direction|
|
146
|
+
# prefer to continue in same direction
|
147
|
+
neck = path[-2]
|
148
|
+
|
149
|
+
if neck && neck.direction(head) == direction
|
150
|
+
0
|
151
|
+
else
|
152
|
+
rand
|
153
|
+
end
|
154
|
+
end.map do |direction|
|
155
|
+
# convert direction string to a location
|
156
|
+
head.move(direction)
|
157
|
+
end.map do |location|
|
158
|
+
# convert location to a full path
|
159
|
+
path + [location]
|
160
|
+
end.select do |candidate|
|
161
|
+
# don't allow paths that overlap themselves
|
162
|
+
candidate.size == candidate.uniq(&:as_json).size
|
163
|
+
end.each do |candidate|
|
164
|
+
# recurse into remaining candidate paths
|
165
|
+
recursive_paths(from, to, candidate)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
def shorty(location)
|
170
|
+
"#{location.x}:#{location.y}"
|
171
|
+
end
|
172
|
+
|
173
|
+
def shorties(list)
|
174
|
+
case list.first
|
175
|
+
when Array
|
176
|
+
list.map{ |l| shorties(l) }
|
177
|
+
when Location
|
178
|
+
list.map{|x| shorty(x)}.join(' ')
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
def print_grid(path, prefix: ' ')
|
183
|
+
max_x = path.map(&:x).max
|
184
|
+
max_y = path.map(&:y).max
|
185
|
+
|
186
|
+
(0..max_y).each do |row|
|
187
|
+
y = max_y - row
|
188
|
+
|
189
|
+
cols = (0..max_x).map do |x|
|
190
|
+
loc = Location.new(x, y)
|
191
|
+
|
192
|
+
if path.include?(loc)
|
193
|
+
after = path.index{ |l| l.as_json == loc.as_json} + 1
|
194
|
+
|
195
|
+
if after >= path.size
|
196
|
+
"\u00d7"
|
197
|
+
elsif loc.as_json == path.first.as_json
|
198
|
+
case loc.direction(path[after])
|
199
|
+
when 'up'
|
200
|
+
"\u21a5"
|
201
|
+
when 'down'
|
202
|
+
"\u21a7"
|
203
|
+
when 'left'
|
204
|
+
"\u21a4"
|
205
|
+
when 'right'
|
206
|
+
"\u21a6"
|
207
|
+
end
|
208
|
+
else
|
209
|
+
case loc.direction(path[after])
|
210
|
+
when 'up'
|
211
|
+
"\u2191"
|
212
|
+
when 'down'
|
213
|
+
"\u2193"
|
214
|
+
when 'left'
|
215
|
+
"\u2190"
|
216
|
+
when 'right'
|
217
|
+
"\u2192"
|
218
|
+
end
|
219
|
+
end
|
220
|
+
else
|
221
|
+
'O'
|
222
|
+
end
|
223
|
+
end.join(' ')
|
224
|
+
|
225
|
+
puts "#{prefix} #{cols}"
|
226
|
+
end
|
227
|
+
end
|
88
228
|
end
|
89
229
|
end
|
data/lib/battlesnake/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: battlesnake
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jaime Bellmyer
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-01-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|