コード例 #1
0
ファイル: Piece.cs プロジェクト: ProjektSI2018/SI_Chess_AI
        public List <Point> GetValidMoves(Board board, Point myPos)
        {
            List <Point> possibleMoves = GetMoves(board, myPos);
            bool         promoted;

            //additional validation for all pieces
            for (int i = 0; i < possibleMoves.Count; i++)
            {
                promoted = false;
                //would this move cause my king to be checked
                Point[] mov = { myPos, possibleMoves[i] };
                board.Execute(mov, true); //but don't check for check

                promoted = board.LatestMoved.hasPromoted;
                bool       check = board.PiecesCheckingKing(Color, true).Count != 0;
                Board.Move move  = board.LatestMoved;
                board.UndoMove(1);
                if (!check && promoted)
                {
                    for (int j = 1; j < Board.promoteOptions.Length; j++)
                    {
                        board.Execute(mov, true, j); //but don't check for check
                        promoted = board.LatestMoved.hasPromoted;
                        check    = board.PiecesCheckingKing(Color, true).Count != 0;

                        board.UndoMove(1);
                        if (check)
                        {
                            break;
                        }
                    }
                }
                if (check)
                {
                    possibleMoves.Remove(possibleMoves[i]);
                    i--;
                }
            }
            return(possibleMoves);
        }
コード例 #2
0
        private Node DoAlphaBeta(bool isMin, Board board, int depth, double alpha = 0, double beta = 1)
        {
            Node MinMax = new Node()
            {
                eval = isMin ? 1 : 0
            };

            if (depth == 0)
            {
                return(new Node()
                {
                    move = null, eval = board.Evaluate(color)
                });
            }
            List <Point> playerPieces = board.GetAllPiecesPositions(isMin ? Board.OppositeColor(color) : color);

            foreach (Point piecePos in playerPieces)
            {
                foreach (Point move in board.BoardTab[piecePos.x, piecePos.y].GetValidMoves(board, piecePos))
                {
                    Point[] tempMove = new Point[2] {
                        piecePos, move
                    };

                    board.Execute(tempMove);

                    Node result = DoAlphaBeta(!isMin, board, depth - 1, alpha, beta);

                    board.UndoMove(1);

                    if ((isMin && result.eval <= MinMax.eval) || (!isMin && result.eval >= MinMax.eval))
                    {
                        MinMax.eval = result.eval;
                        MinMax.move = tempMove;
                    }
                    if (isMin)
                    {
                        beta = Math.Min(result.eval, beta);
                    }
                    else
                    {
                        alpha = Math.Max(result.eval, alpha);
                    }
                    if (beta <= alpha)
                    {
                        return(MinMax);
                    }
                }
            }
            return(MinMax);
        }
コード例 #3
0
        public List <Point> GetValidMoves(Board board, Point myPos)
        {
            List <Point> possibleMoves = GetMoves(board, myPos);

            //additional validation for all pieces
            for (int i = 0; i < possibleMoves.Count; i++)
            {
                //would this move cause my king to be checked
                Point[] mov = { myPos, possibleMoves[i] };
                board.Execute(mov);                 //but don't check for check
                bool check = board.PiecesCheckingKing(color, true).Count != 0;
                board.UndoMove(1);
                if (check)
                {
                    possibleMoves.Remove(possibleMoves[i]);
                    i--;
                }
            }
            return(possibleMoves);
        }