
-----------------------------------
Randolf Nitler
Fri Oct 17, 2008 11:22 pm

Chessboard using a for loop
-----------------------------------
Currently I have this lil method to draw my chessboard. But its awfully slow and theres too much crap in it. I'd like to use a for loop to draw it but i don't know how.
I've currently tested the coordinates (0,0,50,50) and they work. Is there a way to make this more efficient using a for loop, and if there is how do i do it?

This is what i have.

import java.awt.Color;
import hsa.Console;

/** The "Tiles" class.
  * This method will draw a checker board
  * @param x1, the x1 coordinate
  * @param y1, the y1 coordinate
  * @param x2, the x2 coordinate
  * @param y2, the y2 coordinate
 */
public class Tiles
{
    public static void tile (int x1, int y1, int x2, int y2)
    {
        //name the console
        Console c = new Console ("Tiles");

        //draw a black border around the chessboard
        c.setColor (Color.black);
        c.drawRect (x1, y1, 400, 400);

        //this will draw a row of black squares with a
        //white space in between them
        c.fillRect (x1, y1, x2, y2);
        c.fillRect (x1 + (x2 * 2), y1, x2, y2);
        c.fillRect (x1 + (x2 * 4), y1, x2, y2);
        c.fillRect (x1 + (x2 * 6), y1, x2, y2);

        //draw second row
        c.fillRect (x1 + x2, y1 + y2, x2, y2);
        c.fillRect (x1 + (x2 * 3), y1 + y2, x2, y2);
        c.fillRect (x1 + (x2 * 5), y1 + y2, x2, y2);
        c.fillRect (x1 + (x2 * 7), y1 + y2, x2, y2);

        //draw third row
        c.fillRect (x1, y1 + (y2 * 2), x2, y2);
        c.fillRect (x1 + (x2 * 2), y1 + (y2 * 2), x2, y2);
        c.fillRect (x1 + (x2 * 4), y1 + (y2 * 2), x2, y2);
        c.fillRect (x1 + (x2 * 6), y1 + (y2 * 2), x2, y2);

        //draw fourth row
        c.fillRect (x1 + x2, y1 + (3 * y2), x2, y2);
        c.fillRect (x1 + (x2 * 3), y1 + (3 * y2), x2, y2);
        c.fillRect (x1 + (x2 * 5), y1 + (3 * y2), x2, y2);
        c.fillRect (x1 + (x2 * 7), y1 + (3 * y2), x2, y2);

        //draw fifth row
        c.fillRect (x1, y1 + (y2 * 4), x2, y2);
        c.fillRect (x1 + (x2 * 2), y1 + (y2 * 4), x2, y2);
        c.fillRect (x1 + (x2 * 4), y1 + (y2 * 4), x2, y2);
        c.fillRect (x1 + (x2 * 6), y1 + (y2 * 4), x2, y2);

        //draw sixth row
        c.fillRect (x1 + x2, y1 + (5 * y2), x2, y2);
        c.fillRect (x1 + (x2 * 3), y1 + (5 * y2), x2, y2);
        c.fillRect (x1 + (x2 * 5), y1 + (5 * y2), x2, y2);
        c.fillRect (x1 + (x2 * 7), y1 + (5 * y2), x2, y2);

        //draw seventh row
        c.fillRect (x1, y1 + (y2 * 6), x2, y2);
        c.fillRect (x1 + (x2 * 2), y1 + (y2 * 6), x2, y2);
        c.fillRect (x1 + (x2 * 4), y1 + (y2 * 6), x2, y2);
        c.fillRect (x1 + (x2 * 6), y1 + (y2 * 6), x2, y2);

        //draw eighth row
        c.fillRect (x1 + x2, y1 + (7 * y2), x2, y2);
        c.fillRect (x1 + (x2 * 3), y1 + (7 * y2), x2, y2);
        c.fillRect (x1 + (x2 * 5), y1 + (7 * y2), x2, y2);
        c.fillRect (x1 + (x2 * 7), y1 + (7 * y2), x2, y2);

    } // main method
} // Tiles class

-----------------------------------
DemonWasp
Mon Oct 20, 2008 3:47 pm

Re: Chessboard using a for loop
-----------------------------------
That shouldn't be slow. If it's slow, something bad is going on. It WILL, however, be clumsy and hard to maintain. To convert to a loop, do the following:

1. Rename your variables so they mean what they should mean:
x1 should be x
y1 should be y
x2 should be xSize
y2 should be ySize
(you can choose different names, just choose something that describes the variable)

2. Look at each block you have above. You'll notice that the even rows look like this (I have removed brackets where possible - Java follows correct order-of-operations):

c.fillRect (x + (xSize * 1), y + 7*ySize, xSize, ySize);
c.fillRect (x + (xSize * 3), y + 7*ySize, xSize, ySize);
c.fillRect (x + (xSize * 5), y + 7*ySize, xSize, ySize);
c.fillRect (x + (xSize * 7), y + 7*ySize, xSize, ySize); 


So you can replace that by a loop that starts at 1, goes to 7, and counts by twos:

// Draws Eighth Row
for ( int i = 1; i 