Пример #1
0
        static void Main(string[] args)
        {

            while (true)
            {
                Console.WriteLine("Please enter X or O as your token for the game: ");
                char userToken = Console.ReadLine().Trim().ToUpper()[0];
                if (userToken == '0')
                    userToken = 'O';
                char aiToken = userToken.Equals('X') ? 'O' : 'X';

                Console.WriteLine();
                Console.WriteLine("Do you want to start the game?");
                Console.WriteLine("Type Y for YES");
                Console.WriteLine("Type N for NO");
                char start = Console.ReadLine().Trim().ToUpper()[0];

                bool userStarts = start.Equals('Y') ? true : false;

                TicTacToe ttt = new TicTacToe(userToken, aiToken, userStarts);
                AI ai = new AI(ttt);

                bool isThereWinner = false;
                bool isDraw = false;

                while (!isThereWinner && !isDraw)
                {
                    ttt.toggleTurn();
                    if (ttt.isUserTurn())
                    {
                        short pos;
                        do
                        {
                            Console.WriteLine("Enter a valid position to insert your token: ");
                            pos = Int16.Parse(Console.ReadLine()[0].ToString());
                        } while (!ttt.isValidMove(pos));
                        
                        ttt.play(pos);
                    }
                    else
                    {
                        ai.play();
                    }
                    isThereWinner = ttt.isThereAWinner();
                    isDraw = ttt.isDraw();
                }

                if (isThereWinner)
                {
                    Console.WriteLine("We have a WINNER!");
                    Console.WriteLine(ttt.getWinner() + " is the winner!!!");
                }
                else
                {
                    Console.WriteLine("Its a DRAW!");
                }
                
                Console.WriteLine();
                Console.WriteLine("Do you want to play another game? If yes, press Y else press any other key.");
                string toContinue = Console.ReadLine()[0].ToString();
                if (!toContinue.Equals("Y", StringComparison.InvariantCultureIgnoreCase))
                    break;
            }
        }