public static void outputStatesFromPath(List<Vector2> path, GameBoard gameBoard) { GameBoard board = gameBoard; foreach (Vector2 move in path) { board = new GameBoard(board.Board, board.Dimensions, board.Goal); board.playerPosition = move; gameBoard.printGrid(board.Board, gameBoard.Dimensions); Console.WriteLine(); } }
public void TestIsValidPlayerPosition() { int[,] board1 = new int[,] { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 1, 2, 1 }, { 0, 0, 0, 0 } }; GameBoard gameBoard1 = new GameBoard(board1, new MazeOfLife.Vector2(4, 4), new Vector2(1, 1)); int[,] board2 = new int[,] { { 0, 0, 0, 0 }, { 0, 1, 1, 1 }, { 0, 1, 2, 1 }, { 0, 1, 1, 1 } }; GameBoard gameBoard2 = new GameBoard(board2, new MazeOfLife.Vector2(4, 4), new Vector2(1, 1)); Assert.IsTrue(gameBoard1.isValidPlayerPosition(new Vector2(3, 2))); Assert.IsFalse(gameBoard2.isValidPlayerPosition(new Vector2(2, 2))); }
public void TestIsGoal() { int[,] board = new int[5, 5]; GameBoard gameBoard = new GameBoard(board, new MazeOfLife.Vector2(5, 5), new Vector2(1, 1)); for (int i = 1; i < 5; i++) { for (int j = 1; j < 5; j++) { if (i == 1 && j == 1) { Assert.IsTrue(gameBoard.isGoal(new Vector2(i, j))); } else { Assert.IsFalse(gameBoard.isGoal(new Vector2(i, j))); } } } }
static void Main(string[] args) { if (args.Length > 0) { GameBoard gameBoard = new GameBoard(args[0]); List<Vector2> path = GameBoardUtils.Search.BFTS(gameBoard); Console.WriteLine("Maze - Goal("+gameBoard.Goal.X+","+gameBoard.Goal.Y+"): "); gameBoard.print(); Console.WriteLine("\nPath:"); GameBoardUtils.outputPath(path, gameBoard); Console.WriteLine("\nPath cost: " + (path.Count - 1)); Console.WriteLine("States: \n"); GameBoardUtils.outputStatesFromPath(path, gameBoard); Console.WriteLine("\nPress any key to close"); Console.ReadKey(); //wait for user input to exit } }
public static List<Vector2> BFTS(GameBoard gameBoard) { Vector2 playerPosition; bool[,] visited = new bool[gameBoard.Dimensions.X, gameBoard.Dimensions.Y]; Vector2[,] previousPosition = new Vector2[gameBoard.Dimensions.X, gameBoard.Dimensions.Y]; try { playerPosition = gameBoard.playerPosition; } catch (PlayerNotFoundException) { Console.WriteLine("Invalid game board, player not found!"); return null; } if (gameBoard.isGoal(playerPosition)) { List<Vector2> pathToGoal = new List<Vector2>(); pathToGoal.Add(playerPosition); return pathToGoal; } Queue<Vector2> moveQueue = new Queue<Vector2>(); moveQueue.Enqueue(playerPosition); visited[playerPosition.X, playerPosition.Y] = true; while (moveQueue.Count > 0) { Vector2 move = moveQueue.Dequeue(); foreach (Vector2 newPosition in gameBoard.getAllDirections(move)) { if (gameBoard.isValidPlayerPosition(newPosition) && !visited[newPosition.X, newPosition.Y]) { visited[newPosition.X, newPosition.Y] = true; previousPosition[newPosition.X, newPosition.Y] = move; if (gameBoard.isGoal(newPosition)) { //when goal is found retrace path from goal to start List<Vector2> pathToGoal = new List<Vector2>(); Vector2 lastPosition = newPosition; while (lastPosition.Equals(playerPosition) == false) { pathToGoal.Add(lastPosition); lastPosition = previousPosition[lastPosition.X, lastPosition.Y]; } pathToGoal.Add(lastPosition); pathToGoal.Reverse(); return pathToGoal; } else { moveQueue.Enqueue(newPosition); } } } } Console.WriteLine("No solution could be found!"); return null; }
public void TestGetCellAt() { int[,] board = new int[5, 5]; for (int i = 1; i < 5; i++) { for (int j = 1; j < 5; j++) { board[i, j] = (i + j) % 2; } } GameBoard gameBoard = new GameBoard(board, new MazeOfLife.Vector2(5, 5), new Vector2(1, 1)); for (int i = 1; i < 5; i++) { for (int j = 1; j < 5; j++) { int cell = gameBoard.GetCellAt(new Vector2(i, j)); if ((i + j) % 2 == 1) { Assert.IsTrue(cell == 1); } else { Assert.IsFalse(cell == 1); } } } }
public static void outputPath(List<Vector2> path, GameBoard gameBoard) { foreach (Vector2 move in path) { Console.WriteLine(move.ToString()); } }