Exemplo n.º 1
0
 /*
  * Returns true if a move is valid for a given position
  */
 public static Boolean isMoveValid(Move move, Position position)
 {
     if (move == null)
     {
         return false;
     }
     ArrayList moves = new MoveGenerator().legalMoves(position);
     foreach (Move listMove in moves)
     {
         //Move is only valid if it appears in legal moves list
         if (move.Equals(listMove))
         {
             return true;
         }
     }
     return false;
 }
Exemplo n.º 2
0
        /*
         * Converts a move object into a UCI move string (long algebraic
         * notation), a move list can be supplied to search through.
         */
        public static String moveObjectToString(Move move, Position position, ArrayList moves)
        {
            if ((move == null) || move.Equals(new Move(0, 0, 0)))
            {
                return "--";
            }
            StringBuilder moveString = new StringBuilder();
            int whiteKingOrigin = Position.getSquare(4, 0);
            int blackKingOrigin = Position.getSquare(4, 7);
            if ((move.origin == whiteKingOrigin) && (position.getPiece(whiteKingOrigin) == PieceType.K))
            {
                //Check white castle
                if (move.destination == Position.getSquare(6, 0))
                {
                    moveString.Append("O-O");
                }
                else if (move.destination == Position.getSquare(2, 0))
                {
                    moveString.Append("O-O-O");
                }
            }
            else if ((move.origin == blackKingOrigin) && (position.getPiece(blackKingOrigin) == PieceType.k))
            {
                //Check black castle
                if (move.destination == Position.getSquare(6, 7))
                {
                    moveString.Append("O-O");
                }
                else if (move.destination == Position.getSquare(2, 7))
                {
                    moveString.Append("O-O-O");
                }
            }
            if (moveString.Length == 0)
            {
                PieceType piece = position.getPiece(move.origin);
                moveString.Append("" + piece);

                int originFile = Position.getRank(move.origin);
                int originRank = Position.getFile(move.origin);
                int destinationFile = Position.getRank(move.destination);
                int destinationRank = Position.getFile(move.destination);

                //Long Algebraic Notation-----
                moveString.Append((char)(originFile + 'a'));
                moveString.Append((char)(originRank + '1'));
                moveString.Append(isMoveCapture(move, position) ? 'x' : '-');
                //----------------------------
                moveString.Append((char)(destinationFile + 'a'));
                moveString.Append((char)(destinationRank + '1'));
                if (move.promoteTo != PieceType.Empty)
                {
                    moveString.Append("" + piece);
                }
            }
            UnMakeInfo unMake = new UnMakeInfo();
            position.makeMove(move, unMake);
            Boolean inCheck = MoveGenerator.inCheck(position);
            if (inCheck)
            {
                ArrayList nextMoves = MoveGenerator.mgInstance.legalMoves(position);
                if (nextMoves.Count == 0)
                {
                    moveString.Append('#');
                }
                else
                {
                    moveString.Append('+');
                }
            }
            position.unMakeMove(move, unMake);

            return moveString.ToString();
        }