/// <summary> /// Initializes game board and sets starting/ending player position /// </summary> /// <param name="gameBoardPath">Location of a full path to a game board</param> /// <param name="endSolution">Solution of a board (e.g. ABCD)</param> private void LoadBoard(string gameBoardPath, string endSolution) { // Initialize game board, player start/end position and set board solution boardDefinition = BoardLoader.LoadGameBoard(gameBoardPath); // Set properties not populated with LoadGameBoard method boardDefinition.BoardWordSolution = endSolution; boardDefinition.BoardTunnels = FindTunnels(boardDefinition); }
/// <summary> /// Displays game board to a Console window /// </summary> /// <param name="boardDefinition">Properties of a game board</param> /// <param name="currentWord">Current Word (part of a solution)</param> /// <param name="pathWord">Current Path</param> public static void DisplayBoard(GameBoardDefinition boardDefinition, string currentWord, string pathWord) { Console.Clear(); for (var y = 0; y < boardDefinition.BoardHeight; y++) { for (var x = 0; x < boardDefinition.BoardWidth; x++) { char c = boardDefinition.BoardData[x, y]; Console.ForegroundColor = ConsoleColor.Gray; ConsoleColor cColor; switch (boardDefinition.BoardSolution[x, y]) { case false: default: cColor = ConsoleColor.Black; break; case true: cColor = ConsoleColor.Yellow; break; } //if (boardDefinition.BoardTunnels[x, y]) // cColor = ConsoleColor.Red; Console.BackgroundColor = cColor; Console.Write(c); if (x + 1 == boardDefinition.BoardWidth) { Console.Write(Environment.NewLine); } } } Console.ForegroundColor = ConsoleColor.White; Console.BackgroundColor = ConsoleColor.Black; Console.WriteLine(); Console.WriteLine($"WORD: {currentWord}"); Console.WriteLine($"PATH: {pathWord}"); System.Threading.Thread.Sleep(10); }
/// <summary> /// Initializes new instance of Backtracking solver class and populates properties taken from boardDefinition /// </summary> /// <param name="boardDefinition">Properties of a game board</param> public BacktrackingSolver(GameBoardDefinition boardDefinition) { this.boardDefinition = boardDefinition; }
/// <summary> /// Determines whether current X,Y position is withing game board bounds /// </summary> private bool AreValidBounds(int x, int y, GameBoardDefinition boardDefinition) { return(Utils.AreValidBounds(x, y, boardDefinition.BoardWidth, boardDefinition.BoardHeight)); }
/// <summary> /// Finds "tunnels" in a game board, vertical path solution can cross horizontal path solution, and vice versa /// </summary> /// <param name="boardDefinition"></param> /// <returns></returns> private bool[,] FindTunnels(GameBoardDefinition boardDefinition) { int boardWidth = boardDefinition.BoardWidth; int boardHeight = boardDefinition.BoardHeight; char[,] gameBoard = boardDefinition.BoardData; bool[,] tunnels = new bool[boardWidth, boardHeight]; bool AreTunnelBoundsValid(int x, int y, bool checkVertical) { if (checkVertical) { return(AreValidBounds(x, y - 1, boardDefinition) && AreValidBounds(x, y + 1, boardDefinition) && gameBoard[x, y - 1] == (char)BoardLetterDefinition.MoveVertical && gameBoard[x, y + 1] == (char)BoardLetterDefinition.MoveVertical); } else { return(AreValidBounds(x - 1, y, boardDefinition) && AreValidBounds(x + 1, y, boardDefinition) && gameBoard[x - 1, y] == (char)BoardLetterDefinition.MoveHorizontal && gameBoard[x + 1, y] == (char)BoardLetterDefinition.MoveHorizontal); } } for (var y = 0; y < boardHeight; y++) { for (var x = 0; x < boardWidth; x++) { char c = gameBoard[x, y]; if (c == (char)BoardLetterDefinition.MoveHorizontal || c == (char)BoardLetterDefinition.MoveVertical) { switch (c) { case (char)BoardLetterDefinition.MoveHorizontal: if (AreTunnelBoundsValid(x, y, true)) { tunnels[x, y] = true; } break; case (char)BoardLetterDefinition.MoveVertical: if (AreTunnelBoundsValid(x, y, false)) { tunnels[x, y] = true; } break; } } else if (ALPHABET.Contains(c)) { if (AreTunnelBoundsValid(x, y, true) && AreTunnelBoundsValid(x, y, false)) { tunnels[x, y] = true; } } } } return(tunnels); }
private void Solver_PlayerPositionChanged(GameBoardDefinition gameBoard, string currentWord, string pathWord) { PlayerPositionChanged(gameBoard, currentWord, pathWord); }
private static void Solver_PlayerPositionChanged(GameBoardDefinition gameBoard, string currentWord, string pathWord) { // Display game board with a current solution to a Console window GameBoardPresenter.DisplayBoard(gameBoard, currentWord, pathWord); }