public static void Main()
        {
            const int FieldsWithoutMines = 35;
            string command = string.Empty;
            char[,] playingBoard = CreateBoard();
            char[,] minesBoard = DeployMines();
            int playerPoints = 0;
            int row = 0;
            int col = 0;
            bool hasSteppedOnMine = false;
            bool isAtStartOfTheGame = true;
            bool hasWonTheGame = false;
            List<Score> highscores = new List<Score>(6);

            do
            {
                if (isAtStartOfTheGame)
                {
                    Console.WriteLine("Let's play 'Minesweeper'! Try stepping only on spots without mines." +
                    " The 'top' command shows the leaderboard, use 'restart' to reset the game, use 'exit' to stop the game!");
                    DrawBoard(playingBoard);
                    isAtStartOfTheGame = false;
                }

                Console.Write("Enter row and column : ");
                command = Console.ReadLine().Trim();

                if (command.Length >= 3)
                {
                    if (int.TryParse(command[0].ToString(), out row) &&
                        int.TryParse(command[2].ToString(), out col) &&
                        row < playingBoard.GetLength(0) && col < playingBoard.GetLength(1))
                    {
                        command = "turn";
                    }
                }

                switch (command)
                {
                    case "top":
                        DisplayLeaderBoard(highscores);
                        break;
                    case "restart":
                        playingBoard = CreateBoard();
                        minesBoard = DeployMines();
                        DrawBoard(playingBoard);
                        hasSteppedOnMine = false;
                        isAtStartOfTheGame = false;
                        break;
                    case "exit":
                        Console.WriteLine("Bye bye!");
                        break;
                    case "turn":
                        if (minesBoard[row, col] != '*')
                        {
                            if (minesBoard[row, col] == '-')
                            {
                                ShowSpotValue(playingBoard, minesBoard, row, col);
                                playerPoints++;
                            }

                            if (FieldsWithoutMines == playerPoints)
                            {
                                hasWonTheGame = true;
                            }
                            else
                            {
                                DrawBoard(playingBoard);
                            }
                        }
                        else
                        {
                            hasSteppedOnMine = true;
                        }

                        break;
                    default:
                        Console.WriteLine("\nInvalid command!\n");
                        break;
                }

                if (hasSteppedOnMine)
                {
                    DrawBoard(minesBoard);
                    Console.Write("\nGame over! You died with {0} points. " + "Enter nickname: ", playerPoints);
                    string nickname = Console.ReadLine();
                    Score playerScore = new Score(nickname, playerPoints);
                    if (highscores.Count < 5)
                    {
                        highscores.Add(playerScore);
                    }
                    else
                    {
                        for (int i = 0; i < highscores.Count; i++)
                        {
                            if (highscores[i].PlayerPoints < playerScore.PlayerPoints)
                            {
                                highscores.Insert(i, playerScore);
                                highscores.RemoveAt(highscores.Count - 1);
                                break;
                            }
                        }
                    }

                    highscores.Sort((Score playerOne, Score playerTwo) => playerTwo.PlayerName.CompareTo(playerOne.PlayerName));
                    highscores.Sort((Score playerOne, Score playerTwo) => playerTwo.PlayerPoints.CompareTo(playerOne.PlayerPoints));
                    DisplayLeaderBoard(highscores);

                    playingBoard = CreateBoard();
                    minesBoard = DeployMines();
                    playerPoints = 0;
                    hasSteppedOnMine = false;
                    isAtStartOfTheGame = true;
                }

                if (hasWonTheGame)
                {
                    Console.WriteLine("\nCongratulations! You won the game!");
                    DrawBoard(minesBoard);
                    Console.WriteLine("Enter nickname: ");
                    string playerName = Console.ReadLine();
                    Score playerScore = new Score(playerName, playerPoints);
                    highscores.Add(playerScore);
                    DisplayLeaderBoard(highscores);
                    playingBoard = CreateBoard();
                    minesBoard = DeployMines();
                    playerPoints = 0;
                    hasWonTheGame = false;
                    isAtStartOfTheGame = true;
                }
            }
            while (command != "exit");

            Console.WriteLine("Press any key to continue.");
            Console.Read();
        }
Example #2
0
        public static void Main(string[] args)
        {
            const int MAXIMUM_CELLS_TO_OPEN = 35;

            string currentCommand = string.Empty;
            char[,] playfieldMatrix = GeneratePlayfield();
            char[,] bombsMatrix = GenerateBombs();
            int openedPositionsCounter = 0;
            bool openedMine = false;
            List<Score> topPlayers = new List<Score>(6);
            int row = 0;
            int col = 0;
            bool noGameActive = true;
            bool reachedMaximumOpenedCells = false;

            do
            {
                if (noGameActive)
                {
                    Console.WriteLine("Hajde da igraem na “Mini4KI”. Probvaj si kasmeta da otkriesh poleteta bez mini4ki." +
                    " Komanda 'top' pokazva klasiraneto, 'restart' po4va nova igra, 'exit' izliza i hajde 4ao!");
                    ShowPlayfieldOnConsole(playfieldMatrix);
                    noGameActive = false;
                }

                Console.Write("Daj red i kolona : ");
                currentCommand = Console.ReadLine().Trim();

                if (currentCommand.Length >= 3)
                {
                    if (int.TryParse(currentCommand[0].ToString(), out row) &&
                    int.TryParse(currentCommand[2].ToString(), out col) &&
                        row <= playfieldMatrix.GetLength(0) && col <= playfieldMatrix.GetLength(1))
                    {
                        currentCommand = "turn";
                    }
                }

                switch (currentCommand)
                {
                    case "top":
                        ShowTopScores(topPlayers);
                        break;
                    case "restart":
                        playfieldMatrix = GeneratePlayfield();
                        bombsMatrix = GenerateBombs();
                        ShowPlayfieldOnConsole(playfieldMatrix);
                        openedMine = false;
                        noGameActive = false;
                        break;
                    case "exit":
                        Console.WriteLine("4a0, 4a0, 4a0!");
                        break;
                    case "turn":
                        if (bombsMatrix[row, col] != '*')
                        {
                            if (bombsMatrix[row, col] == '-')
                            {
                                OpenPositionOnPlayfield(playfieldMatrix, bombsMatrix, row, col);
                                openedPositionsCounter++;
                            }

                            if (MAXIMUM_CELLS_TO_OPEN == openedPositionsCounter)
                            {
                                reachedMaximumOpenedCells = true;
                            }
                            else
                            {
                                ShowPlayfieldOnConsole(playfieldMatrix);
                            }
                        }
                        else
                        {
                            openedMine = true;
                        }

                        break;
                    default:
                        Console.WriteLine("\nGreshka! nevalidna Komanda\n");
                        break;
                }

                if (openedMine)
                {
                    ShowPlayfieldOnConsole(bombsMatrix);

                    Console.Write("\nHrrrrrr! Umria gerojski s {0} to4ki. Daj si niknejm: ", openedPositionsCounter);
                    string currentName = Console.ReadLine();
                    Score currentScore = new Score(currentName, openedPositionsCounter);

                    if (topPlayers.Count < 5)
                    {
                        topPlayers.Add(currentScore);
                    }
                    else
                    {
                        for (int i = 0; i < topPlayers.Count; i++)
                        {
                            if (topPlayers[i].Points < currentScore.Points)
                            {
                                topPlayers.Insert(i, currentScore);
                                topPlayers.RemoveAt(topPlayers.Count - 1);
                                break;
                            }
                        }
                    }

                    topPlayers.Sort((Score r1, Score r2) => r2.Name.CompareTo(r1.Name));
                    topPlayers.Sort((Score r1, Score r2) => r2.Points.CompareTo(r1.Points));
                    ShowTopScores(topPlayers);

                    playfieldMatrix = GeneratePlayfield();
                    bombsMatrix = GenerateBombs();
                    openedPositionsCounter = 0;
                    openedMine = false;
                    noGameActive = true;
                }

                if (reachedMaximumOpenedCells)
                {
                    Console.WriteLine("\nBRAVOOOS! Otvri 35 kletki bez kapka kryv.");
                    ShowPlayfieldOnConsole(bombsMatrix);
                    Console.WriteLine("Daj si imeto, batka: ");
                    string imeee = Console.ReadLine();
                    Score to4kii = new Score(imeee, openedPositionsCounter);
                    topPlayers.Add(to4kii);
                    ShowTopScores(topPlayers);
                    playfieldMatrix = GeneratePlayfield();
                    bombsMatrix = GenerateBombs();
                    openedPositionsCounter = 0;
                    reachedMaximumOpenedCells = false;
                    noGameActive = true;
                }
            }
            while (currentCommand != "exit");
            Console.WriteLine("Made in Bulgaria - Uauahahahahaha!");
            Console.WriteLine("AREEEEEEeeeeeee.");
            Console.Read();
        }
        public static void Main()
        {
            const int MaximumCellsToOpen = 35;

            string command = string.Empty;
            char[,] board = CreateGameBoard();
            char[,] bombs = GenerateBombs();
            int pointCounter = 0;
            bool openedMine = false;
            int row = 0;
            int col = 0;
            bool noGameActive = true;
            bool wonTheGame = false;

            List<Score> topPlayers = new List<Score>(6);

            do
            {
                if (noGameActive)
                {
                    Console.WriteLine("Hajde da igraem na “Mini4KI”. Probvaj si kasmeta da otkriesh poleteta bez mini4ki." +
                    " Komanda 'top' pokazva klasiraneto, 'restart' po4va nova igra, 'exit' izliza i hajde 4ao!");
                    ShowBoardOnConsole(board);
                    noGameActive = false;
                }

                Console.Write("Daj red i kolona : ");

                command = Console.ReadLine().Trim();
                if (command.Length >= 3)
                {
                    if (int.TryParse(command[0].ToString(), out row) &&
                    int.TryParse(command[2].ToString(), out col) &&
                        row <= board.GetLength(0) && col <= board.GetLength(1))
                    {
                        command = "turn";
                    }
                }

                switch (command)
                {
                    case "top":
                        ShowHighScores(topPlayers);
                        break;
                    case "restart":
                        board = CreateGameBoard();
                        bombs = GenerateBombs();
                        ShowBoardOnConsole(board);
                        openedMine = false;
                        noGameActive = false;
                        break;
                    case "exit":
                        Console.WriteLine("4a0, 4a0, 4a0!");
                        break;
                    case "turn":
                        if (bombs[row, col] != '*')
                        {
                            if (bombs[row, col] == '-')
                            {
                                OpenPositionOnBoard(board, bombs, row, col);
                                pointCounter++;
                            }

                            if (MaximumCellsToOpen == pointCounter)
                            {
                                wonTheGame = true;
                            }
                            else
                            {
                                ShowBoardOnConsole(board);
                            }
                        }
                        else
                        {
                            openedMine = true;
                        }

                        break;
                    default:
                        Console.WriteLine("\nGreshka! nevalidna Komanda\n");
                        break;
                }

                if (openedMine)
                {
                    ShowBoardOnConsole(bombs);
                    Console.Write("\nHrrrrrr! Umria gerojski s {0} to4ki. Daj si niknejm: ", pointCounter);
                    string name = Console.ReadLine();
                    Score currentPlayer = new Score(name, pointCounter);
                    if (topPlayers.Count < 5)
                    {
                        topPlayers.Add(currentPlayer);
                    }
                    else
                    {
                        for (int i = 0; i < topPlayers.Count; i++)
                        {
                            if (topPlayers[i].Points < currentPlayer.Points)
                            {
                                topPlayers.Insert(i, currentPlayer);
                                topPlayers.RemoveAt(topPlayers.Count - 1);
                                break;
                            }
                        }
                    }

                    topPlayers.Sort((Score r1, Score r2) => r2.Name.CompareTo(r1.Name));
                    topPlayers.Sort((Score r1, Score r2) => r2.Points.CompareTo(r1.Points));
                    ShowHighScores(topPlayers);

                    board = CreateGameBoard();
                    bombs = GenerateBombs();
                    pointCounter = 0;
                    openedMine = false;
                    noGameActive = true;
                }

                if (wonTheGame)
                {
                    Console.WriteLine("\nBRAVOOOS! Otvori 35 kletki bez kapka kryv.");
                    ShowBoardOnConsole(bombs);
                    Console.WriteLine("Daj si imeto, batka: ");
                    string name = Console.ReadLine();
                    Score currentPlayer = new Score(name, pointCounter);
                    topPlayers.Add(currentPlayer);
                    ShowHighScores(topPlayers);
                    board = CreateGameBoard();
                    bombs = GenerateBombs();
                    pointCounter = 0;
                    wonTheGame = false;
                    noGameActive = true;
                }
            }
            while (command != "exit");
            
            Console.WriteLine("Made in Bulgaria - Uauahahahahaha!");
            Console.WriteLine("AREEEEEEeeeeeee.");
            Console.Read();
        }