Exemplo n.º 1
0
        public static void Main()
        {
            string command = string.Empty;

            char[,] playingField = DrawField();
            char[,] mines        = SetMnes();
            int  counter       = 0;
            bool isMineClicked = false;
            List <PlayerScore> highScorePlayers = new List <PlayerScore>(6);
            int       row        = 0;
            int       column     = 0;
            bool      isNewGame  = true;
            const int MaxCells   = 35;
            bool      isMaxCells = false;

            do
            {
                if (isNewGame)
                {
                    Console.WriteLine("Start playing \"Minesweeper\". Try to find fields without mines." +
                                      " Commands:\n -'top' shows rating;\n -'restart' start new game;\n -'exit' close program!");
                    UpdatePlayingField(playingField);
                    isNewGame = 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 column) &&
                        row <= playingField.GetLength(0) && column <= playingField.GetLength(1))
                    {
                        command = "turn";
                    }
                }

                switch (command)
                {
                case "top":
                    Rate(highScorePlayers);
                    break;

                case "restart":
                    playingField = DrawField();
                    mines        = SetMnes();
                    UpdatePlayingField(playingField);
                    isMineClicked = false;
                    isNewGame     = false;
                    break;

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

                case "turn":
                    if (mines[row, column] != '*')
                    {
                        if (mines[row, column] == '-')
                        {
                            Turn(playingField, mines, row, column);
                            counter++;
                        }

                        if (MaxCells == counter)
                        {
                            isMaxCells = true;
                        }
                        else
                        {
                            UpdatePlayingField(playingField);
                        }
                    }
                    else
                    {
                        isMineClicked = true;
                    }

                    break;

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

                if (isMineClicked)
                {
                    UpdatePlayingField(mines);
                    Console.Write("\nGame over with {0} scores. Enter your nickname: ", counter);
                    string      nickname    = Console.ReadLine();
                    PlayerScore playerScore = new PlayerScore(nickname, counter);

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

                    highScorePlayers.Sort((PlayerScore ps1, PlayerScore ps2) => ps2.Name.CompareTo(ps1.Name));
                    highScorePlayers.Sort((PlayerScore ps1, PlayerScore ps2) => ps2.Score.CompareTo(ps1.Score));
                    Rate(highScorePlayers);

                    playingField  = DrawField();
                    mines         = SetMnes();
                    counter       = 0;
                    isMineClicked = false;
                    isNewGame     = true;
                }

                if (isMaxCells)
                {
                    Console.WriteLine("\nCongratulations! You open 35 cells and win.");
                    UpdatePlayingField(mines);
                    Console.WriteLine("Enter your nickname: ");
                    string      playerName = Console.ReadLine();
                    PlayerScore score      = new PlayerScore(playerName, counter);
                    highScorePlayers.Add(score);
                    Rate(highScorePlayers);
                    playingField = DrawField();
                    mines        = SetMnes();
                    counter      = 0;
                    isMaxCells   = false;
                    isNewGame    = true;
                }
            }while (command != "exit");
            Console.WriteLine("Made in Bulgaria");
            Console.Read();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            string command = string.Empty;

            char[,] playField = CreatePlayField();
            char[,] mines     = placeMines();
            int  counter                  = 0;
            bool isMineHitted             = false;
            List <PlayerScore> highScores = new List <PlayerScore>(6);
            int       row                 = 0;
            int       column              = 0;
            bool      isStartingNewGame   = true;
            const int MaxCells            = 35; // or MAX_CELLS
            bool      isGameWon           = false;

            do
            {
                if (isStartingNewGame)
                {
                    Console.WriteLine("Lets play “Minesweeper”. Try our luck to find the fields without mines." +
                                      " Comman 'top' show the score chart, 'restart' start new game, 'exit' exit the game!");

                    DrawGameField(playField);
                    isStartingNewGame = 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 column) &&
                        row <= playField.GetLength(0) && column <= playField.GetLength(1))
                    {
                        command = "turn";
                    }
                }

                switch (command)
                {
                case "top":
                    ShowScoreChart(highScores);
                    break;

                case "restart":
                    playField = CreatePlayField();
                    mines     = placeMines();
                    DrawGameField(playField);
                    isMineHitted      = false;
                    isStartingNewGame = false;
                    break;

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

                case "turn":
                    if (mines[row, column] != '*')
                    {
                        if (mines[row, column] == '-')
                        {
                            MakeMove(playField, mines, row, column);
                            counter++;
                        }

                        if (MaxCells == counter)
                        {
                            isGameWon = true;
                        }
                        else
                        {
                            DrawGameField(playField);
                        }
                    }
                    else
                    {
                        isMineHitted = true;
                    }

                    break;

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

                if (isMineHitted)
                {
                    DrawGameField(mines);
                    Console.Write("\nHrrrrrr! You are dead with {0} points. " +
                                  "Write yor nickname: ", counter);

                    string      nickname      = Console.ReadLine();
                    PlayerScore currentPlayer = new PlayerScore(nickname, counter);

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

                    highScores.Sort((PlayerScore r1, PlayerScore r2) => r2.Name.CompareTo(r1.Name));
                    highScores.Sort((PlayerScore r1, PlayerScore r2) => r2.Score.CompareTo(r1.Score));
                    ShowScoreChart(highScores);

                    playField         = CreatePlayField();
                    mines             = placeMines();
                    counter           = 0;
                    isMineHitted      = false;
                    isStartingNewGame = true;
                }

                if (isGameWon)
                {
                    Console.WriteLine("\nCongratulations! You've opened 35 cells!");
                    DrawGameField(mines);

                    Console.WriteLine("Enter your nickname: ");
                    string nickname = Console.ReadLine();

                    PlayerScore currentPlayerScore = new PlayerScore(nickname, counter);
                    highScores.Add(currentPlayerScore);
                    ShowScoreChart(highScores);

                    playField         = CreatePlayField();
                    mines             = placeMines();
                    counter           = 0;
                    isGameWon         = false;
                    isStartingNewGame = true;
                }
            }while (command != "exit");

            Console.WriteLine("Made in Bulgaria - Uauahahahahaha!");
            Console.WriteLine("Bye!");
            Console.Read();
        }