static int DegreesOfFreedom(SnakeBoard board, string direction) { int x = board.GetPlayerPosition().x; int y = board.GetPlayerPosition().y; int newX, newY; switch (direction) { case "right": newX = x + 1; if (board.CellBlocked(newX, y)) { return(0); } return(1 + CellFree(board, newX + 1, y) + CellFree(board, newX, y + 1) + CellFree(board, newX, y - 1)); case "left": newX = x - 1; if (board.CellBlocked(newX, y)) { return(0); } return(1 + CellFree(board, newX - 1, y) + CellFree(board, newX, y + 1) + CellFree(board, newX, y - 1)); case "down": newY = y + 1; if (board.CellBlocked(x, newY)) { return(0); } return(1 + CellFree(board, x, newY + 1) + CellFree(board, x + 1, newY) + CellFree(board, x - 1, newY)); default: // up newY = y - 1; if (board.CellBlocked(x, newY)) { return(0); } return(1 + CellFree(board, x, newY - 1) + CellFree(board, x + 1, newY) + CellFree(board, x - 1, newY)); } }
static string BestDFSMove(SnakeBoard board, List <string> moves) { Console.WriteLine("Doing DFS..."); // Order such that direction away from player is searched first moves = moves.OrderByDescending(x => NextSquareDistanceFromEnemy( board.GetPlayerPosition(), board.GetEnemyPosition(), x, board.GetEnemyDirection())).ToList(); MoveScore bestMove = new MoveScore(moves[0], DFSScore(game.SimulatePlayerMove(board, moves[0]))); for (int i = 1; i < moves.Count; i++) { int score = DFSScore(game.SimulatePlayerMove(board, moves[i])); if (score > bestMove.score) { bestMove.dir = moves[i]; bestMove.score = score; } } Console.WriteLine("Done DFS! Max depth: " + bestMove.score); return(bestMove.dir); }