Reputation: 27
As a class we were creating a checkers board in eclipse. I came up with this code but instead of displaying a checkers board it has only three white boxes and the rest is black. here is my code:
import acm.program.*;
import acm.graphics.*;
import java.awt.*;
public class checkgame extends GraphicsProgram
{
private static final int ROWS = 8;
private static final int COLUMS = 8;
public void run()
{
int sqSize = getHeight() / ROWS;
for (int i = 0; i<ROWS; i++)
{
for (int j = 0; j<COLUMS; j++)
{
int x = j*sqSize;
int y = i*sqSize;
GRect sq = new GRect (x,y,sqSize,sqSize);
sq.setFilled(((i+j)/2)!=0);
add(sq);
}
}
}
}
Any ideas where I went wrong? Thanks!
Upvotes: 3
Views: 346
Reputation: 1249
Instead of
((i+j)/2)!=0
you want to use
((i+j)%2)!=0
Your initial expression is exactly three times true (0,0 / 0,1 / 1,0), that is why you see three white boxes. The corrected version depends on whether (i+j)/2 is odd or even - the modulo operator is the common choice in such cases.
Upvotes: 1