예제 #1
0
파일: AI.cs 프로젝트: Flyboy1010/ChessAI
        public Move GetBestMove(Board board)
        {
            int  bestValue = int.MinValue;
            Move bestMove  = null;

            List <Move> pseudoLegalMoves = MoveGeneration.GenerateAllMoves(board, Piece.black);

            SortMoves(board, pseudoLegalMoves);

            foreach (var move in pseudoLegalMoves)
            {
                board.MakeMove(move, true);

                if (MoveGeneration.IsInCheck(board, Piece.black))
                {
                    board.UnMakeMove(true);
                    continue;
                }

                int value = Minimax(board, depth, int.MinValue, int.MaxValue, false);

                board.UnMakeMove(true);

                if (value >= bestValue)
                {
                    bestValue = value;
                    bestMove  = move;
                }
            }

            return(bestMove);
        }
예제 #2
0
파일: AI.cs 프로젝트: Flyboy1010/ChessAI
        private int Minimax(Board board, int depth, int alpha, int beta, bool isMaximizingPlayer)
        {
            if (depth == 0)
            {
                return(Evaluate(board));
            }

            if (isMaximizingPlayer)
            {
                int bestValue = int.MinValue;

                List <Move> pseudoLegalMoves = MoveGeneration.GenerateAllMoves(board, Piece.black);

                SortMoves(board, pseudoLegalMoves);

                foreach (var move in pseudoLegalMoves)
                {
                    board.MakeMove(move, true);

                    if (MoveGeneration.IsInCheck(board, Piece.black))
                    {
                        board.UnMakeMove(true);
                        continue;
                    }

                    int value = Minimax(board, depth - 1, alpha, beta, false);

                    board.UnMakeMove(true);

                    bestValue = Math.Max(value, bestValue);

                    alpha = Math.Max(alpha, value);

                    if (beta <= alpha)
                    {
                        break;
                    }
                }

                return(bestValue);
            }
            else
            {
                int bestValue = int.MaxValue;

                List <Move> pseudoLegalMoves = MoveGeneration.GenerateAllMoves(board, Piece.white);

                SortMoves(board, pseudoLegalMoves);

                foreach (var move in pseudoLegalMoves)
                {
                    board.MakeMove(move, true);

                    if (MoveGeneration.IsInCheck(board, Piece.white))
                    {
                        board.UnMakeMove(true);
                        continue;
                    }

                    int value = Minimax(board, depth - 1, alpha, beta, true);

                    board.UnMakeMove(true);

                    bestValue = Math.Min(value, bestValue);

                    beta = Math.Min(beta, value);

                    if (beta <= alpha)
                    {
                        break;
                    }
                }

                return(bestValue);
            }
        }
예제 #3
0
        // Called every frame. 'delta' is the elapsed time since the previous frame.
        public override void _Process(float delta)
        {
            if (!Engine.EditorHint)
            {
                if (turnPlayer)
                {
                    if (Input.IsActionJustPressed("Select"))
                    {
                        // get mouse position
                        Vector2 mouse = GetViewport().GetMousePosition() - boardUI.Position;

                        // get position in grid
                        int squareX        = (int)(mouse.x / boardUI.scale);
                        int squareY        = (int)(mouse.y / boardUI.scale);
                        int selectedSquare = squareX + squareY * 8;

                        // check if it is inside the bounds
                        if (IsInBounds(squareX, squareY))
                        {
                            // get the piece if there is one
                            if (!Piece.IsPieceType(board.pieces[selectedSquare], Piece.none) && Piece.IsPieceColor(board.pieces[selectedSquare], Piece.white))
                            {
                                selectedPieceSquare = selectedSquare;
                                isPieceSelected     = true;

                                legalMoves = MoveGeneration.GenerateLegalMoves(board, selectedSquare);
                            }
                            else
                            {
                                legalMoves.Clear();
                            }
                        }
                        else
                        {
                            legalMoves.Clear();
                        }
                    }
                    else if (Input.IsActionJustReleased("Select"))
                    {
                        if (isPieceSelected)
                        {
                            // get mouse position
                            Vector2 mouse = GetViewport().GetMousePosition() - boardUI.Position;

                            int squareX      = (int)(mouse.x / boardUI.scale);
                            int squareY      = (int)(mouse.y / boardUI.scale);
                            int targetSquare = squareX + squareY * 8;

                            // check if it is inside the bounds
                            if (IsInBounds(squareX, squareY))
                            {
                                foreach (var move in legalMoves)
                                {
                                    if (targetSquare == move.targetSquare)
                                    {
                                        if (Piece.IsPieceType(board.pieces[targetSquare], Piece.none))
                                        {
                                            moveSound.Play();
                                        }
                                        else
                                        {
                                            captureSound.Play();
                                        }

                                        board.MakeMove(move);

                                        turnPlayer = false;

                                        legalMoves.Clear();

                                        break;
                                    }
                                }
                            }

                            boardUI.UpdateBoardPieces(board);

                            isPieceSelected = false;
                        }
                    }

                    if (Input.IsActionPressed("Select"))
                    {
                        if (isPieceSelected)
                        {
                            // get mouse position
                            Vector2 mouse = GetViewport().GetMousePosition() - boardUI.Position;

                            Sprite piece = boardUI.piecesSprites[selectedPieceSquare];
                            piece.Position = mouse + new Vector2(-boardUI.scale / 2f, -boardUI.scale / 2f);
                            piece.ZIndex   = 1;
                        }
                    }

                    if (Input.IsActionJustPressed("Restore"))
                    {
                        // unmake both moves your move and black pieces move
                        board.UnMakeMove();
                        board.UnMakeMove();

                        boardUI.UpdateBoardPieces(board);

                        legalMoves.Clear();
                    }
                }
                else
                {
                    /*
                     *      I DONT THINK THIS IS THE RIGHT WAY TO DO THIS
                     *      IF I DONT USE A THREAD, IF IT TAKES "TOO MUCH TIME"
                     *      WINDOWS WILL THINK IT STOPPED WORKING,
                     *      I AM NOT REALLY HAPPY WITH THIS SOLUTION
                     */
                    if (!calculating)
                    {
                        calculating = true;

                        System.Threading.Thread t = new System.Threading.Thread(new ThreadStart(ComputerTurn));

                        t.Start();
                    }
                }

                boardUI.ResetBoardSquares();
                boardUI.ViewLastMove(board);
                boardUI.ViewLegalMovements(board, legalMoves);
            }
        }