コード例 #1
0
ファイル: Program.cs プロジェクト: javon27/ConsoleMinesweeper
        static void Main(string[] args)
        {
            Console.SetWindowSize(75, 35);
            Console.BufferWidth = 75;
            Console.Clear();
            Console.WriteLine("Welcome to Minesweeper!");
            Console.Write("Press any key to start...");
            Console.ReadKey();

            bool playAgain;

            do
            {
                playAgain = false;

                var board = new Board(Width, Height, NumBombs);
                board.Draw();
                do
                {
                    // get user input
                    UserCommand uc;
                    bool        valid = GetUserInput(out uc);
                    if (valid)
                    {
                        if (uc.Reveal)
                        {
                            valid = board.RevealCell(uc.x - 1, uc.y - 1);
                        }
                        else
                        {
                            valid = board.ToggleFlag(uc.x - 1, uc.y - 1);
                        }
                    }
                    if (!valid)
                    {
                        Console.Clear();
                        Console.WriteLine("Invalid command. Please try again.");
                        Console.WriteLine("Press any key to continue...");
                        Console.ReadKey();
                    }

                    board.Draw();
                } while (!board.GameOver);

                if (board.IsWinCondition())
                {
                    Console.WriteLine("You WON!!!");
                }
                else
                {
                    Console.WriteLine("You LOST...");
                }

                Console.WriteLine("Would you like to play again? y or n:");
                var key = Console.ReadKey();
                if (key.KeyChar == 'y')
                {
                    playAgain = true;
                }
            } while (playAgain);

            Console.SetCursorPosition(0, Console.WindowHeight - 1);
            Console.Write("Press any key to continue...");
            Console.ReadKey();
        }