Could someone give me feedback on my algorithm?

Started by
23 comments, last by alvaro 11 years, 4 months ago
Hello. I am writing an algorithm that paints an 8 x 8 checkerboard red, and white. However the algorithm at this moment is 87 lines long. Sadly It would be only 64 lines to individually color all 64 squares. Please give me feedback.

Here is some more relevant information. At first I tried to paint every other square red. However it looked like this. (rough illustration)
previous.png

Now after implementing my algorithm it looks like this.
imp.png


Variable k represents the current square. j is the counter. So I use it to run a % on in order to determine even or odd.


if(rects.size()<=8)
{
if(j%2 == 0 )
{
(*rects[(k)]).set_fill_color(Color::red);
}
else if(j%2 == 1)
{
(*rects[(k)]).set_fill_color(Color::white);
}
}
else if(rects.size()<=16)
{
if(j%2 == 1 )
{
(*rects[(k)]).set_fill_color(Color::red);
}
else if(j%2 == 0)
{
(*rects[(k)]).set_fill_color(Color::white);
}
}
else if(rects.size()<=24)
{
if(j%2 == 0 )
{
(*rects[(k)]).set_fill_color(Color::red);
}
else if(j%2 == 1)
{
(*rects[(k)]).set_fill_color(Color::white);
}
}
else if(rects.size()<=32)
{
if(j%2 == 1 )
{
(*rects[(k)]).set_fill_color(Color::red);
}
else if(j%2 == 0)
{
(*rects[(k)]).set_fill_color(Color::white);
}
}
else if(rects.size()<=40)
{
if(j%2 == 0 )
{
(*rects[(k)]).set_fill_color(Color::red);
}
else if(j%2 == 1)
{
(*rects[(k)]).set_fill_color(Color::white);
}
}
else if(rects.size()<=48)
{
if(j%2 == 1 )
{
(*rects[(k)]).set_fill_color(Color::red);
}
else if(j%2 == 0)
{
(*rects[(k)]).set_fill_color(Color::white);
}
}
else if(rects.size()<=56)
{
if(j%2 == 0 )
{
(*rects[(k)]).set_fill_color(Color::red);
}
else if(j%2 == 1)
{
(*rects[(k)]).set_fill_color(Color::white);
}
}
else if(rects.size()<=64)
{
if(j%2 == 1 )
{
(*rects[(k)]).set_fill_color(Color::red);
}
else if(j%2 == 0)
{
(*rects[(k)]).set_fill_color(Color::white);
}
}
Advertisement
I don't understand much of what you're doing or working with, but it looks like, without changing the logic of how you wanna paint it, you could use a for loops instead of repeating 8,16,24...64 you could make it 8 * i where i = 1,2,3...8

I don't understand much of what you're doing or working with, but it looks like, without changing the logic of how you wanna paint it, you could use a for loops instead of repeating 8,16,24...64 you could make it 8 * i where i = 1,2,3...8


Here is an explanation of the goal.

I have this information for my algorithm to work with.

* current square (or k)
* j is a counter. So it changes from even to odd every turn, letting me know if current square is even or odd. (I could use k to do this and make simpler though I think)
* I have the rects.size() (This is the size of the vector)

I want to use this information to paint a checkerboard pattern. In order to do this the first 8 must have odds painted white, and evens painted red. The second 8 must have odds painted red and evens painted white. This will alternate line by line until I get to the bottom.

I couldn't think of how to do this without manually switching every time k increases by 8.
I think you could immediately shorten that to:


// This if statement might seem kind of magical
if (((rects.size() / 8) % 2) == 0) // if <= 16, 32, 48, 64
{
if(j%2 == 1 )
{
(*rects[(k)]).set_fill_color(Color::red);
}
else if(j%2 == 0)
{
(*rects[(k)]).set_fill_color(Color::white);
}
}
else // <= 8, 24, 40, 56
{
if(j%2 == 0 )
{
(*rects[(k)]).set_fill_color(Color::red);
}
else if(j%2 == 1)
{
(*rects[(k)]).set_fill_color(Color::white);
}
}


However, I wouldn't actually recommend the above, and if I were you, I'd do something more like:

for (int i = 0; i < rects.size(); ++i)
{
if (((i + i / 8) % 2) == 0)
{
rects->set_fill_color(Color::red);
}
else
{
rects->set_fill_color(Color::white);
}
}


Edited: sorry, I messed up the above the first time. I think it's good now...

Edit again: Another alternative is the below double loop:


bool white = false;
for (int i = 0; i < rects.size(); i += 8)
{
for (int k = 0; k < 8; ++k)
{
if (white)
{
rects[i + k]->set_fill_color(Color::white);
}
else
{
rects[i + k]->set_fill_color(Color::red);
}

white = !white;
}

white = !white;
}
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
First step is to obtain the 2-dimensional coordinate of a square. For example, if k is what you call the current square is a 1-dimensional sequential index (for example you number the squares sequentially along a row, and column by column), then you can obtain the (x,y) coordinate as x=k%8 and y=k/8.

Once you have a 2-dimensional coordinate into the checker board, you can color it according to:

if(x%2 == y%2)
red square
else
white square

I think you could immediately shorten that to:

// This if statement might seem kind of magical
if (((rects.size() / 8) % 2) == 0) // if <= 16, 32, 48, 64
{
if(j%2 == 1 )
{
(*rects[(k)]).set_fill_color(Color::red);
}
else if(j%2 == 0)
{
(*rects[(k)]).set_fill_color(Color::white);
}
}
else // <= 8, 24, 40, 56
{
if(j%2 == 0 )
{
(*rects[(k)]).set_fill_color(Color::red);
}
else if(j%2 == 1)
{
(*rects[(k)]).set_fill_color(Color::white);
}
}


Yeah I kind of want to avoid another solution like this.

However, I wouldn't actually recommend the above, and if I were you, I'd do something more like:

for (int i = 0; i < rects.size(); ++i)
{
if ((i % 2) == 0)
{
rects->set_fill_color(Color::red);
}
else
{
rects->set_fill_color(Color::white);
}
}


I tried that at first but since the first square on the left is always even it made a red and white stripe pattern. So that is why I was forced to switch every time k increased by 8.

First step is to obtain the 2-dimensional coordinate of a square. For example, if k is what you call the current square is a 1-dimensional sequential index (for example you number the squares sequentially along a row, and column by column), then you can obtain the (x,y) coordinate as x=k%8 and y=k/8.

Once you have a 2-dimensional coordinate into the checker board, you can color it according to:

if(x%2 == y%2)
red square
else
white square



This seems like it will work thank you. I will try to implement a 2d system.

*snip*
I tried that at first but since the first square on the left is always even it made a red and white stripe pattern. So that is why I was forced to switch every time k increased by 8.

Sorry, yeah, I forgot to watch out for that. I've edited that post just barely to fix that and add another alternative way of doing it.

Edit: Holy ninja'ing fest... We're all replying at once ha
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Can be reduced further:

int sqIdx = 0;
for ( row = 0; row < rowCount; ++row ) { // Each row in checkerboard
for ( col = 0; col < colCount; ++col ) { // Each cell in column
Color clr;
if ( (row+col)%2 == 1 )
clr = Color::red;
else
clr = Color::white;
rects[sqIdx++]->set_fill_color(clr); // Or "row*colCount+col" in place of "sqIdx++"
}
}


And reduced further:

Color lookup[2] = { Color::red, Color::white };
for ( row = 0; row < rowCount; ++row )
for ( col = 0; col < colCount; ++col )
rects[row*colCount+col] = lookup[(row+col)&1];


Also, ninja'd to the floor. :(




Hey Cornstalks I wrote a test and it works but It is switching 1 square too soon.

test_1.jpg

Here is the test I wrote.

#include <iostream>
int main()
{
using namespace std;
int ooo_test = (1+1/8) % 2;
cout<<"square 1 is: "<<ooo_test<<"\n";
ooo_test = (2+2/8) % 2;
cout<<"square 2 is: "<<ooo_test<<"\n";
ooo_test = (3+3/8) % 2;
cout<<"square 3 is: "<<ooo_test<<"\n";
ooo_test = (4+4/8) % 2;
cout<<"square 4 is: "<<ooo_test<<"\n";
ooo_test = (5+6/8) % 2;
cout<<"square 5 is: "<<ooo_test<<"\n";
ooo_test = (6+6/8) % 2;
cout<<"square 6 is: "<<ooo_test<<"\n";
ooo_test = (7+7/8) % 2;
cout<<"square 7 is: "<<ooo_test<<"\n";
ooo_test = (8+8/8) % 2;
cout<<"square 8 is: "<<ooo_test<<"\n";
cin>>ooo_test;
}

This topic is closed to new replies.

Advertisement