/// <summary> /// Changes the game board according to the hit of mine with size five. /// </summary> /// <param name="gameBoard">the game board it is played on</param> /// <param name="row">the row coordinate of the field</param> /// <param name="col">the col coordinate of the field</param> private static void HitMineOfSizeFive(Board gameBoard, int row, int col) { int rows = gameBoard.Rows; int cols = gameBoard.Cols; HitMineOfSizeFour(gameBoard, row, col); if (row - 2 >= 0 && col - 2 >= 0) { gameBoard.GameBoard[row - 2, col - 2] = "X"; } if (row - 2 >= 0 && col + 2 < cols) { gameBoard.GameBoard[row - 2, col + 2] = "X"; } if (row + 2 < rows && col + 2 < cols) { gameBoard.GameBoard[row + 2, col + 2] = "X"; } if (row + 2 < rows && col - 2 >= 0) { gameBoard.GameBoard[row + 2, col - 2] = "X"; } }
/// <summary> /// Main Method. Begining of the program. /// </summary> public static void Main(string[] args) { ConsoleUI userInput = new ConsoleUI(); int gameBoardSize = GameEngine.ReadBoardSize(userInput); Board board = new Board(gameBoardSize); Console.WriteLine(board); GameEngine.PlayGame(board, userInput); }
/// <summary> /// Check if the game has ended. /// </summary> /// <param name="gameBoard">the game board it is played on</param> /// <returns>whether the game has come to an end</returns> public static bool IsGameOver(Board gameBoard) { for (int i = 0; i < gameBoard.Rows; i++) { for (int j = 0; j < gameBoard.Cols; j++) { if (gameBoard.GameBoard[i, j] == "1" || gameBoard.GameBoard[i, j] == "2" || gameBoard.GameBoard[i, j] == "3" || gameBoard.GameBoard[i, j] == "4" || gameBoard.GameBoard[i, j] == "5") { return false; } } } return true; }
/// <summary> /// The main game cycle. /// </summary> /// <param name="gameBoard">the game board it is played on</param> /// <param name="userInput">the user UI handling object</param> public static void PlayGame(Board gameBoard, IConsole userInput) { int gameBoardSize = gameBoard.Rows; int rows = gameBoard.Rows; int cols = gameBoard.Cols; int detonatedMinesCounter = 0; do { int row; int col; bool isValidCell = false; do { EnterCoordinates(out row, out col, userInput); bool isInField = (row >= 0 && row < gameBoardSize) && (col >= 0 && col < gameBoardSize); isValidCell = isInField && gameBoard.GameBoard[row, col] != "-" && gameBoard.GameBoard[row, col] != "X"; if (!isValidCell) { userInput.WriteLine("Invalid move!"); } } while (!isValidCell); ExplosionHandler.HitMine(gameBoard, row, col); detonatedMinesCounter++; userInput.WriteLine(gameBoard.ToString()); } while (!IsGameOver(gameBoard)); userInput.WriteLine("Game over. Detonated mines: " + detonatedMinesCounter); }
/// <summary> /// Determines what mine has been hit. /// </summary> /// <param name="gameBoard">the game board it is played on</param> /// <param name="row">the row coordinate of the field</param> /// <param name="col">the col coordinate of the field</param> public static void HitMine(Board gameBoard, int row, int col) { int mineSize = Convert.ToInt32(gameBoard.GameBoard[row, col]); switch (mineSize) { case 1: HitMineOfSizeOne(gameBoard, row, col); break; case 2: HitMineOfSizeTwo(gameBoard, row, col); break; case 3: HitMineOfSizeThree(gameBoard, row, col); break; case 4: HitMineOfSizeFour(gameBoard, row, col); break; case 5: HitMineOfSizeFive(gameBoard, row, col); break; } }
/// <summary> /// Changes the game board according to the hit of mine with size two. /// </summary> /// <param name="gameBoard">the game board it is played on</param> /// <param name="row">the row coordinate of the field</param> /// <param name="col">the col coordinate of the field</param> private static void HitMineOfSizeTwo(Board gameBoard, int row, int col) { int rows = gameBoard.Rows; int cols = gameBoard.Cols; gameBoard.GameBoard[row, col] = "X"; HitMineOfSizeOne(gameBoard, row, col); if (col - 1 >= 0) { gameBoard.GameBoard[row, col - 1] = "X"; } if (col + 1 < cols) { gameBoard.GameBoard[row, col + 1] = "X"; } if (row - 1 >= 0) { gameBoard.GameBoard[row - 1, col] = "X"; } if (row + 1 < rows) { gameBoard.GameBoard[row + 1, col] = "X"; } }