Example #1
0
        public bool MakeMove(TicTacToeMove move, TicTacToeBoardValue boardValue)
        {
            bool isValid = this.IsValidMove(move);

            if (isValid)
            {
                this.values[move.X, move.Y] = boardValue;
            }
            return(isValid);
        }
Example #2
0
        private TicTacToeMove CheckForWin(TicTacToeGameState gameState, TicTacToeBoardValue boardValue)
        {
            IEnumerable <TicTacToeMove> openMoves = gameState.Board.GetOpenMoves();

            foreach (TicTacToeMove move in openMoves)
            {
                gameState.Board.MakeMove(move, boardValue);

                TicTacToeBoardValue?winner = gameState.Board.CheckForWinner();
                if (winner == boardValue)
                {
                    return(move);
                }

                gameState.Board.ResetPosition(move.X, move.Y);
            }

            return(null);
        }
Example #3
0
        public override TicTacToeMove NextMove(TicTacToeGameState gameState)
        {
            // Can I win?
            TicTacToeMove winningMove = this.CheckForWin(gameState, this.BoardValue);

            // Can they win?
            TicTacToeBoardValue otherBoardValue = this.BoardValue == TicTacToeBoardValue.O ?
                                                  TicTacToeBoardValue.X : TicTacToeBoardValue.O;
            TicTacToeMove blockingMove = this.CheckForWin(gameState, otherBoardValue);

            // Can I play a corner?
            TicTacToeMove cornerMove = new TicTacToeMove[]
            {
                new TicTacToeMove(0, 0),
                new TicTacToeMove(0, 2),
                new TicTacToeMove(2, 0),
                new TicTacToeMove(2, 2)
            }.OrderBy(move => Guid.NewGuid()).FirstOrDefault(move => gameState.Board.IsValidMove(move));

            // play best move
            return(winningMove ?? blockingMove ?? cornerMove ?? base.NextMove(gameState));
        }
 public TicTacToeRandomPlayer(TicTacToeBoardValue boardValue)
     : base(boardValue)
 {
 }
 public TicTacToePlayer(TicTacToeBoardValue boardValue)
 {
     this.BoardValue = boardValue;
 }
Example #6
0
 public TicTacToeStrategyPlayer(TicTacToeBoardValue boardValue)
     : base(boardValue)
 {
 }
Example #7
0
 public TicTacToeHumanPlayer(TicTacToeBoardValue boardValue)
     : base(boardValue)
 {
 }