int widthHeight = 0; Random rnd = new Random(); Boolean gameLoop = true; int turn = 0; int updateLoopCounter = 0; Console.WriteLine("Welcome to Minesweeper!"); Console.WriteLine("Every time you select an empty space, the value will show up as a '9' and if you hit a mine the game will end. Good luck!\n"); Console.WriteLine("What board size do you want to play with? \nSmall \nMedium \nLarge"); string boardSize = Console.ReadLine().ToLower(); Console.Clear(); if (boardSize.Contains("small")) { widthHeight = 10; int[,] board = new int[widthHeight, widthHeight]; for (int i = 0; i < widthHeight; i++) { for (int j = 0; j < widthHeight; j++) { board[i, j] = rnd.Next(100); } } Game(board); } else if (boardSize.Contains("medium")) { widthHeight = 15; int[,] board = new int[widthHeight, widthHeight]; for (int i = 0; i < widthHeight; i++) { for (int j = 0; j < widthHeight; j++) { board[i, j] = rnd.Next(100); } } Game(board); } else if (boardSize.Contains("large")) { widthHeight = 20; int[,] board = new int[widthHeight, widthHeight]; for (int i = 0; i < widthHeight; i++) { for (int j = 0; j < widthHeight; j++) { board[i, j] = rnd.Next(100); } } Game(board); } bool CheckBoard(int _width, int _height, int[,] _board, int[,] _original) { for (int i = 0; i < _board.GetLength(0); i++) { for (int k = 0; k < _board.GetLength(1); k++) { if (_original[i, k] == 1 && _original[_width, _height] == 1) { return false; } } } return true; } int[,] GenerateBoard(int[,] _randomBoard) { for (int i = 0; i < widthHeight; i++) { for (int j = 0; j < widthHeight; j++) { if (_randomBoard[i, j] > 87) { _randomBoard[i, j] = 1; } else { _randomBoard[i, j] = 0; } } } return _randomBoard; } int[,] HideValues(int[,] _board) { for (int i = 0; i < _board.GetLength(0); i++) { for (int k = 0; k < _board.GetLength(1); k++) { if (_board[i, k] == 1) { _board[i, k] = 0; } } } return _board; } void PrintBoard(int[,] _board) { string boardBottom = new string('-', widthHeight*4-1); Console.Write(" "); for (int i = 0; i < _board.GetLength(1); i++) { if (i <= 9) { Console.Write(i + " "); } else { Console.Write(i + " "); } } Console.WriteLine("\n"); for (int i = 0; i < _board.GetLength(0); i++) { if (i <= 9) { Console.Write(i + " "); } else { Console.Write(i + " "); } for (int k = 0; k < _board.GetLength(1); k++) { //put a single value Console.Write(_board[i, k] + " | "); } //next row Console.Write("\n " + boardBottom + "\n"); } } int[,] UpdateBoard(int _width, int _height, int[,] _board, int[,] _originalBoard) { if (_originalBoard[_width, _height] == 1) { // The selected position contains a mine, end the game Console.WriteLine("Game over!"); gameLoop = false; } else if (_board[_width, _height] == 0) { _board[_width, _height] = 9; if ((_originalBoard[_width + 1, _height] == 0 || _originalBoard[_width, _height + 1] == 0 || _originalBoard[_width - 1, _height] == 0 || _originalBoard[_width, _height - 1] == 0) && updateLoopCounter < 11) { UpdateBoard(_width + 1, _height, _board, _originalBoard); UpdateBoard(_width, _height + 1, _board, _originalBoard); UpdateBoard(_width - 1, _height, _board, _originalBoard); UpdateBoard(_width, _height - 1, _board, _originalBoard); updateLoopCounter++; } } return _board; } bool CheckFirstTurn() { return turn == 0; } int[,] OverrideBoard(int _width, int _height, int[,] _board, int[,] _original) { if(CheckFirstTurn()) { for(int i = 0; i < _original.GetLength(0); i++) { for(int j = 0; j < _original.GetLength(1); j++) { if(i == _width && j == _height && _original[i, j] == 1 && turn < 1) { _board[i, j] = 9; UpdateBoard(i, j, _board, _original); } } } } turn++; return _board; } void Game(int[,] board) { int[,] originalBoard = GenerateBoard(board); int[,] theBoard = GenerateBoard(board); int userWidth; int userHeight; PrintBoard(HideValues(theBoard)); while (gameLoop) { try { Console.WriteLine("Enter a height value."); userWidth = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter a width value."); userHeight = Convert.ToInt32(Console.ReadLine()); theBoard = OverrideBoard(userWidth, userHeight, theBoard, board); theBoard = UpdateBoard(userWidth, userHeight, theBoard, board); bool tempLoop = CheckBoard(userHeight, userWidth, theBoard, originalBoard); if (!tempLoop) { Console.WriteLine("You hit a mine: \n"); PrintBoard(originalBoard); } else { PrintBoard(theBoard); } gameLoop = tempLoop; } catch (Exception e) { Console.WriteLine("Enter a value please. \n"); PrintBoard(theBoard); } } }