private static bool rookValid(Piece p, Position from, Position to) { int xDist = to.x - from.x; int yDist = to.y - from.y; if (!(xDist == 0 || yDist == 0)) { return(false); } int yDirection = yDist == 0 ? 0 : yDist < 0 ? -1 : 1; int xDirection = xDist == 0 ? 0 : xDist < 0 ? -1 : 1; int x = from.x + xDirection; int y = from.y + yDirection; Position current = new Position(x, y); while (!current.Equals(to)) { if (!Chess.IsEmpty(current)) { return(false); } current.x += xDirection; current.y += yDirection; } return(!Chess.IsSame(p, to)); }
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); }