示例#1
0
        static void Main(string[] args)
        {
            var rules = new List <baseRule>();

            rules.Add(new RowRule());
            rules.Add(new ColumnRule());
            rules.Add(new DiagonalRule());
            var gameBoard = new GameBoard(PlayerSymbol.Circle, rules);

            Console.WriteLine(WELCOME_MESSAGE);
            Console.WriteLine(INITIALIZE_MESSAGE);
            Console.WriteLine(DISPLAY_CURRENT_BOARD);
            DisplayCurrentBoard(gameBoard.Board);
            Console.WriteLine(ASK_FOR_MOVE);
            Console.WriteLine(MOVE_EXAMPLE);

            var humanPlayer = new HumanPlayer(PlayerSymbol.Circle);
            var aiPlayer    = new AIPlayer(PlayerSymbol.Cross, new RandomStrategy());

            while (!(gameBoard.IsWinning() || gameBoard.IsGameEnd()))
            {
                Move humanMove = null;
                var  userInput = Console.ReadLine().Trim();
                while (humanMove == null || !ValidateInput(userInput) || !gameBoard.ValidateMove(humanMove))
                {
                    if (!ValidateInput(userInput) || (humanMove != null && !gameBoard.ValidateMove(humanMove)))
                    {
                        Console.WriteLine(INVALID_MOVE);
                        DisplayCurrentBoard(gameBoard.Board);
                        Console.WriteLine(ASK_FOR_MOVE);
                        userInput = Console.ReadLine().Trim();
                    }

                    humanMove = humanPlayer.MakeAMove(Int32.Parse(userInput[0].ToString()) - MOVE_NORMALIZATION_MODIFIER, Int32.Parse(userInput[2].ToString()) - MOVE_NORMALIZATION_MODIFIER);
                }
                gameBoard.TakeAMove(humanMove);
                DisplayCurrentBoard(gameBoard.Board);

                if (gameBoard.IsWinning() || gameBoard.IsGameEnd())
                {
                    break;
                }

                var aiMove = aiPlayer.MakeAMove(gameBoard.Board);
                while (!gameBoard.ValidateMove(aiMove))
                {
                    aiMove = aiPlayer.MakeAMove(gameBoard.Board);
                }
                Console.WriteLine($"{AI_MOVE_MESSAGE} X: {aiMove.Position.X + MOVE_NORMALIZATION_MODIFIER}, Y: {aiMove.Position.Y + MOVE_NORMALIZATION_MODIFIER}");
                gameBoard.TakeAMove(aiMove);
                DisplayCurrentBoard(gameBoard.Board);
            }

            if (gameBoard.Winner != null)
            {
                Console.WriteLine($"{WINNER_MESSAGE} {gameBoard.Winner.Name}");
            }
            else if (gameBoard.GameEnd)
            {
                Console.WriteLine(DRAW_MESSAGE);
            }
            Console.ReadKey();
        }