Ejemplo n.º 1
0
        private static bool pawnValid(Piece p, Position from, Position to)
        {
            int xDist = from.x - to.x;
            int yDist = from.y - to.y;

            int direction = p.getColor() == "white" ? -1 : 1;

            if (from.y + direction > 7 || from.y + direction < 0)
            {
                return(false);
            }

            if (from.y + direction != to.y && ((from.y + 2 * direction != to.y && p.getNumberOfMoves() == 0) || !Chess.IsEmpty(new Position(from.x, from.y + direction)) || p.getNumberOfMoves() > 0))
            {
                return(false);
            }
            if (xDist < -1 || xDist > 1)
            {
                return(false);
            }
            if (xDist == 0 && !Chess.IsEmpty(to))
            {
                return(false);
            }
            if (xDist * xDist == 1 && yDist * yDist != 1)
            {
                return(false);
            }
            if (xDist != 0 && (Chess.IsEmpty(to) || Chess.IsSame(p, to)))
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
        private static void move(Position from, Position to, bool realMove)
        {
            lastMove = new ChessMove(from, to);
            Piece p1 = board.at(from);
            Piece p2 = board.at(to);

            pieceRemoved = p2;

            int xDiff = to.x - from.x;

            if (p1.getType() == "king" && Math.Abs(xDiff) > 1 && realMove)
            {
                int      xDir = xDiff < 0 ? -2 : 1;
                Position rook = new Position(to.x + xDir, to.y);
                xDir = xDiff < 0 ? 1 : -1;
                board.at(rook).debug();
                move(rook, new Position(to.x + xDir, to.y));
            }

            board.set(from, new Piece("blank", "none", from));

            p1.numberOfMoves++;
            if (p1.getType() == "pawn" && Math.Abs(to.y - from.y) == 2)
            {
                p1.numberOfMoves++;
            }

            p1.setPosition(to);

            if (p1.getType() == "pawn" && (to.y == 0 || to.y == 7))
            {
                p1 = new Piece("queen", p1.getColor(), p1.getPosition(), p1.getNumberOfMoves());
            }

            board.set(to, p1);
        }
Ejemplo n.º 3
0
        private static bool pawnValid(Piece p, Position from, Position to)
        {
            int xDist = from.x - to.x;
            int yDist = from.y - to.y;

            int direction = p.getColor() == "white" ? -1 : 1;

            if (from.y + direction > 7 || from.y + direction < 0) return false;

            if (from.y + direction != to.y && ((from.y + 2 * direction != to.y && p.getNumberOfMoves() == 0) || !Chess.IsEmpty(new Position(from.x, from.y + direction)) || p.getNumberOfMoves() > 0))
            {
                return false;
            }
            if (xDist < -1 || xDist > 1)
            {
                return false;
            }
            if (xDist == 0 && !Chess.IsEmpty(to))
            {
                return false;
            }
            if (xDist * xDist == 1 && yDist * yDist != 1)
            {
                return false;
            }
            if (xDist != 0 && (Chess.IsEmpty(to) || Chess.IsSame(p, to)))
            {
                return false;
            }

            return true;
        }