@@ -213,3 +213,118 @@ func TestFindFood(t *testing.T) {
213
213
})
214
214
}
215
215
}
216
+
217
+ // 30 31 32 33 34
218
+ // 20 21 22 23 24
219
+ // 10 11 12 13 14
220
+ // 00 01 02 03 04
221
+
222
+ func TestAvoidCollisions (t * testing.T ) {
223
+ type testcase struct {
224
+ name string
225
+ inputSelf internal.Battlesnake
226
+ inputOthers []internal.Battlesnake
227
+ expected []string
228
+ }
229
+
230
+ testcases := []testcase {
231
+ {
232
+ name : "other snake longer" ,
233
+ inputSelf : internal.Battlesnake {
234
+ ID : "me" ,
235
+ Head : internal.Coord {X : 0 , Y : 1 },
236
+ Body : []internal.Coord {
237
+ {X : 0 , Y : 1 },
238
+ {X : 0 , Y : 2 },
239
+ {X : 0 , Y : 3 },
240
+ },
241
+ Length : 3 ,
242
+ },
243
+ inputOthers : []internal.Battlesnake {
244
+ {
245
+ ID : "other1" ,
246
+ Head : internal.Coord {X : 1 , Y : 0 },
247
+ Body : []internal.Coord {
248
+ {X : 1 , Y : 0 },
249
+ {X : 2 , Y : 0 },
250
+ {X : 3 , Y : 0 },
251
+ {X : 4 , Y : 0 },
252
+ {X : 5 , Y : 0 },
253
+ },
254
+ Length : 5 ,
255
+ },
256
+ },
257
+ expected : []string {"left" , "up" },
258
+ },
259
+ {
260
+ name : "other snake shorter" ,
261
+ inputSelf : internal.Battlesnake {
262
+ ID : "me" ,
263
+ Head : internal.Coord {X : 0 , Y : 1 },
264
+ Body : []internal.Coord {
265
+ {X : 0 , Y : 0 },
266
+ {X : 0 , Y : 1 },
267
+ {X : 0 , Y : 2 },
268
+ },
269
+ Length : 3 ,
270
+ },
271
+ inputOthers : []internal.Battlesnake {
272
+ {
273
+ ID : "other1" ,
274
+ Head : internal.Coord {X : 1 , Y : 0 },
275
+ Body : []internal.Coord {
276
+ {X : 1 , Y : 0 },
277
+ {X : 2 , Y : 0 },
278
+ },
279
+ Length : 2 ,
280
+ },
281
+ },
282
+ expected : []string {"down" , "left" , "right" , "up" },
283
+ },
284
+ {
285
+ name : "multiple snakes" ,
286
+ inputSelf : internal.Battlesnake {
287
+ ID : "me" ,
288
+ Head : internal.Coord {X : 3 , Y : 1 },
289
+ Body : []internal.Coord {
290
+ {X : 3 , Y : 3 },
291
+ {X : 3 , Y : 2 },
292
+ {X : 3 , Y : 1 },
293
+ },
294
+ Length : 3 ,
295
+ },
296
+ inputOthers : []internal.Battlesnake {
297
+ {
298
+ ID : "other1" ,
299
+ Head : internal.Coord {X : 2 , Y : 0 },
300
+ Body : []internal.Coord {
301
+ {X : 2 , Y : 0 },
302
+ {X : 1 , Y : 0 },
303
+ {X : 0 , Y : 0 },
304
+ },
305
+ Length : 3 ,
306
+ },
307
+ {
308
+ ID : "other2" ,
309
+ Head : internal.Coord {X : 5 , Y : 1 },
310
+ Body : []internal.Coord {
311
+ {X : 5 , Y : 1 },
312
+ {X : 6 , Y : 1 },
313
+ {X : 7 , Y : 1 },
314
+ },
315
+ Length : 3 ,
316
+ },
317
+ },
318
+ expected : []string {"up" },
319
+ },
320
+ }
321
+
322
+ for _ , tc := range testcases {
323
+ t .Run (tc .name , func (t * testing.T ) {
324
+ is := is .New (t )
325
+ actual := AvoidCollisions (tc .inputSelf , tc .inputOthers )
326
+ sort .Strings (actual )
327
+ is .Equal (actual , tc .expected )
328
+ })
329
+ }
330
+ }
0 commit comments