예제 #1
0
        /// <summary>
        /// Gets the next move as dictated by the neural network.
        /// </summary>
        public Move GetMove(SquareTypes[,] board)
        {
            // Clear the network
            Brain.ResetState();

            // Convert the game board into an input array for the network
            setInputSignalArray(Brain.InputSignalArray, board);

            // Activate the network
            Brain.Activate();

            // Find the highest-scoring available move
            Move move = null;
            double max = double.MinValue;
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                {
                    // If the square is taken, skip it.
                    if (board[i, j] != SquareTypes.N)
                        continue;

                    // Set the score for this square.
                    double score = Brain.OutputSignalArray[i * 3 + j];

                    // If this is the first available move we've found, 
                    // set it to the current best.
                    if (move == null)
                    {
                        move = new Move(i, j);
                        max = score;
                    }
                    // If this square has a higher score than any we've
                    // found, set it to the current best.
                    else if (max < score)
                    {
                        move.X = i;
                        move.Y = j;
                        max = score;
                    }
                }

            return move;
        }
        public Move GetMove(SquareTypes[,] board)
        {
            TicTacToeGame.GetWinner(board);
            int moveNum = 0;
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                    if (board[i, j] != SquareTypes.N)
                        moveNum++;

            //first move is always a corner
            if (moveNum == 0)
                return new Move(0, 0);

            //second move should be the center if free, else a corner
            if (moveNum == 1)
            {
                if (board[1, 1] == SquareTypes.N)
                    return new Move(1, 1);

                return new Move(0, 0);
            }

            //make a winning move if possible
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                {
                    if (board[i, j] != SquareTypes.N)
                        continue;

                    board[i, j] = SquareType;
                    var winner = TicTacToeGame.GetWinner(board);
                    board[i, j] = SquareTypes.N;
                    if (winner == SquareType)
                        return new Move(i, j);
                }

            //if we can't win, check if there are any moves that we have to make
            //to prevent ourselves from losing
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                {
                    if (board[i, j] != SquareTypes.N)
                        continue;

                    //set the move to the opponent's type
                    board[i, j] = SquareType == SquareTypes.X ? SquareTypes.O : SquareTypes.X;
                    var winner = TicTacToeGame.GetWinner(board);
                    board[i, j] = SquareTypes.N;

                    //if the opponent will win by moving here, move here to block them
                    if (winner != SquareTypes.N)
                        return new Move(i, j);
                }

            //if we're here, that means we have made at least 1 move already and can't win
            //nor lose in 1 move, so just make the optimal play which would be to a free
            //corner that isn't blocked
            Move move = null;
            int max = -1;
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                {
                    if (board[i, j] != SquareTypes.N)
                        continue;

                    board[i, j] = SquareType;
                    int count = 0;
                    for (int m = 0; m < 3; m++)
                        for (int n = 0; n < 3; n++)
                        {
                            if (board[m, n] != SquareTypes.N)
                                continue;

                            board[m, n] = SquareType;
                            var winner = TicTacToeGame.GetWinner(board);
                            board[m, n] = SquareTypes.N;
                            if (winner == SquareType)
                                count++;
                        }
                    board[i, j] = SquareTypes.N;
                    if (count > max)
                    {
                        move = new Move(i, j);
                        max = count;
                    }
                }

            return move;
        }
        public Move GetMove(SquareTypes[,] board)
        {
            TicTacToeGame.GetWinner(board);
            int moveNum = 0;

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (board[i, j] != SquareTypes.N)
                    {
                        moveNum++;
                    }
                }
            }

            //first move is always a corner
            if (moveNum == 0)
            {
                return(new Move(0, 0));
            }

            //second move should be the center if free, else a corner
            if (moveNum == 1)
            {
                if (board[1, 1] == SquareTypes.N)
                {
                    return(new Move(1, 1));
                }

                return(new Move(0, 0));
            }

            //make a winning move if possible
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (board[i, j] != SquareTypes.N)
                    {
                        continue;
                    }

                    board[i, j] = SquareType;
                    var winner = TicTacToeGame.GetWinner(board);
                    board[i, j] = SquareTypes.N;
                    if (winner == SquareType)
                    {
                        return(new Move(i, j));
                    }
                }
            }

            //if we can't win, check if there are any moves that we have to make
            //to prevent ourselves from losing
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (board[i, j] != SquareTypes.N)
                    {
                        continue;
                    }

                    //set the move to the opponent's type
                    board[i, j] = SquareType == SquareTypes.X ? SquareTypes.O : SquareTypes.X;
                    var winner = TicTacToeGame.GetWinner(board);
                    board[i, j] = SquareTypes.N;

                    //if the opponent will win by moving here, move here to block them
                    if (winner != SquareTypes.N)
                    {
                        return(new Move(i, j));
                    }
                }
            }

            //if we're here, that means we have made at least 1 move already and can't win
            //nor lose in 1 move, so just make the optimal play which would be to a free
            //corner that isn't blocked
            Move move = null;
            int  max  = -1;

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (board[i, j] != SquareTypes.N)
                    {
                        continue;
                    }

                    board[i, j] = SquareType;
                    int count = 0;
                    for (int m = 0; m < 3; m++)
                    {
                        for (int n = 0; n < 3; n++)
                        {
                            if (board[m, n] != SquareTypes.N)
                            {
                                continue;
                            }

                            board[m, n] = SquareType;
                            var winner = TicTacToeGame.GetWinner(board);
                            board[m, n] = SquareTypes.N;
                            if (winner == SquareType)
                            {
                                count++;
                            }
                        }
                    }
                    board[i, j] = SquareTypes.N;
                    if (count > max)
                    {
                        move = new Move(i, j);
                        max  = count;
                    }
                }
            }

            return(move);
        }