コード例 #1
0
        public Square[,] PlayerInput(Square[,] board)
        {
            Console.WriteLine("Player O's turn");

            _scores = new Dictionary <int[], int>();

            Board moveBoard = CloneBoard(board);

            var moves = GameplayHelper.ListEmptySquares(moveBoard.Squares);

            foreach (var move in moves)
            {
                _scores.Add(move, 0);
                Maximise(moveBoard.Squares, move, this.SquareOccupied, 0);
            }

            var optimalMove = _scores.OrderByDescending(score => score.Value).First();

            board[optimalMove.Key[0], optimalMove.Key[1]].State            = this.SquareOccupied;
            board[optimalMove.Key[0], optimalMove.Key[1]].DisplayCharacter = this.DisplayCharacter;
            return(board);
        }
コード例 #2
0
        public Square[,] PlayerInput(Square[,] board)
        {
            Console.WriteLine("Player O's turn");
            var availableMoves = GameplayHelper.ListEmptySquares(board);

            var computerWin = GameplayHelper.CheckForWinningSquare(board, availableMoves, SquareState.O);

            if (computerWin != null)
            {
                board[computerWin[0], computerWin[1]].State            = this.SquareOccupied;
                board[computerWin[0], computerWin[1]].DisplayCharacter = this.DisplayCharacter;
                return(board);
            }

            var humanWin = GameplayHelper.CheckForWinningSquare(board, availableMoves, SquareState.X);

            if (humanWin != null)
            {
                board[humanWin[0], humanWin[1]].State            = this.SquareOccupied;
                board[humanWin[0], humanWin[1]].DisplayCharacter = this.DisplayCharacter;
                return(board);
            }

            var  random       = new Random();
            bool turnComplete = false;

            while (!turnComplete)
            {
                var xCoord = random.Next(board.GetLength(0));
                var yCoord = random.Next(board.GetLength(1));
                if (board[xCoord, yCoord].State.Equals(SquareState.Empty))
                {
                    board[xCoord, yCoord].State            = this.SquareOccupied;
                    board[xCoord, yCoord].DisplayCharacter = this.DisplayCharacter;
                    turnComplete = true;
                }
            }
            return(board);
        }