Beispiel #1
0
        public override List <Move> getLegalMoves(Board board)
        {
            List <Move> legalMoves = new List <Move>();

            foreach (int argument in Knight.legalMoveArguments)
            {
                int unCheckedPosition = this.piecePosition + argument;
                if (!BoardUtils.checkedForLegalPosition(unCheckedPosition) ||
                    Knight.firstColumnViolation(this.piecePosition, argument) ||
                    Knight.secondColumnViolation(this.piecePosition, argument) ||
                    Knight.seventhColumnViolation(this.piecePosition, argument) ||
                    Knight.eightColumnViolation(this.piecePosition, argument))
                {
                    continue;
                }
                else
                {
                    Cell currentCell = board.getCell(unCheckedPosition);
                    if (!currentCell.isCellOccupied())
                    {
                        legalMoves.Add(new NormalMove(board, this, unCheckedPosition));
                    }
                    else
                    {
                        if (this.pieceSide != currentCell.getPiece().getSide())
                        {
                            legalMoves.Add(new AttackMove(board, this, unCheckedPosition, currentCell.getPiece()));
                        }
                    }
                }
            }
            return(legalMoves);
        }
Beispiel #2
0
        private int maxValue(Board board, int depth, int alpha, int beta)
        {
            if (depth == 0 || BoardUtils.isBoardinStaleMateorCheckMate(board))
            {
                return(this.heuristic.calculate(board, depth));
            }

            int value = Int32.MinValue;

            //Find state in transposition table
            int key = this.table.hash(board);

            if (this.table.getNode(key, depth, board) != null)
            {
                return(this.table.getNode(key, depth, board).Value);
            }

            //If the current state is not in TT then run minimax;

            Move bestMove = null;

            foreach (Move move in board.CurrentPlayer.getLegalMoves())
            {
                nodeCount++;
                MoveTransition trans = board.CurrentPlayer.makeMove(move);
                if (trans.getMoveStatus().isDone())
                {
                    int temp = minValue(trans.ToBoard, depth - 1, alpha, beta);
                    if (temp > value)
                    {
                        value    = temp;
                        bestMove = move;
                    }
                    if (value > beta)
                    {
                        this.table.add(key, new Node(bestMove, value, depth, board));
                        return(value);
                    }
                    alpha = Math.Max(alpha, value);
                }
            }
            this.table.add(key, new Node(bestMove, value, depth, board));
            return(value);
        }
Beispiel #3
0
        public override List <Move> getLegalMoves(Board board)
        {
            List <Move> legalMove = new List <Move>();

            foreach (int argument in King.legalMoveArguments)
            {
                int unCheckedPosition = this.piecePosition + argument;
                if (!BoardUtils.checkedForLegalPosition(unCheckedPosition) ||
                    King.firstColumnViolation(this.piecePosition, argument) ||
                    King.eightColumnViolation(this.piecePosition, argument))
                {
                    continue;
                }

                else
                {
                    Cell currentCell = board.getCell(unCheckedPosition);
                    if (!currentCell.isCellOccupied())
                    {
                        legalMove.Add(new NormalMove(board, this, unCheckedPosition));
                    }
                    else
                    {
                        if (this.pieceSide != currentCell.getPiece().getSide())
                        {
                            legalMove.Add(new AttackMove(board, this, unCheckedPosition, currentCell.getPiece()));
                        }
                    }
                }
            }
            if (board.CurrentPlayer != null)
            {
                List <Move> currentPlayerMove  = board.CurrentPlayer.getLegalMoves();
                List <Move> opponentPlayerMove = board.CurrentPlayer.getOpponent().getLegalMoves();
                legalMove.AddRange(board.CurrentPlayer.calculateKingCastles(currentPlayerMove, opponentPlayerMove));
            }
            return(legalMove);
        }
Beispiel #4
0
        public override List <Move> getLegalMoves(Board board)
        {
            List <Move> legalMoves = new List <Move>();

            foreach (int argument in Bishop.legalMovesArguments)
            {
                int unCheckedPosition = this.piecePosition;

                while (BoardUtils.checkedForLegalPosition(unCheckedPosition))
                {
                    if (firstColumnViolation(unCheckedPosition, argument) ||
                        eightColumnViolation(unCheckedPosition, argument))
                    {
                        break;
                    }

                    unCheckedPosition += argument;
                    if (BoardUtils.checkedForLegalPosition(unCheckedPosition))
                    {
                        Cell currentCell = board.getCell(unCheckedPosition);
                        if (!currentCell.isCellOccupied())
                        {
                            legalMoves.Add(new NormalMove(board, this, unCheckedPosition));
                        }
                        else
                        {
                            if (this.pieceSide != currentCell.getPiece().getSide())
                            {
                                legalMoves.Add(new AttackMove(board, this, unCheckedPosition, currentCell.getPiece()));
                            }
                            break;
                        }
                    }
                }
            }
            return(legalMoves);
        }
Beispiel #5
0
        public override List <Move> getLegalMoves(Board board)
        {
            List <Move> legalMoves = new List <Move>();

            foreach (int argument in legalMoveArguments)
            {
                int unCheckedPosition = this.piecePosition + (this.direction * argument);

                if (!BoardUtils.checkedForLegalPosition(unCheckedPosition))
                {
                    continue;
                }

                if (argument == 8 && !board.getCell(unCheckedPosition).isCellOccupied())
                {
                    if (this.isPromotionSquare(unCheckedPosition))
                    {
                        legalMoves.Add(new PawnPromotionMove(new NormalMove(board, this, unCheckedPosition), this.getPromotionPiece(PieceType.ROOK)));
                        legalMoves.Add(new PawnPromotionMove(new NormalMove(board, this, unCheckedPosition), this.getPromotionPiece(PieceType.KNIGHT)));
                        legalMoves.Add(new PawnPromotionMove(new NormalMove(board, this, unCheckedPosition), this.getPromotionPiece(PieceType.BISHOP)));
                        legalMoves.Add(new PawnPromotionMove(new NormalMove(board, this, unCheckedPosition), this.getPromotionPiece()));
                    }
                    else
                    {
                        legalMoves.Add(new PawnMove(board, this, unCheckedPosition));
                    }
                }

                if (argument == 16 && this.isFirstMove() &&
                    (this.piecePosition / 8 == 1 && this.isBlack() ||
                     this.piecePosition / 8 == 6 && this.isWhite()))
                {
                    int behindPosition = this.piecePosition + (this.direction * 8);
                    if (!board.getCell(unCheckedPosition).isCellOccupied() &&
                        !board.getCell(behindPosition).isCellOccupied())
                    {
                        legalMoves.Add(new PawnJump(board, this, unCheckedPosition));
                    }
                }

                if (argument == 9 &&
                    !((this.piecePosition % 8 == 7 && this.isBlack() ||
                       (this.piecePosition % 8 == 0 && this.isWhite()))))
                {
                    if (board.getCell(unCheckedPosition).isCellOccupied())
                    {
                        Piece occupiedPiece = board.getCell(unCheckedPosition).getPiece();
                        if (occupiedPiece.getSide() != this.getSide())
                        {
                            if (this.isPromotionSquare(unCheckedPosition))
                            {
                                legalMoves.Add(new PawnPromotionMove(new PawnAttackMove(board, this, unCheckedPosition, occupiedPiece), this.getPromotionPiece(PieceType.ROOK)));
                                legalMoves.Add(new PawnPromotionMove(new PawnAttackMove(board, this, unCheckedPosition, occupiedPiece), this.getPromotionPiece(PieceType.KNIGHT)));
                                legalMoves.Add(new PawnPromotionMove(new PawnAttackMove(board, this, unCheckedPosition, occupiedPiece), this.getPromotionPiece(PieceType.BISHOP)));
                                legalMoves.Add(new PawnPromotionMove(new PawnAttackMove(board, this, unCheckedPosition, occupiedPiece), this.getPromotionPiece()));
                            }
                            else
                            {
                                legalMoves.Add(new PawnAttackMove(board, this, unCheckedPosition, occupiedPiece));
                            }
                        }
                    }
                    else if (board.getEnPassantPawn() != null)
                    {
                        if (board.getEnPassantPawn().getPiecePosition() == this.piecePosition - (this.direction * -1))
                        {
                            Piece enPassantPiece = board.getEnPassantPawn();
                            if (this.getSide() != enPassantPiece.getSide())
                            {
                                legalMoves.Add(new PawnEnPassantAttackMove(board, this, unCheckedPosition, enPassantPiece));
                            }
                        }
                    }
                }

                if (argument == 7 &&
                    !((this.piecePosition % 8 == 0 && this.isBlack() ||
                       (this.piecePosition % 8 == 7 && this.isWhite()))))
                {
                    if (board.getCell(unCheckedPosition).isCellOccupied())
                    {
                        Piece occupiedPiece = board.getCell(unCheckedPosition).getPiece();
                        if (occupiedPiece.getSide() != this.getSide())
                        {
                            if (this.isPromotionSquare(unCheckedPosition))
                            {
                                legalMoves.Add(new PawnPromotionMove(new PawnAttackMove(board, this, unCheckedPosition, occupiedPiece), this.getPromotionPiece(PieceType.ROOK)));
                                legalMoves.Add(new PawnPromotionMove(new PawnAttackMove(board, this, unCheckedPosition, occupiedPiece), this.getPromotionPiece(PieceType.KNIGHT)));
                                legalMoves.Add(new PawnPromotionMove(new PawnAttackMove(board, this, unCheckedPosition, occupiedPiece), this.getPromotionPiece(PieceType.BISHOP)));
                                legalMoves.Add(new PawnPromotionMove(new PawnAttackMove(board, this, unCheckedPosition, occupiedPiece), this.getPromotionPiece()));
                            }
                            else
                            {
                                legalMoves.Add(new PawnAttackMove(board, this, unCheckedPosition, occupiedPiece));
                            }
                        }
                    }
                    else if (board.getEnPassantPawn() != null)
                    {
                        if (board.getEnPassantPawn().getPiecePosition() == this.piecePosition + (this.direction * -1))
                        {
                            Piece enPassantPiece = board.getEnPassantPawn();
                            if (this.getSide() != enPassantPiece.getSide())
                            {
                                legalMoves.Add(new PawnEnPassantAttackMove(board, this, unCheckedPosition, enPassantPiece));
                            }
                        }
                    }
                }
            }
            return(legalMoves);
        }
Beispiel #6
0
 public override string ToString()
 {
     return(BoardUtils.getPositionAtCoordinate(this.DesCoordinate));
 }
Beispiel #7
0
 public override string ToString()
 {
     return(BoardUtils.getPositionAtCoordinate(this.getCurrentCoordinate()) + " " + this.getAttackedPiece().getPieceType().getPieceName()
            + BoardUtils.getPositionAtCoordinate(this.getAttackedPiece().getPiecePosition()));
 }
Beispiel #8
0
 public override string ToString()
 {
     return(this.piece.getPieceType().getPieceName() + BoardUtils.getPositionAtCoordinate(this.getCurrentCoordinate()) + " "
            + BoardUtils.getPositionAtCoordinate(this.DesCoordinate));
 }