Пример #1
0
        public static void Main(string[] args)
        {
            GameField gameField = new GameField(10,10);

            char[,] playingField = gameField.Create();
            char[,] bombsField = gameField.PlaceBombs();

            int maxScore = (gameField.FieldCols * gameField.FieldCols) -
                           (gameField.FieldCols + gameField.FieldCols);

            Engine engine = new Engine();

            while (true)
            {
                if (Engine.IsNewGame)
                {
                    playingField = gameField.Create();
                    bombsField = gameField.PlaceBombs();

                    Draw.GameLoad();
                    Draw.PlayingField(playingField);
                }

                Console.Write("Enter row and column: ");
                string inputCommand = Console.ReadLine();

                if (inputCommand == "exit")
                {
                    break;
                }

                inputCommand = engine.ParseInputCommand(inputCommand, playingField);

                engine.ExecuteCommand(inputCommand, gameField, playingField, bombsField, maxScore);
            }

            Console.WriteLine("Press any key to exit the game.");
            Console.Read();
        }
Пример #2
0
        /// <summary>
        /// Executes a parsed command.
        /// </summary>
        /// <param name="inputCommand">The parsed command to execute.</param>
        /// <param name="gameField">The game field dimensions.</param>
        /// <param name="fieldWithQuestionmarks">The unrevealed game field.(with question marks)</param>
        /// <param name="fieldWithBombs">The bombs field.(the places of the bombs)</param>
        /// <param name="maxScore">The max score formula.
        ///     Calculated by the formula (gameField.FieldCols * gameField.FieldCols) - (gameField.FieldCols + gameField.FieldCols)
        /// </param>
        public void ExecuteCommand(string inputCommand, GameField gameField, char[,] fieldWithQuestionmarks, char[,] fieldWithBombs, int maxScore)
        {
            switch (inputCommand)
            {
                case "top":
                    Draw.ScoreBoard(ScoreBoardTopPlayers);
                    break;

                case "restart":
                    fieldWithQuestionmarks = gameField.Create();
                    fieldWithBombs = gameField.PlaceBombs();

                    Draw.PlayingField(fieldWithQuestionmarks);

                    IsNewGame = true;
                    break;

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

                case "turn":
                    if (fieldWithBombs[this.rowToCheckForBomb, this.colToCheckForBomb] != '*')
                    {
                        IsNewGame = false;
                        this.SetSurroundingBombsCount(fieldWithQuestionmarks, fieldWithBombs, this.rowToCheckForBomb, this.colToCheckForBomb);
                        this.personalScore++;

                        if (maxScore == this.personalScore)
                        {
                            Console.WriteLine("Congrats! You won the game!");

                            Draw.PlayingField(fieldWithBombs);

                            this.EnterScoreToScoreBoard();

                            Draw.ScoreBoard(ScoreBoardTopPlayers);

                            fieldWithQuestionmarks = gameField.Create();
                            fieldWithBombs = gameField.PlaceBombs();

                            this.personalScore = 0;

                            IsNewGame = true;
                        }
                        else
                        {
                            Draw.PlayingField(fieldWithQuestionmarks);
                        }
                    }
                    else
                    {
                        Draw.PlayingField(fieldWithBombs);

                        Console.WriteLine("You just hit a bomb. Sorry.");

                        EnterScoreToScoreBoard();

                        Draw.ScoreBoard(ScoreBoardTopPlayers);

                        fieldWithQuestionmarks = gameField.Create();
                        fieldWithBombs = gameField.PlaceBombs();

                        this.personalScore = 0;

                        IsNewGame = true;
                    }

                    break;

                default:
                    Console.WriteLine("Wrong command: {0}", inputCommand);
                    break;
            }
        }
Пример #3
0
        public static void Main(string[] args)
        {
            string inputCommand = string.Empty;

            GameField gameField = new GameField(10,10);

            char[,] playingField = gameField.Create();
            char[,] bombsField = gameField.PlaceBombs();

            int personalScore = 0;

            bool isBombHit = false;

            List<Player> scoreBoardTopPlayers = new List<Player>(6);

            int row = 0;
            int col = 0;

            bool isNewGame = true;
            bool isWon = false;

            int maxScore = (gameField.FieldCols * gameField.FieldCols) -
                           (gameField.FieldCols + gameField.FieldCols);

            do
            {
                if (isNewGame)
                {
                   isNewGame = Draw.GameLoad(playingField);
                }

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

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

                switch (inputCommand)
                {
                    case "top":
                        ShowScoreBoard(scoreBoardTopPlayers);
                        break;
                    case "restart":
                        playingField = gameField.Create();

                        bombsField = gameField.PlaceBombs();

                        Draw.PlayingField(playingField);

                        isBombHit = false;
                        isNewGame = false;
                        break;
                    case "exit":
                        Console.WriteLine("Good bye.");
                        break;
                    case "turn":
                        if (bombsField[row, col] != '*')
                        {
                            if (bombsField[row, col] == '-')
                            {
                                SetSurroundingBombsCount(playingField, bombsField, row, col);
                                personalScore++;
                            }

                            if (maxScore == personalScore)
                            {
                                isWon = true;
                            }
                            else
                            {
                                Draw.PlayingField(playingField);
                            }
                        }
                        else
                        {
                            isBombHit = true;
                        }

                        break;
                    default:
                        Console.WriteLine("Wrong command.");
                        break;
                }

                if (isBombHit)
                {
                    Draw.PlayingField(bombsField);

                    Console.WriteLine("You just hit a bomb. Sorry.");
                    Console.WriteLine("Enter your nickname for the score board: ", personalScore);

                    string nickname = Console.ReadLine();
                    Player playerPersonalScore = new Player(nickname, personalScore);

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

                    scoreBoardTopPlayers.Sort((Player firstPlayer, Player secondPlayer) => secondPlayer.PlayerName.CompareTo(firstPlayer.PlayerName));
                    scoreBoardTopPlayers.Sort((Player firstPlayer, Player secondPlayer) => secondPlayer.PlayerPoints.CompareTo(firstPlayer.PlayerPoints));
                    ShowScoreBoard(scoreBoardTopPlayers);

                    playingField = gameField.Create();
                    bombsField = gameField.PlaceBombs();

                    personalScore = 0;

                    isBombHit = false;
                    isNewGame = true;
                }

                if (isWon)
                {
                    Console.WriteLine("Congrats! You won the game!");

                    Draw.PlayingField(bombsField);

                    Console.WriteLine("Enter your nickname for the score board: ");
                    string playerNickname = Console.ReadLine();

                    Player playerCurrentScore = new Player(playerNickname, personalScore);
                    scoreBoardTopPlayers.Add(playerCurrentScore);
                    ShowScoreBoard(scoreBoardTopPlayers);

                    playingField = gameField.Create();
                    bombsField = gameField.PlaceBombs();
                    personalScore = 0;

                    isWon = false;
                    isNewGame = true;
                }
            }
            while (inputCommand != "exit");

            Console.WriteLine("Press any key to exit the game.");
            Console.Read();
        }