Пример #1
0
        private void HandleVersusAI()
        {
            // Human player always has first move
            if (IsPostBack)
            {
                int computerMove = UnbeatableAI.GetBestMove(GameBoard);
                if (computerMove >= 0)
                {
                    //CurrentPlayer = Square.O;
                    GameBoard[computerMove] = Square.O;
                }
            }

            UpdateImageButtons();

            // Check for game win
            Square winner = GetWinner();

            if (winner != Square.Empty)
            {
                WonGameImage(winner);
            }

            else if (GameBoardFilled())
            {
                TiedGameImage();
            }
        }
Пример #2
0
        /// <summary>
        /// Checks the game board for a winning combination and returns the winner.
        /// </summary>
        /// <returns>The player who won or Square.Empty if there is no winner.</returns>
        private Square GetWinner()
        {
            Square[,] board2D = UnbeatableAI.To2DArray(GameBoard);

            // Check rows
            for (int row = 0; row < 3; row++)
            {
                if (board2D[row, 0] == board2D[row, 1] &&
                    board2D[row, 1] == board2D[row, 2] &&
                    board2D[row, 0] != Square.Empty)
                {
                    return(board2D[row, 0]);
                }
            }

            // Check columns
            for (int col = 0; col < 3; col++)
            {
                if (board2D[0, col] == board2D[1, col] &&
                    board2D[1, col] == board2D[2, col] &&
                    board2D[0, col] != Square.Empty)
                {
                    return(board2D[0, col]);
                }
            }

            // Check diagonals
            if ((board2D[0, 0] == board2D[1, 1] && board2D[1, 1] == board2D[2, 2] ||
                 board2D[0, 2] == board2D[1, 1] && board2D[1, 1] == board2D[2, 0]) &&
                board2D[1, 1] != Square.Empty)
            {
                return(board2D[1, 1]);
            }
            return(Square.Empty);
        }
Пример #3
0
        public GameManager()
        {
            var running = true;

            Console.WriteLine("Hello, welcome in Tic Tac Toe. Choose your game mode pleas.");
            while (running)
            {
                Console.WriteLine("Game modes are PvP, EasyAI, NormalAI, UnbeatableAI. For quit write Quit.");
                var input = Console.ReadLine();

                switch (input)
                {
                case "EasyAI":
                    Console.Clear();
                    EasyAI.Start();
                    break;

                case "NormalAI":
                    Console.Clear();
                    NormaAI.Start();
                    break;

                case "UnbeatableAI":
                    Console.Clear();
                    UnbeatableAI.Start();
                    break;

                case "PvP":
                    Console.Clear();
                    PvP.Start();
                    break;

                case "Quit":
                    running = false;
                    break;

                default:
                    Console.WriteLine("Invalid input. This program is case sensitive.");
                    Thread.Sleep(2000);
                    Console.Clear();
                    break;
                }
            }
        }