public static void Main(string[] args) { // Store the original console state so we can restore it after the game is over var originalTitle = Console.Title; var originalColor = Console.ForegroundColor; var originalCursorVisible = Console.CursorVisible; // I pressed ctrl + c to stop the game too many times... Console.CancelKeyPress += delegate { Console.Title = originalTitle; Console.CursorVisible = originalCursorVisible; Console.ForegroundColor = originalColor; Console.Clear(); }; // Prepare the console Console.Clear(); // Lets clear the console window Console.CursorVisible = false; Console.Title = "Westerdals Oslo ACT - SNAKE"; Console.ForegroundColor = ConsoleColor.Yellow; // Create a gameboard and start the game var gameBoard = new SnakeBoard(Console.WindowWidth, Console.WindowHeight, RenderingFactory.createRenderer()); gameBoard.startGame(); // Set the cursor visible back to the original value to leave the console in a usable state... Console.ResetColor(); Console.Title = originalTitle; Console.ForegroundColor = originalColor; Console.CursorVisible = originalCursorVisible; Console.Clear(); }
public SnakeEngine(SnakeBoard snakeBoard) { Board = snakeBoard; SnakeObj = new SnakeObj(); SnPoints = new List <SnakePoints>(); SnakePointGenerator = new SnakePointGenerator(); }
private void SnakeMoveSwitch(object sender, KeyEventArgs e) { switch (e.Key) { case Key.Up: if (SnakeBoard.GetCurrentSnakeDirection() != DirectionFlag.Up) { SnakeBoard.ChangeSnakeDirection(DirectionFlag.Up); } break; case Key.Down: if (SnakeBoard.GetCurrentSnakeDirection() != DirectionFlag.Down) { SnakeBoard.ChangeSnakeDirection(DirectionFlag.Down); } break; case Key.Left: if (SnakeBoard.GetCurrentSnakeDirection() != DirectionFlag.Left) { SnakeBoard.ChangeSnakeDirection(DirectionFlag.Left); } break; case Key.Right: if (SnakeBoard.GetCurrentSnakeDirection() != DirectionFlag.Right) { SnakeBoard.ChangeSnakeDirection(DirectionFlag.Right); } break; } }
static int DFSScore(SnakeBoard currentBoard) { int score = DFSScore(currentBoard, 0); Console.WriteLine("Nodes searched: " + nodeCount); nodeCount = 0; return(score); }
private SnakeWindow() { BackgroundImage = new BackgroundImage(); Configuration = new Configuration(); InitializeComponent(); SnakeBoard = new SnakeBoard(this, 40, 40, 20); TimerInitialization(); }
public SnakeBoard Run(SnakeBoard snakeBoard) { Board = snakeBoard; PointToGenerate = snakeBoard.SnakeTimer.PointToGenerate; UpdateBoard(); UpdateSnake(); UpdatePointsOnBoard(); return(Board); }
static string DecideDirection(SnakeBoard board) { //List<MoveScore> movesFreeDeg = CalcMoveFreeDegs(board); return(BestDFSMove(board, game.GetPossibleMoves(board).playerMoves)); /* * for (int i = 4; i >= 1; i--) { * List<MoveScore> goodMoves = movesFreeDeg.Where(x => (x.score == i || x.score == i-1)).ToList(); * if (goodMoves.Count > 0) * return BestDFSMove(board, goodMoves); * } * return movesFreeDeg[0].dir; */ }
static List <MoveScore> CalcMoveFreeDegs(SnakeBoard board) { PossibleMoves possMoves = game.GetPossibleMoves(board); List <MoveScore> moves = new List <MoveScore>(3); foreach (string direction in possMoves.playerMoves) { moves.Add(new MoveScore() { dir = direction, score = DegreesOfFreedom(board, direction) }); } return(moves); }
static string DecideMove(SnakeBoard board) { board.PrintBoard("My current board"); // Since this gives a struct with both playerMoves and enemyMoves we specify playerMoves. List <string> possibleMoves = game.GetPossibleMoves(board).playerMoves; int randomIndex = rnd.Next(possibleMoves.Count); string randomMove = possibleMoves[randomIndex]; SnakeBoard simBoard = game.SimulatePlayerMove(board, randomMove); simBoard.PrintBoard("My simulated board"); Console.WriteLine(game.EvaluateBoard(simBoard)); return(randomMove); }
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); }
static int DFSScore(SnakeBoard currentBoard, int depth) { nodeCount++; BoardState currentState = game.EvaluateBoard(currentBoard); if (depth == maxDepth || currentState != BoardState.ongoing) { return(depth); } List <MoveScore> moveScores = new List <MoveScore>(); foreach (string move in game.GetPossibleMoves(currentBoard).playerMoves) { SnakeBoard simBoard = game.SimulatePlayerMove(currentBoard, move); int score = DFSScore(simBoard, depth + 1); if (score == maxDepth) { return(score); } moveScores.Add(new MoveScore(move, score)); } return(moveScores.Max(x => x.score)); }
private static int CellFree(SnakeBoard board, int x, int y) { return(Convert.ToInt32(!board.CellBlocked(x, y))); }
private void GameLogicController(object sender, EventArgs e) { SnakeBoard.UpdateSnakePositionLogic(SnakeBoard); }