battlesnake 0.1.2 → 0.1.3

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: b2100d8f190752e432572971a16b1ad7de7ee941008df10df475084d9851cb8e
4
- data.tar.gz: 17435dbdad3f48cb3f99591af60b2f471dcafca058221146b534754a09b6be13
3
+ metadata.gz: b89d9fd8df0aef295a5b8b854347b633550daf06ec2327c9f0fbd7b220eaace4
4
+ data.tar.gz: 9eb544e850f0f4267519e9b802db9dce508f7a8eeb9d6d9cbc19463a0596f2e1
5
5
  SHA512:
6
- metadata.gz: 34023efce3a589e86f65a30f983b93f9c24d745faebbaf05ef4db31d0a695be641665f282f45fe6d84a0c35462c4b7d67a91ae67bee11af0842fa67cf71536f9
7
- data.tar.gz: 8565f7f891d1c59be120f9b002e2f2c2242ef38b68dae5e0798da735a3353f83d8cd1ec232203e3515845b3380690f378a744851671ffa2b1c9de0af8cec63d8
6
+ metadata.gz: 48edb7affb7b6ff01e943b5cd4c9c87c8b6284b281f256bddda9412c1b371ab8ccdeb504fd5de469e830f789ea54fe66ae36c768e079f0f0757161bd88cac855
7
+ data.tar.gz: 86dbedc0c782be217c6220de2de438606fb8b006d6f0661b5da08c9f68dc967eaf116bc1901ae8451c55c3bb303d20287ab220762115cff4d62f8dfb90948e87
data/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # Battlesnake
2
2
 
3
+ ## 0.1.3 (2022-11-07)
4
+
5
+ ### Added
6
+
7
+ - Board
8
+ - #on_board?(location) returns true if location is within board boundaries.
9
+ - #paths(from, to) returns all valid paths from one location to the next.
10
+
11
+ ### Changed
12
+
13
+ - Board
14
+ - #occupied?(location) now only accepts a location object.
15
+ - #available?(location) now only accepts a location object.
16
+ - locations must be both unoccupied AND on_board to be considered available.
17
+
3
18
  ## 0.1.2 (2022-11-05)
4
19
 
5
20
  ### Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- battlesnake (0.1.2)
4
+ battlesnake (0.1.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -241,7 +241,7 @@
241
241
  </div>
242
242
 
243
243
  <div id="footer">
244
- Generated on Sat Nov 5 20:09:02 2022 by
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>
@@ -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>(*coordinates) &#x21d2; Boolean </a>
312
+ <a href="#available%3F-instance_method" title="#available? (instance method)">#<strong>available?</strong>(location) &#x21d2; 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>(*coordinates) &#x21d2; Boolean </a>
386
+ <a href="#occupied%3F-instance_method" title="#occupied? (instance method)">#<strong>occupied?</strong>(location) &#x21d2; 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) &#x21d2; 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) &#x21d2; Array&lt;Path&gt; </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>(*coordinates) &#x21d2; <tt>Boolean</tt>
952
+ #<strong>available?</strong>(location) &#x21d2; <tt>Boolean</tt>
905
953
 
906
954
 
907
955
 
@@ -921,16 +969,16 @@
921
969
 
922
970
  <li>
923
971
 
924
- <span class='name'>*coordinates</span>
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>, <tt>Hash</tt>, <tt>String</tt>, <tt>Array</tt>)</span>
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
  &mdash;
932
980
  <div class='inline'>
933
- <p>can be specified as a <em>Location</em> object, hash containing x/y keys, JSON string of such a hash, or a pair of x,y coordinates expressed as a 2-element array or two separate parameters.</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
- 73
966
- 74
967
- 75</pre>
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 73</span>
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='op'>*</span><span class='id identifier rubyid_coordinates'>coordinates</span><span class='rparen'>)</span>
973
- <span class='op'>!</span><span class='id identifier rubyid_occupied?'>occupied?</span><span class='lparen'>(</span><span class='op'>*</span><span class='id identifier rubyid_coordinates'>coordinates</span><span class='rparen'>)</span>
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'>&amp;&amp;</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
- 83
1045
- 84
1046
- 85
1047
- 86
1048
- 87</pre>
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 83</span>
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>(*coordinates) &#x21d2; <tt>Boolean</tt>
1114
+ #<strong>occupied?</strong>(location) &#x21d2; <tt>Boolean</tt>
1067
1115
 
1068
1116
 
1069
1117
 
@@ -1083,16 +1131,16 @@
1083
1131
 
1084
1132
  <li>
1085
1133
 
1086
- <span class='name'>*coordinates</span>
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>, <tt>Hash</tt>, <tt>String</tt>, <tt>Array</tt>)</span>
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
  &mdash;
1094
1142
  <div class='inline'>
1095
- <p>can be specified as a <em>Location</em> object, hash containing x/y keys, JSON string of such a hash, or a pair of x,y coordinates expressed as a 2-element array or two separate parameters.</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
- 60
1128
- 61
1129
- 62
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 60</span>
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='op'>*</span><span class='id identifier rubyid_coordinates'>coordinates</span><span class='rparen'>)</span>
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'>&amp;</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) &#x21d2; <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
+ &mdash;
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
+ &mdash;
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'>&gt;=</span> <span class='int'>0</span> <span class='op'>&amp;&amp;</span> <span class='id identifier rubyid_location'>location</span><span class='period'>.</span><span class='id identifier rubyid_y'>y</span> <span class='op'>&gt;=</span> <span class='int'>0</span> <span class='op'>&amp;&amp;</span> <span class='id identifier rubyid_location'>location</span><span class='period'>.</span><span class='id identifier rubyid_x'>x</span> <span class='op'>&lt;</span> <span class='id identifier rubyid_width'>width</span> <span class='op'>&amp;&amp;</span> <span class='id identifier rubyid_location'>location</span><span class='period'>.</span><span class='id identifier rubyid_y'>y</span> <span class='op'>&lt;</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) &#x21d2; <tt>Array&lt;Path&gt;</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
+ &mdash;
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
+ &mdash;
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&lt;Path&gt;</tt>)</span>
1399
+
1400
+
1401
+
1402
+ &mdash;
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 Sat Nov 5 20:09:02 2022 by
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>
@@ -125,7 +125,7 @@
125
125
  </div>
126
126
 
127
127
  <div id="footer">
128
- Generated on Sat Nov 5 20:09:02 2022 by
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>
@@ -803,7 +803,7 @@
803
803
  </div>
804
804
 
805
805
  <div id="footer">
806
- Generated on Sat Nov 5 20:09:02 2022 by
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 Sat Nov 5 20:09:02 2022 by
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>
@@ -1352,7 +1352,7 @@
1352
1352
  </div>
1353
1353
 
1354
1354
  <div id="footer">
1355
- Generated on Sat Nov 5 20:09:02 2022 by
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>
@@ -145,7 +145,7 @@
145
145
  </div>
146
146
 
147
147
  <div id="footer">
148
- Generated on Sat Nov 5 20:09:02 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
@@ -171,7 +171,7 @@
171
171
  </div>
172
172
 
173
173
  <div id="footer">
174
- Generated on Sat Nov 5 20:09:02 2022 by
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>
@@ -97,7 +97,7 @@
97
97
  </div></div>
98
98
 
99
99
  <div id="footer">
100
- Generated on Sat Nov 5 20:09:02 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 Sat Nov 5 20:09:02 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>
@@ -62,16 +62,16 @@
62
62
 
63
63
  <li class="odd ">
64
64
  <div class="item">
65
- <span class='object_link'><a href="Battlesnake/Snake.html#as_json-instance_method" title="Battlesnake::Snake#as_json (method)">#as_json</a></span>
66
- <small>Battlesnake::Snake</small>
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/Game.html#as_json-instance_method" title="Battlesnake::Game#as_json (method)">#as_json</a></span>
74
- <small>Battlesnake::Game</small>
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/Location.html#initialize-instance_method" title="Battlesnake::Location#initialize (method)">#initialize</a></span>
218
- <small>Battlesnake::Location</small>
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/Board.html#initialize-instance_method" title="Battlesnake::Board#initialize (method)">#initialize</a></span>
226
- <small>Battlesnake::Board</small>
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 Sat Nov 5 20:09:02 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>
@@ -52,26 +52,31 @@ module Battlesnake
52
52
  ##
53
53
  # Whether the supplied location is occupied.
54
54
  #
55
- # @param *coordinates [Location,Hash,String,Array] can be specified as a _Location_ object,
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.
55
+ # @param [Location] location being checked for occupancy.
58
56
  #
59
57
  # @return [Boolean] true if location is occupied by snakes, food, hazards, etc.
60
- def occupied?(*coordinates)
61
- location = coordinates.first.is_a?(Location) ? coordinates.first : Location.new(*coordinates)
58
+ def occupied?(location)
62
59
  occupied_locations.include?(location)
63
60
  end
64
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
+
65
72
  ##
66
73
  # Whether the supplied location is available (unoccupied).
67
74
  #
68
- # @param *coordinates [Location,Hash,String,Array] can be specified as a _Location_ object,
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.
75
+ # @param [Location] location being tested for availability.
71
76
  #
72
77
  # @return [Boolean] true if location is available (unoccupied by snakes, food, hazards, etc).
73
- def available?(*coordinates)
74
- !occupied?(*coordinates)
78
+ def available?(location)
79
+ on_board?(location) && !occupied?(location)
75
80
  end
76
81
 
77
82
  ##
@@ -85,5 +90,129 @@ module Battlesnake
85
90
  available?(location.move(direction))
86
91
  end
87
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
88
217
  end
89
218
  end
@@ -1,4 +1,4 @@
1
1
  module Battlesnake
2
2
  # The current version of the driver.
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  end
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.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaime Bellmyer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-11-06 00:00:00.000000000 Z
11
+ date: 2023-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec