コード例 #1
0
ファイル: Engine.cs プロジェクト: AsenTahchiyski/SoftUni
        public void Run()
        {
            do
            {
                if (!this.isRunning)
                {
                    this.renderer.WriteLine("Welcome to Minesweeper! Try to find all cells that do not contain a mine.\n" +
                                      "The 'top' command shows the highscores, 'restart' starts a new game,\n" +
                                      "'exit' stops the game.");
                    this.DrawPlayField(this.playField);
                    this.isRunning = true;
                }

                this.renderer.Write("Please provide row and column numbers: ");
                this.command = this.renderer.ReadLine().Trim();
                if (this.command.Length >= 3)
                {
                    if (int.TryParse(this.command[0].ToString(), out this.row) && int.TryParse(this.command[2].ToString(), out this.column)
                        && this.row <= this.playField.GetLength(0) && this.column <= this.playField.GetLength(1))
                    {
                        this.command = "turn";
                    }
                }

                switch (this.command)
                {
                    case "top":
                        this.ShowHighScores(this.highscores);
                        break;
                    case "restart":
                        this.playField = CreatePlayField();
                        this.mineField = PopulateMinesInField();
                        this.DrawPlayField(this.playField);
                        this.isPlayerAlive = true;
                        this.isRunning = true;
                        break;
                    case "exit":
                        this.renderer.WriteLine("Bye-bye!");
                        break;
                    case "turn":
                        if (this.mineField[this.row, this.column] != '*')
                        {
                            if (this.mineField[this.row, this.column] == '-')
                            {
                                this.ProcessGameTurn(this.playField, this.mineField, this.row, this.column);
                                this.currentPoints++;
                            }

                            if (MaxNumberOfMines == this.currentPoints)
                            {
                                this.areAllMinesFound = true;
                            }
                            else
                            {
                                this.DrawPlayField(this.playField);
                            }
                        }
                        else
                        {
                            this.isPlayerAlive = false;
                        }

                        break;
                    default:
                        this.renderer.WriteLine("\nInvalid command.\n");
                        break;
                }

                if (!this.isPlayerAlive)
                {
                    this.DrawPlayField(this.mineField);
                    this.renderer.Write(string.Format("\nOh noes! You've stepped on a mine! Your score is {0}. " + "Enter your name: ", this.currentPoints));
                    string name = this.renderer.ReadLine();
                    IPlayer t = new Player(name, this.currentPoints);
                    if (this.highscores.Count < 5)
                    {
                        this.highscores.Add(t);
                    }
                    else
                    {
                        for (int i = 0; i < this.highscores.Count; i++)
                        {
                            if (this.highscores[i].Points < t.Points)
                            {
                                this.highscores.Insert(i, t);
                                this.highscores.RemoveAt(this.highscores.Count - 1);
                                break;
                            }
                        }
                    }

                    this.highscores.Sort((IPlayer r1, IPlayer r2) => r2.Name.CompareTo(r1.Name));
                    this.highscores.Sort((IPlayer r1, IPlayer r2) => r2.Points.CompareTo(r1.Points));
                    this.ShowHighScores(this.highscores);

                    this.playField = CreatePlayField();
                    this.mineField = PopulateMinesInField();
                    this.currentPoints = 0;
                    this.isPlayerAlive = true;
                    this.isRunning = false;
                }

                if (this.areAllMinesFound)
                {
                    this.renderer.WriteLine("\nCongratulations! You've opened 35 cells without stepping on a mine!");
                    this.DrawPlayField(this.mineField);
                    this.renderer.WriteLine("Enter your name: ");
                    string name = this.renderer.ReadLine();
                    IPlayer player = new Player(name, this.currentPoints);
                    this.highscores.Add(player);
                    this.ShowHighScores(this.highscores);
                    this.playField = CreatePlayField();
                    this.mineField = PopulateMinesInField();
                    this.currentPoints = 0;
                    this.areAllMinesFound = false;
                    this.isRunning = false;
                }
            }
            while (this.command != "exit");

            this.renderer.Read();
        }
コード例 #2
0
        public static void Main()
        {
            const int AllCells = 35;

            var winners = new List<Player>(6);
            var command = string.Empty;
            var board = LoadBoard();
            var bombs = LoadBombs();
            var isGameOver = false;
            var isStart = true;
            var areAllCellsOpen = false;
            var openCellCounter = 0;
            var row = 0;
            var col = 0;

            do
            {
                if (isStart)
                {
                    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!");
                    PrintBoard(board);
                    isStart = 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":
                        Ranking(winners);
                        break;
                    case "restart":
                        board = LoadBoard();
                        bombs = LoadBombs();
                        PrintBoard(board);
                        isGameOver = false;
                        isStart = false;
                        break;
                    case "exit":
                        Console.WriteLine("4a0, 4a0, 4a0!");
                        break;
                    case "turn":
                        if (bombs[row, col] != '*')
                        {
                            if (bombs[row, col] == '-')
                            {
                                YourTurn(board, bombs, row, col);
                                openCellCounter++;
                            }

                            if (AllCells == openCellCounter)
                            {
                                areAllCellsOpen = true;
                            }
                            else
                            {
                                PrintBoard(board);
                            }
                        }
                        else
                        {
                            isGameOver = true;
                        }

                        break;
                    default:
                        Console.WriteLine("\nInvalid command!\n");
                        break;
                }
                if (isGameOver)
                {
                    PrintBoard(bombs);
                    Console.Write("\nHrrrrrr! Umria gerojski s {0} to4ki. Daj si niknejm: ", openCellCounter);
                    var nickname = Console.ReadLine();
                    var player = new Player(nickname, openCellCounter);
                    if (winners.Count < 5)
                    {
                        winners.Add(player);
                    }
                    else
                    {
                        for (int i = 0; i < winners.Count; i++)
                        {
                            if (winners[i].Points < player.Points)
                            {
                                winners.Insert(i, player);
                                winners.RemoveAt(winners.Count - 1);
                                break;
                            }
                        }
                    }

                    winners.Sort((Player firstPlayer, Player secondPlayer) => secondPlayer.Name.CompareTo(firstPlayer.Name));
                    winners.Sort((Player firstPlayer, Player secondPlayer) => secondPlayer.Points.CompareTo(firstPlayer.Points));
                    Ranking(winners);

                    board = LoadBoard();
                    bombs = LoadBombs();
                    openCellCounter = 0;
                    isGameOver = false;
                    isStart = true;
                }
                if (areAllCellsOpen)
                {
                    Console.WriteLine("\nBRAVOOOS! Otvri 35 kletki bez kapka kryv.");
                    PrintBoard(bombs);
                    Console.WriteLine("Daj si imeto, batka: ");
                    var name = Console.ReadLine();
                    var player = new Player(name, openCellCounter);
                    winners.Add(player);
                    Ranking(winners);
                    board = LoadBoard();
                    bombs = LoadBombs();
                    openCellCounter = 0;
                    areAllCellsOpen = false;
                    isStart = true;
                }
            }
            while (command != "exit");

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