Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Chessboard using a for loop
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Randolf Nitler




PostPosted: Fri Oct 17, 2008 11:22 pm   Post subject: 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
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Mon Oct 20, 2008 3:47 pm   Post subject: 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):
Java:

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:
Java:

// Draws Eighth Row
for ( int i = 1; i <= 7; i+= 2 ) {  // += means "add to that variable
    c.fillRect ( x + xSize*i, y + 7*ySize, xSize, ySize);
}


3. Now look carefully at the odd rows, and compare them to the even rows. Hey, it's the same thing! We can use a very very similar loop to do the same work (this one starts with 0, goes up to 6, and counts by twos:
Java:

// Draws Third Row
for ( int i = 0; i <= 6; i+= 2 ) {  // += means "add to that variable
    c.fillRect ( x + xSize*i, y + 2*ySize, xSize, ySize);
}


4. Now we've shrunk your code a fair bit (though it's not likely to be any faster). You still have 8 for loops, however, instead of 8 blocks of 4 calls each. Look carefully at the two different kinds of for-loop (odd and even). We notice some similarities:

a) The last two arguments are ALWAYS xSize, ySize.
b) The first argument is always x + xSize * i (the base x-coordinate for the block in column i.
c) The second argument is always y + (rowNumber - 1) * ySize (the base y-coordinate for row rowNumber). Let's make a variable called "row", which is rowNumber-1, and therefore goes from 0 to 7, counting by ones.
d) If rowNumber is even, then the loop starts at 1; if it is odd, then it starts at 0. If we use 'row' instead of 'rowNumber', then the loops above will start at ( row % 2 ), since when rowNumber is even, row is odd, so row % 2 is 1, etc. (Incidentally, number % divisor means "the remainder when 'number' is divided by 'divisor', so 5%2 = 1, and 19%4 = 3, etc).

Now, armed with that information, we can code the following:

Java:

// Draws the Chessboard
// This outer loop counts over all the rows. It starts at 0 (for row 1) and ends at  (for row 8)
for ( int row = 0; i < 8; ++i )  // ++ means "increment by one"

    // This inner loop counts over all the (white or black) squares in a row. It starts at 0 if row is even, or 1 if row is odd, and goes up to no more than 7 (so it will count 1-3-5-7 or 0-2-4-6
    for ( int i = row % 2; i <= 7; i+= 2 ) {  // += means "add to that variable
        c.fillRect ( x + xSize*i, y + row*ySize, xSize, ySize);
    }

}


And that should draw the same chessboard with much less code. In the future, as a courtesy, please use [ syntax = "java" ] code goes here [ / syntax ] around your code so that it's easier to read.
Randolf Nitler




PostPosted: Tue Oct 21, 2008 9:47 pm   Post subject: Re: Chessboard using a for loop

Thank you very much, I know how to make the code all pretty to look at after you post it with Turing, but not with Java. This helps! Once again thx!
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: