This discussion thread is for helping Razzo with his new 'Chess' scenario.
// instead of int nx = Math.abs(x-getX()); int ny = Math.abs(y-getY()); // use int nx = x - getX(); int ny = y - getY(); // from the above, you can get direction and distance int dx = (int) Math.signum(nx); int dy = (int) Math.signum(ny); int dist = Math.max(Math.abs(nx), Math.abs(ny)); // now, you can start the 'for' loop with for (int i = 0; i < dist - 1; i++) // remove this first line in the 'for' loop (shown below) // execution will exit the 'for' loop automatically when this condition occurs // -- well, when i == dist (or the greater of Math.abs(nx) and Math.abs(ny)) if (i == nx) return true; // remove this line // you only need one check inside the 'for' loop if (PieceExist(getX() + dx * i, getY() + dy * i)) return false; // the 'for' loop does not check the last square (the one the moving piece lands on) // as it CAN have a piece on it
public boolean isValidMove(int x,int y)
{
int nx = x - getX();
int ny = y - getY();
// from the above, you can get direction and distance
int dx = (int) Math.signum(nx);
int dy = (int) Math.signum(ny);
int dist = Math.max(Math.abs(nx), Math.abs(ny));
for (int i = 0; i < dist - 1; i++)
if (PieceExist(getX() + dx * i, getY() + dy * i)) return false;
if ((nx == 0 || ny == 0) && (nx != 0 || ny != 0))
return true;
return false;
// the 'for' loop does not check the last square (the one the moving piece lands on)
// as it CAN have a piece on it
}for (int i = 1; i < dist; i++)
{
if (i != dist)
{
if (PieceExist(getX() + dx * i, getY() + dy * i)) return false;
}
else
{
// checks for move end square here
// (if pieceE exists, verify other team [if not other team, return 'false'])
}
}if ((nx == 0 && ny == 0) || (nx != 0 && ny != 0)) return false;
for (int i = 1; i < dist - 1; i++) if (PieceExist(getX() + dx * i, getY() + dy * i)) return false;
public boolean isValidMove(int x,int y)
{
int nx = x - getX();
int ny = y - getY();
// from the above, you can get direction and distance
int dx = (int) Math.signum(nx);
int dy = (int) Math.signum(ny);
int dist = Math.max(Math.abs(nx), Math.abs(ny));
for (int i = 1; i < dist; i++) if (PieceExist(getX() + dx * i, getY() + dy * i)) return false;
if (Math.abs(x) == Math.abs(y))
return true;
return false;
}if ((nx == 0 || ny == 0) || (Math.abs(nx) != Math.abs(ny))) return false;
public boolean isValidMove(int x, int y)
{
int nx = x - getX();
int ny = y - getY();
if ((nx == 0 && ny == 0) || (nx != 0 && ny != 0)) return false;
int dx = (int) Math.signum(nx);
int dy = (int) Math.signum(ny);
int dist = Math.max(Math.abs(nx), Math.abs(ny));
for (int i = 1; i < dist; i++) if (PieceExist(getX() + dx * i, getY() + dy * i)) return false;
return true;
}// for the Bishop if (nx == 0 || ny == 0 || Math.abs(nx) != Math.abs(ny)) return false; // for the Queen if (!((nx == 0 || ny == 0) && (nx != 0 || ny != 0)) && !(nx != 0 && ny != 0 && Math.abs(nx) == Math.abs(ny))) return false;
public boolean isValidMove(int x, int y)
{
return Math.abs((x - getX()) * (y - getY())) == 2;
}public boolean isValidMove(int x, int y)
{
int nx = x - getX();
int ny = y - getY();
return Math.abs(nx * ny) < 2 && Math.abs(nx - ny) < 2;
}public boolean isValidMove(int x, int y)
{
if (isEnemy(x, y)) return Math.abs(x - getX()) == 1 && (team == "blue" ? getY() - 1 == y : getY() + 1 == y);
if (x != getX()) return false;
if (team == "blue" ? y > getY() : y < getY()) return false;
if (y - getY() == 1 || y - getY() == -1) return true;
return !moved && Math.abs(y - getY()) == 2 && !PieceExist(getX(), getY() + (int) Math.signum(y - getY()));
}