Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


#ActualCornstalks

Posted 13 December 2012 - 05:13 PM

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[i]->set_fill_color(Color::red);
    }
    else
    {
        rects[i]->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;
}

#4Cornstalks

Posted 13 December 2012 - 05:13 PM

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[i]->set_fill_color(Color::red);
    }
    else
    {
        rects[i]->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]->set_fill_color(Color::white);
        }
        else
        {
            rects[i]->set_fill_color(Color::red);
        }

        white = !white;
    }

    white = !white;
}

#3Cornstalks

Posted 13 December 2012 - 05:08 PM

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[i]->set_fill_color(Color::red);
    }
    else
    {
        rects[i]->set_fill_color(Color::white);
    }
}

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

#2Cornstalks

Posted 13 December 2012 - 05:06 PM

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 % 2) == 0)
    {
        rects[i]->set_fill_color(Color::red);
    }
    else
    {
        rects[i]->set_fill_color(Color::white);
    }
}

#1Cornstalks

Posted 13 December 2012 - 05:05 PM

It would help if I knew what rects, j, and k were, but (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 % 2) == 0)
    {
        rects[i]->set_fill_color(Color::red);
    }
    else
    {
        rects[i]->set_fill_color(Color::white);
    }
}

PARTNERS