Exemplo n.º 1
0
        public TicTacToeGameRules(GameBoard board, GameBoard aiBoard, int length, int depth, List <Player> players)
        {
            myGameBoard   = board;
            aiGameBoard   = aiBoard;
            winningLength = length;
            minimaxDepth  = depth;
            myPlayers     = players;
            string firstPlayer = DetermineFirstPlayer();

            currentPlayer = myPlayers.Find(p => p.myPiece == firstPlayer);
            if (currentPlayer.myPiece == "X")
            {
                opponent = myPlayers.Find(p => p.myPiece == "O");
            }
            else
            {
                opponent = myPlayers.Find(p => p.myPiece == "X");
            }
            gameOver = false;
            do
            {
                myGameBoard.DrawGameBoard();
                if (currentPlayer.myType == "Computer" && opponent.myType == "Computer")
                {
                    Thread.Sleep(1000);
                }
                TakeTurn(myGameBoard, aiBoard, currentPlayer);
                if (!gameOver)
                {
                    SwitchPlayer();
                }
            }while (!gameOver);
        }
Exemplo n.º 2
0
        private void TakeTurn(GameBoard board, GameBoard aiBoard, Player player)
        {
            bool validMove;
            int  row;
            int  col;

            do
            {
                if (player.myType == "Human")
                {
                    row = SelectRow(board);
                    col = SelectColumn(board);
                }
                else if (player.myType == "Computer" && currentPlayer.mySkill == "Amateur")
                {
                    Random rand = new Random();
                    row = rand.Next(board.rows);
                    col = rand.Next(board.cols);
                }
                else if (player.myType == "Computer" && currentPlayer.mySkill == "Expert")
                {
                    AI.ExpertMove(board, aiBoard, player, this, out row, out col);
                }
                else
                {
                    //Skip Turn
                    return;
                }
                validMove = PlacePiece(board, row, col, player);
            } while (!validMove);
            if (CheckForWin(board, row, col, player.myPiece))
            {
                gameOver = true;
                board.DrawGameBoard();
                Console.WriteLine(" Game over! {0} Wins!", player.myPiece);
                return;
            }
            if (CheckForDraw(board))
            {
                gameOver = true;
                board.DrawGameBoard();
                Console.WriteLine(" The game is a draw!");
                return;
            }
            return;
        }