public static void Main()
        {
            int pointsCount = 0;
            int row = 0;
            int col = 0;
            string command = string.Empty;
            char[,] gameField = CreateGameField();
            char[,] mines = PositionMines();
            bool steppedOnAMine = false;
            bool startedGame = true;
            bool endedGame = false;
            List<ScoreInfo> highscore = new List<ScoreInfo>(6);

            do
            {
                if (startedGame)
                {
                    Console.WriteLine("Let's play \"Minesweeper\". Try your luck not to step on fields with a mine." +
                    " Command 'top' shows the highscores, 'restart' starts a new game, 'exit' quits the game!");
                    PrintGameBoard(gameField);
                    startedGame = 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 <= gameField.GetLength(0) && col <= gameField.GetLength(1))
                    {
                        command = "turn";
                    }
                }
                switch (command)
                {
                    case "top":
                        Highscore(highscore);
                        break;
                    case "restart":
                        gameField = CreateGameField();
                        mines = PositionMines();
                        PrintGameBoard(gameField);
                        steppedOnAMine = false;
                        startedGame = false;
                        break;
                    case "exit":
                        Console.WriteLine("Bye bye!");
                        break;
                    case "turn":
                        if (mines[row, col] != '*')
                        {
                            if (mines[row, col] == '-')
                            {
                                GetFieldValue(gameField, mines, row, col);
                                pointsCount++;
                            }
                            if (MAX_FIELDS_WITHOUT_MINES == pointsCount)
                            {
                                endedGame = true;
                            }
                            else
                            {
                                PrintGameBoard(gameField);
                            }
                        }
                        else
                        {
                            steppedOnAMine = true;
                        }
                        break;
                    default:
                        Console.WriteLine("\nIvalid command!\n");
                        break;
                }
                if (steppedOnAMine)
                {
                    PrintGameBoard(mines);
                    Console.Write("\nSorry! You died with {0} points. " +
                        "Enter your nickname: ", pointsCount);
                    string nickName = Console.ReadLine();
                    ScoreInfo playerScore = new ScoreInfo(nickName, pointsCount);
                    if (highscore.Count < 5)
                    {
                        highscore.Add(playerScore);
                    }
                    else
                    {
                        for (int i = 0; i < highscore.Count; i++)
                        {
                            if (highscore[i].PlayerPoints < playerScore.PlayerPoints)
                            {
                                highscore.Insert(i, playerScore);
                                highscore.RemoveAt(highscore.Count - 1);
                                break;
                            }
                        }
                    }
                    highscore.Sort((ScoreInfo r1, ScoreInfo r2) => r2.PlayerName.CompareTo(r1.PlayerName));
                    highscore.Sort((ScoreInfo r1, ScoreInfo r2) => r2.PlayerPoints.CompareTo(r1.PlayerPoints));
                    Highscore(highscore);

                    gameField = CreateGameField();
                    mines = PositionMines();
                    pointsCount = 0;
                    steppedOnAMine = false;
                    startedGame = true;
                }
                if (endedGame)
                {
                    Console.WriteLine("\nCongrats! You opened all 35 fields unharmed.");
                    PrintGameBoard(mines);
                    Console.WriteLine("Please, enter your nickname: ");
                    string nickName = Console.ReadLine();
                    ScoreInfo playerScore = new ScoreInfo(nickName, pointsCount);
                    highscore.Add(playerScore);
                    Highscore(highscore);
                    gameField = CreateGameField();
                    mines = PositionMines();
                    pointsCount = 0;
                    endedGame = false;
                    startedGame = true;
                }
            }
            while (command != "exit");
            Console.WriteLine("Made in Bulgaria");
            Console.WriteLine("Bye bye. Press any key to exit.");
            Console.Read();
        }
Exemple #2
0
        public static void Main()
        {
            string command = string.Empty;
            char[,] field = CreateGameField();
            char[,] mines = GenerateMines();
            int playerScorePoints = 0;
            bool isOverMine = false;
            List<ScoreInfo> playersScores = new List<ScoreInfo>(6);
            int row = 0;
            int col = 0;
            bool isAtStartOfTheGame = true;
            bool wonTheGame = false;

            do
            {
                if (isAtStartOfTheGame)
                {
                    Console.WriteLine("Let`s play Minesweeper. Try your luck to find the cells without mines." +
                    " The command 'top' shows HighScores, the command 'restart' restarts the game, the command 'exit' quits the game!");
                    PrintGameField(field);
                    isAtStartOfTheGame = false;
                }

                Console.Write("Please enter row and col (row col): ");
                command = Console.ReadLine().Trim();

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

                switch (command)
                {
                    case "top":
                        ShowHighScore(playersScores);
                        break;
                    case "restart":
                        field = CreateGameField();
                        mines = GenerateMines();
                        PrintGameField(field);
                        isOverMine = false;
                        isAtStartOfTheGame = false;
                        break;
                    case "exit":
                        Console.WriteLine("Bye, Bye, Bye!");
                        break;
                    case "turn":
                        if (mines[row, col] != '*')
                        {
                            if (mines[row, col] == '-')
                            {
                                ShowFieldValue(field, mines, row, col);
                                playerScorePoints++;
                            }

                            if (FieldsWithoutMines == playerScorePoints)
                            {
                                wonTheGame = true;
                            }
                            else
                            {
                                PrintGameField(field);
                            }
                        }
                        else
                        {
                            isOverMine = true;
                        }

                        break;
                    default:
                        Console.WriteLine("\nWrong Command! Please try again!\n");
                        break;
                }

                if (isOverMine)
                {
                    PrintGameField(mines);
                    Console.Write("\nHrrrrrr! Game Over with {0} points. Please, enter your name: ", playerScorePoints);
                    string nickName = Console.ReadLine();
                    ScoreInfo t = new ScoreInfo(nickName, playerScorePoints);
                    if (playersScores.Count < 5)
                    {
                        playersScores.Add(t);
                    }
                    else
                    {
                        for (int i = 0; i < playersScores.Count; i++)
                        {
                            if (playersScores[i].Score < t.Score)
                            {
                                playersScores.Insert(i, t);
                                playersScores.RemoveAt(playersScores.Count - 1);
                                break;
                            }
                        }
                    }

                    playersScores.Sort((ScoreInfo r1, ScoreInfo r2) => r2.Name.CompareTo(r1.Name));
                    playersScores.Sort((ScoreInfo r1, ScoreInfo r2) => r2.Score.CompareTo(r1.Score));
                    ShowHighScore(playersScores);

                    field = CreateGameField();
                    mines = GenerateMines();
                    playerScorePoints = 0;
                    isOverMine = false;
                    isAtStartOfTheGame = true;
                }

                if (wonTheGame)
                {
                    Console.WriteLine("\nCongrats! You have open 35 cells without mines.");
                    PrintGameField(mines);
                    Console.WriteLine("Please, enter your name: ");
                    string winner = Console.ReadLine();
                    ScoreInfo winnerScore = new ScoreInfo(winner, playerScorePoints);
                    playersScores.Add(winnerScore);
                    ShowHighScore(playersScores);
                    field = CreateGameField();
                    mines = GenerateMines();
                    playerScorePoints = 0;
                    wonTheGame = false;
                    isAtStartOfTheGame = true;
                }
            }
            while (command != "exit");

            Console.WriteLine("Made in Bulgaria!");
            Console.WriteLine("Byeeeeeeee.");
            Console.Read();
        }
        public static void Main()
        {
            const int FieldsWithoutMines = 35;
            int pointCount = 0;
            int row = 0;
            int col = 0;
            string command;
            char[,] playfield = CreatePlayfield();
            char[,] mines = PutMines();
            bool isAtStartOfGame = true;
            bool steppedOnAMine = false;
            bool wonTheGame = false;
            List<ScoreInfo> highscores = new List<ScoreInfo>(6);

            do
            {
                if (isAtStartOfGame)
                {
                    Console.WriteLine("Let's play 'Minesweepers'. Try stepping only on the mine-free fields. " +
                    "Command 'top' shows the highscores, 'restart' starts a new game, 'exit' quits the game!");
                    PrintPlayfield(playfield);
                    isAtStartOfGame = 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 <= playfield.GetLength(0) && col <= playfield.GetLength(1))
                    {
                        command = "turn";
                    }
                }

                switch (command)
                {
                    case "top":
                        ShowHighscores(highscores);
                        break;
                    case "restart":
                        playfield = CreatePlayfield();
                        mines = PutMines();
                        PrintPlayfield(playfield);
                        break;
                    case "exit":
                        Console.WriteLine("Bye bye!");
                        break;
                    case "turn":
                        if (mines[row, col] != '*')
                        {
                            if (mines[row, col] == '-')
                            {
                                ShowFieldValue(playfield, mines, row, col);
                                pointCount++;
                            }

                            if (FieldsWithoutMines == pointCount)
                            {
                                wonTheGame = true;
                            }
                            else
                            {
                                PrintPlayfield(playfield);
                            }
                        }
                        else
                        {
                            steppedOnAMine = true;
                        }

                        break;
                    default:
                        Console.WriteLine("{0}Invalid command!{0}", Environment.NewLine);
                        break;
                }

                if (steppedOnAMine)
                {
                    PrintPlayfield(mines);
                    Console.Write("{0}You died with {1} points. Enter your nickname: ", Environment.NewLine, pointCount);
                    string nickname = Console.ReadLine();
                    ScoreInfo playerScore = new ScoreInfo(nickname, pointCount);

                    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((r1, r2) => r2.PlayerName.CompareTo(r1.PlayerName));
                    highscores.Sort((r1, r2) => r2.PlayerPoints.CompareTo(r1.PlayerPoints));
                    ShowHighscores(highscores);

                    playfield = CreatePlayfield();
                    mines = PutMines();
                    pointCount = 0;
                    steppedOnAMine = false;
                    isAtStartOfGame = true;
                }

                if (wonTheGame)
                {
                    Console.WriteLine("{0}Congratulations! You've opened all 35 fields unharmed!", Environment.NewLine);
                    PrintPlayfield(mines);
                    Console.WriteLine("Enter your nickname: ");
                    string nickname = Console.ReadLine();
                    ScoreInfo playerScore = new ScoreInfo(nickname, pointCount);
                    highscores.Add(playerScore);
                    ShowHighscores(highscores);
                    playfield = CreatePlayfield();
                    mines = PutMines();
                    pointCount = 0;
                    wonTheGame = false;
                    isAtStartOfGame = true;
                }
            }
            while (command != "exit");
            Console.WriteLine("Press any key to exit.");
            Console.Read();
        }
        public static void Main()
        {
            const int FieldsWithoutMines = 35;
            int       pointCount         = 0;
            int       row     = 0;
            int       col     = 0;
            string    command = string.Empty;

            char[,] playfield = CreatePlayfield();
            char[,] mines     = PutMines();
            bool             isAtStartOfGame = true;
            bool             steppedOnAMine  = false;
            bool             wonTheGame      = false;
            List <ScoreInfo> highscores      = new List <ScoreInfo>(6);

            do
            {
                if (isAtStartOfGame)
                {
                    Console.WriteLine("Let's play 'Minesweepers'. Try stepping only on the mine-free fields. " +
                                      "Command 'top' shows the highscores, 'restart' starts a new game, 'exit' quits the game!");
                    PrintPlayfield(playfield);
                    isAtStartOfGame = 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 <= playfield.GetLength(0) && col <= playfield.GetLength(1))
                    {
                        command = "turn";
                    }
                }

                switch (command)
                {
                case "top":
                    ShowHighscores(highscores);
                    break;

                case "restart":
                    playfield = CreatePlayfield();
                    mines     = PutMines();
                    PrintPlayfield(playfield);
                    steppedOnAMine  = false;
                    isAtStartOfGame = false;
                    break;

                case "exit":
                    Console.WriteLine("Bye bye!");
                    break;

                case "turn":
                    if (mines[row, col] != '*')
                    {
                        if (mines[row, col] == '-')
                        {
                            ShowFieldValue(playfield, mines, row, col);
                            pointCount++;
                        }

                        if (FieldsWithoutMines == pointCount)
                        {
                            wonTheGame = true;
                        }
                        else
                        {
                            PrintPlayfield(playfield);
                        }
                    }
                    else
                    {
                        steppedOnAMine = true;
                    }

                    break;

                default:
                    Console.WriteLine("{0}Invalid command!{0}", Environment.NewLine);
                    break;
                }

                if (steppedOnAMine)
                {
                    PrintPlayfield(mines);
                    Console.Write("{0}You died with {1} points. Enter your nickname: ", Environment.NewLine, pointCount);
                    string    nickname    = Console.ReadLine();
                    ScoreInfo playerScore = new ScoreInfo(nickname, pointCount);

                    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((ScoreInfo r1, ScoreInfo r2) => r2.PlayerName.CompareTo(r1.PlayerName));
                    highscores.Sort((ScoreInfo r1, ScoreInfo r2) => r2.PlayerPoints.CompareTo(r1.PlayerPoints));
                    ShowHighscores(highscores);

                    playfield       = CreatePlayfield();
                    mines           = PutMines();
                    pointCount      = 0;
                    steppedOnAMine  = false;
                    isAtStartOfGame = true;
                }

                if (wonTheGame)
                {
                    Console.WriteLine("{0}Congratulations! You've opened all 35 fields unharmed!", Environment.NewLine);
                    PrintPlayfield(mines);
                    Console.WriteLine("Enter your nickname: ");
                    string    nickname    = Console.ReadLine();
                    ScoreInfo playerScore = new ScoreInfo(nickname, pointCount);
                    highscores.Add(playerScore);
                    ShowHighscores(highscores);
                    playfield       = CreatePlayfield();
                    mines           = PutMines();
                    pointCount      = 0;
                    wonTheGame      = false;
                    isAtStartOfGame = true;
                }
            }while (command != "exit");
            Console.WriteLine("Press any key to exit.");
            Console.Read();
        }
        public static void Main()
        {
            int    pointsCount = 0;
            int    row         = 0;
            int    col         = 0;
            string command     = string.Empty;

            char[,] gameField = CreateGameField();
            char[,] mines     = PositionMines();
            bool             steppedOnAMine = false;
            bool             startedGame    = true;
            bool             endedGame      = false;
            List <ScoreInfo> highscore      = new List <ScoreInfo>(6);

            do
            {
                if (startedGame)
                {
                    Console.WriteLine("Let's play \"Minesweeper\". Try your luck not to step on fields with a mine." +
                                      " Command 'top' shows the highscores, 'restart' starts a new game, 'exit' quits the game!");
                    PrintGameBoard(gameField);
                    startedGame = 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 <= gameField.GetLength(0) && col <= gameField.GetLength(1))
                    {
                        command = "turn";
                    }
                }
                switch (command)
                {
                case "top":
                    Highscore(highscore);
                    break;

                case "restart":
                    gameField = CreateGameField();
                    mines     = PositionMines();
                    PrintGameBoard(gameField);
                    steppedOnAMine = false;
                    startedGame    = false;
                    break;

                case "exit":
                    Console.WriteLine("Bye bye!");
                    break;

                case "turn":
                    if (mines[row, col] != '*')
                    {
                        if (mines[row, col] == '-')
                        {
                            GetFieldValue(gameField, mines, row, col);
                            pointsCount++;
                        }
                        if (MAX_FIELDS_WITHOUT_MINES == pointsCount)
                        {
                            endedGame = true;
                        }
                        else
                        {
                            PrintGameBoard(gameField);
                        }
                    }
                    else
                    {
                        steppedOnAMine = true;
                    }
                    break;

                default:
                    Console.WriteLine("\nIvalid command!\n");
                    break;
                }
                if (steppedOnAMine)
                {
                    PrintGameBoard(mines);
                    Console.Write("\nSorry! You died with {0} points. " +
                                  "Enter your nickname: ", pointsCount);
                    string    nickName    = Console.ReadLine();
                    ScoreInfo playerScore = new ScoreInfo(nickName, pointsCount);
                    if (highscore.Count < 5)
                    {
                        highscore.Add(playerScore);
                    }
                    else
                    {
                        for (int i = 0; i < highscore.Count; i++)
                        {
                            if (highscore[i].PlayerPoints < playerScore.PlayerPoints)
                            {
                                highscore.Insert(i, playerScore);
                                highscore.RemoveAt(highscore.Count - 1);
                                break;
                            }
                        }
                    }
                    highscore.Sort((ScoreInfo r1, ScoreInfo r2) => r2.PlayerName.CompareTo(r1.PlayerName));
                    highscore.Sort((ScoreInfo r1, ScoreInfo r2) => r2.PlayerPoints.CompareTo(r1.PlayerPoints));
                    Highscore(highscore);

                    gameField      = CreateGameField();
                    mines          = PositionMines();
                    pointsCount    = 0;
                    steppedOnAMine = false;
                    startedGame    = true;
                }
                if (endedGame)
                {
                    Console.WriteLine("\nCongrats! You opened all 35 fields unharmed.");
                    PrintGameBoard(mines);
                    Console.WriteLine("Please, enter your nickname: ");
                    string    nickName    = Console.ReadLine();
                    ScoreInfo playerScore = new ScoreInfo(nickName, pointsCount);
                    highscore.Add(playerScore);
                    Highscore(highscore);
                    gameField   = CreateGameField();
                    mines       = PositionMines();
                    pointsCount = 0;
                    endedGame   = false;
                    startedGame = true;
                }
            }while (command != "exit");
            Console.WriteLine("Made in Bulgaria");
            Console.WriteLine("Bye bye. Press any key to exit.");
            Console.Read();
        }
Exemple #6
0
        public static void Run()
        {
            string command = string.Empty;

            char[,] gameField = GenerateGameField();
            char[,] mines     = GenerateMines();
            int              pointsCounter  = 0;
            bool             isMineExploded = false;
            List <ScoreInfo> hignScoreInfo  = new List <ScoreInfo>(MaxHighScoreLength);
            int              row            = 0;
            int              col            = 0;
            bool             isNewGame      = true;
            bool             isGameOver     = false;

            do
            {
                if (isNewGame)
                {
                    Console.WriteLine(GameRules);
                    PrintGameField(gameField);
                    isNewGame = false;
                }

                Console.Write(EnterMove);

                command = Console.ReadLine().Trim();

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

                switch (command)
                {
                case "top":
                    ShowHighScoreList(hignScoreInfo);
                    break;

                case "restart":
                    gameField = GenerateGameField();
                    mines     = GenerateMines();
                    PrintGameField(gameField);
                    isMineExploded = false;
                    isNewGame      = false;
                    break;

                case "exit":
                    Console.WriteLine(Bye);
                    break;

                case "turn":
                    if (mines[row, col] != MineFieldSymbol)
                    {
                        if (mines[row, col] == WithoutMineFieldSymbol)
                        {
                            ShowFieldValue(gameField, mines, row, col);
                            pointsCounter++;
                        }

                        if (MaxPossibleMoves == pointsCounter)
                        {
                            isGameOver = true;
                        }
                        else
                        {
                            PrintGameField(gameField);
                        }
                    }
                    else
                    {
                        isMineExploded = true;
                    }

                    break;

                default:
                    Console.WriteLine(WrongCommand);
                    break;
                }

                if (isMineExploded)
                {
                    PrintGameField(mines);
                    Console.Write(string.Format(YouLost, pointsCounter));
                    Console.WriteLine(EnterYourNickName);

                    string    nickname      = Console.ReadLine();
                    ScoreInfo currentPlayer = new ScoreInfo(nickname, pointsCounter);

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

                    hignScoreInfo.Sort((ScoreInfo firstPlayerInfo, ScoreInfo nextPlayerInfo) =>
                                       nextPlayerInfo.Name.CompareTo(firstPlayerInfo.Name));
                    hignScoreInfo.Sort((ScoreInfo firstPlayerInfo, ScoreInfo nextPlayerInfo) =>
                                       nextPlayerInfo.Points.CompareTo(firstPlayerInfo.Points));
                    ShowHighScoreList(hignScoreInfo);

                    gameField      = GenerateGameField();
                    mines          = GenerateMines();
                    pointsCounter  = 0;
                    isMineExploded = false;
                    isNewGame      = true;
                }

                if (isGameOver)
                {
                    Console.WriteLine(YouWon);
                    PrintGameField(mines);
                    Console.WriteLine(EnterYourNickName);
                    string    nickname          = Console.ReadLine();
                    ScoreInfo currentPlayerInfo = new ScoreInfo(nickname, pointsCounter);
                    hignScoreInfo.Add(currentPlayerInfo);
                    ShowHighScoreList(hignScoreInfo);
                    gameField     = GenerateGameField();
                    mines         = GenerateMines();
                    pointsCounter = 0;
                    isGameOver    = false;
                    isNewGame     = true;
                }
            }while (command != "exit");

            Console.WriteLine(MadeInBulgaria);
            Console.Read();
        }