示例#1
0
        public HighScore GetScore(GameModes mode)
        {
            HighScore score = null;

            switch (mode)
            {
            case GameModes.EASY:
                score = settingsFile.easyTime;
                break;

            case GameModes.INTERMEDIATE:
                score = settingsFile.mediumTime;
                break;

            case GameModes.ADVANCED:
                score = settingsFile.advancedTime;
                break;
            }

            return(score);
        }
示例#2
0
        private static void Main()
        {
            const int Max = 35;

            string command;
            char[,] gameBoard = CreateGameBoard();
            char[,] bombs = PlaceBombs();

            List<HighScore> highScores = new List<HighScore>(6);

            int row = 0;
            int column = 0;
            int count = 0;

            bool isNewGame = true;
            bool win = false;
            bool lose = false;

            do
            {
                if (isNewGame)
                {
                    Console.WriteLine(
                        "Lets play Minesweeper. Command 'top' shows the high scores, 'restart' starts new game, 'exit' exits from the game!");
                    RenderBoard(gameBoard);
                    isNewGame = false;
                }

                Console.Write("Type row and column: ");
                command = Console.ReadLine().Trim();
                if (command.Length >= 3)
                {
                    bool isValidRow =
                        int.TryParse(command[0].ToString(), out row) && row <= gameBoard.GetLength(0);
                    bool isValidColumn =
                        int.TryParse(command[2].ToString(), out column) && column <= gameBoard.GetLength(1);
                    if (isValidRow && isValidColumn)
                    {
                        command = "turn";
                    }
                }

                switch (command)
                {
                    case "top":
                        PrintHighScores(highScores);
                        break;
                    case "restart":
                        gameBoard = CreateGameBoard();
                        bombs = PlaceBombs();
                        RenderBoard(gameBoard);
                        lose = false;
                        isNewGame = false;
                        break;
                    case "exit":
                        Console.WriteLine("Bye!");
                        break;
                    case "turn":
                        if (bombs[row, column] != '*')
                        {
                            if (bombs[row, column] == '-')
                            {
                                NextTurn(gameBoard, bombs, row, column);
                                count++;
                            }

                            if (Max == count)
                            {
                                win = true;
                            }
                            else
                            {
                                RenderBoard(gameBoard);
                            }
                        }
                        else
                        {
                            lose = true;
                        }

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

                if (lose)
                {
                    RenderBoard(bombs);
                    Console.Write("\nGame over! You made {0} points. \nUsername: "******"\nYou win!");
                    RenderBoard(bombs);
                    Console.WriteLine("Username: "******"exit");
            Console.Read();
        }
示例#3
0
        private void GameOver()
        {
            this.PrintBoard(this.gameBoardWithBombs);
            Console.Write("\nYou died with {0} points. " + "Enter nickname: ", this.points);
            string nickname = Console.ReadLine();
            HighScore currentScore = new HighScore(nickname, this.points);
            if (currentScore.Points > 0)
            {
                this.scores.Add(currentScore);
            }
            else
            {
                for (int i = 0; i < this.scores.Count; i++)
                {
                    if (this.scores[i].Points < currentScore.Points)
                    {
                        this.scores.Insert(i, currentScore);
                        this.scores.RemoveAt(this.scores.Count - 1);
                        break;
                    }
                }
            }

            this.scores = this.scores.OrderByDescending(x => x.Points).ToList();
            this.Scoreboard(this.scores);

            this.gameBoard = this.CreateGameBoard();
            this.gameBoardWithBombs = this.CreateGameBoardWithBombs();
            this.points = 0;
            this.steppedOnABomb = false;
            this.gameBegin = true;
        }
示例#4
0
 private void GameWin()
 {
     Console.WriteLine("\nCongratulations! You opened 35 cells without dying.");
     this.PrintBoard(this.gameBoardWithBombs);
     Console.WriteLine("Enter nickname: ");
     string nickname = Console.ReadLine();
     HighScore currentScore = new HighScore(nickname, this.points);
     this.scores.Add(currentScore);
     this.Scoreboard(this.scores);
     this.gameBoard = this.CreateGameBoard();
     this.gameBoardWithBombs = this.CreateGameBoardWithBombs();
     this.points = 0;
     this.youWin = false;
     this.gameBegin = true;
 }
示例#5
0
        private static void Main()
        {
            string playerCommand = string.Empty;

            char[,] playField = CreatePlayField();
            char[,] mineField = PutMinesIntoTheField();
            int              openFields       = 0;
            bool             foundMine        = false;
            List <HighScore> highScorePlayers = new List <HighScore>(6);
            int              chosenRow        = 0;
            int              chosenColumn     = 0;
            bool             newGameStarted   = true;
            const int        NonMineFields    = 35;
            bool             areAllFieldsOpen = false;

            do
            {
                if (newGameStarted)
                {
                    Console.WriteLine("Welcome to Minesweeper! \nYou must find the fields without mines on them." +
                                      "\nCommands: \n'top' for HiScores, \n'restart' for a new game, \n'exit' if you want to leave!");
                    RenderField(playField);
                    newGameStarted = false;
                }

                Console.Write("Please enter command or minefield coordinates: ");
                playerCommand = Console.ReadLine().Trim();
                if (playerCommand.Length >= 3)
                {
                    if (int.TryParse(playerCommand[0].ToString(), out chosenRow) &&
                        int.TryParse(playerCommand[2].ToString(), out chosenColumn) &&
                        chosenRow < playField.GetLength(0) && chosenColumn < playField.GetLength(1))
                    {
                        playerCommand = "turn";
                    }
                }

                switch (playerCommand)
                {
                case "top":
                    TopScores(highScorePlayers);
                    break;

                case "restart":
                    playField = CreatePlayField();
                    mineField = PutMinesIntoTheField();
                    RenderField(playField);
                    foundMine      = false;
                    newGameStarted = false;
                    break;

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

                case "turn":
                    if (mineField[chosenRow, chosenColumn] != '*')
                    {
                        if (mineField[chosenRow, chosenColumn] == '-')
                        {
                            PlayersMove(playField, mineField, chosenRow, chosenColumn);
                            openFields++;
                        }

                        if (NonMineFields == openFields)
                        {
                            areAllFieldsOpen = true;
                        }
                        else
                        {
                            RenderField(playField);
                        }
                    }
                    else
                    {
                        foundMine = true;
                    }

                    break;

                default:
                    Console.WriteLine("\nNot a valid command!\n");
                    break;
                }

                if (foundMine)
                {
                    RenderField(mineField);
                    Console.Write("\nYou stepped on a mine! You've got {0} points. Enter a nickname: ", openFields);
                    string    nickname         = Console.ReadLine();
                    HighScore currentHighScore = new HighScore(nickname, openFields);
                    if (highScorePlayers.Count < 5)
                    {
                        highScorePlayers.Add(currentHighScore);
                    }
                    else
                    {
                        for (int i = 0; i < highScorePlayers.Count; i++)
                        {
                            if (highScorePlayers[i].Score < currentHighScore.Score)
                            {
                                highScorePlayers.Insert(i, currentHighScore);
                                highScorePlayers.RemoveAt(highScorePlayers.Count - 1);
                                break;
                            }
                        }
                    }

                    highScorePlayers.Sort((HighScore firstScore, HighScore secondScore)
                                          => secondScore.Name.CompareTo(firstScore.Name));
                    highScorePlayers.Sort((HighScore firstScore, HighScore secondScore)
                                          => secondScore.Score.CompareTo(firstScore.Score));
                    TopScores(highScorePlayers);

                    playField      = CreatePlayField();
                    mineField      = PutMinesIntoTheField();
                    openFields     = 0;
                    foundMine      = false;
                    newGameStarted = true;
                }

                if (areAllFieldsOpen)
                {
                    Console.WriteLine("\nCongratulations! You win!");
                    RenderField(mineField);
                    Console.WriteLine("Enter a nickname: ");
                    string    nickname       = Console.ReadLine();
                    HighScore currentHiScore = new HighScore(nickname, openFields);
                    highScorePlayers.Add(currentHiScore);
                    TopScores(highScorePlayers);
                    playField        = CreatePlayField();
                    mineField        = PutMinesIntoTheField();
                    openFields       = 0;
                    areAllFieldsOpen = false;
                    newGameStarted   = true;
                }
            }while (playerCommand != "exit");
            Console.WriteLine("Thank you for playing!");
            Console.Read();
        }