-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSnakeEngine.cs
52 lines (45 loc) · 1.44 KB
/
SnakeEngine.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.Linq;
namespace SnakeWars.SampleBot
{
internal class SnakeEngine
{
private readonly string _mySnakeId;
private readonly Random _random = new Random();
public SnakeEngine(string mySnakeId)
{
_mySnakeId = mySnakeId;
}
public Move GetNextMove(GameBoardState gameBoardState)
{
//===========================
// Your snake logic goes here
//===========================
var mySnake = gameBoardState.GetSnake(_mySnakeId);
if (mySnake.IsAlive)
{
var occupiedCells = gameBoardState.GetOccupiedCells();
// Check possible moves in random order.
var moves = new List<Move>
{
Move.Left,
Move.Right,
Move.Straight
};
while (moves.Any())
{
// Select random move.
var move = moves[_random.Next(moves.Count)];
moves.Remove(move);
var newHead = gameBoardState.GetSnakeNewHeadPosition(_mySnakeId, move);
if (!occupiedCells.Contains(newHead))
{
return move;
}
}
}
return Move.None;
}
}
}