2
\$\begingroup\$

I have done this, but it's pretty ugly and it feels like I may be missing something to shorten it:

    vector<int> xcomb, ycomb;
    int limitW = Pdriver->getViewPort().getWidth() / 32;
    int limitH = Pdriver->getViewPort().getHeight() / 32;
    int width = Pdriver->getViewPort().getWidth();
    int height = Pdriver->getViewPort().getHeight();
    while (true){
    start:
        ax = -rand() % width+32;
        ax = (ceil((double)ax / 32) * 32);

        ay = -rand() % height + 32;
        ay = (ceil((double)ay / 32) * 32);

        int temp = 0;

        if (xcomb.size() == 0)
            xcomb.push_back(ax);
        for (int i = 0; i < xcomb.size(); i++)
        {
            if (ax != xcomb[i])
                temp++;
        }
        if (temp == xcomb.size())
            xcomb.push_back(ax);
        temp = 0;

        if (ycomb.size() == 0)
            ycomb.push_back(ay);

        for (int i = 0; i < ycomb.size(); i++)
        {
            if (ay != ycomb[i])
                temp++;
        }
        if (temp == ycomb.size())
            ycomb.push_back(ay);

        if (ycomb.size() == limitH && xcomb.size() == limitW)
        {
            char na[20];
            sprintf_s(na, "GAME COMPLETE!");
            OutputDebugString(na);
        }


        for (u16 i = 0; i < TailCollision.size(); i++)
            if (TailCollision[i] == core::rect<s32>(32 - ax, 32 - ay, 64 - ax, 64 - ay)){

            OutputDebugString("\nAPPLE TAIL CRASH PREVENTED!\n");
            goto start;
            }

        if (core::rect<s32>(32 - ax, 32 - ay, 64 - ax, 64 - ay) != Block(x, y)){
            break;
        }
    }
    xcomb.clear();
    ycomb.clear();
}

If you excuse the mess, you can see that I want a number that is divisible by 32. I use this to make a Max combination number (limitW and limitH).

I then use two vectors and add all random values that hasn't been tried before during this process (while loop).

And to do that I think I may be doing it in a bit of a workaround. I use a temporary int which I increment for each comparison, and if it has compared the same amount as the size of the vector, it means that number already exists and it has to redo the calculation.

It may be that this is the only option, but it just seems that I do too many steps.

\$\endgroup\$
  • \$\begingroup\$ Is there a reason for the label "start"? \$\endgroup\$ – Ryan Feb 14 '15 at 0:32
  • \$\begingroup\$ Yes later i have some detection if it collides with something and simply reset everything by going to start. The calculation itself isn't effected by it. - Updated to show the whole while loop. \$\endgroup\$ – Zerowalker Feb 14 '15 at 0:36
  • \$\begingroup\$ Explain what if (core::rect<s32>(32 - ax, 32 - ay, 64 - ax, 64 - ay) != Block(x, y)) does? \$\endgroup\$ – 200_success Feb 14 '15 at 2:47
  • 1
    \$\begingroup\$ If you want an exhaustive enumeration, why use a random algorithm instead of a deterministic one? \$\endgroup\$ – 200_success Feb 14 '15 at 2:48
  • \$\begingroup\$ It simply makes sure that Object A isn't the same as Object B preventing it to spawn on the same place as another thing (Snake and Apple). Well i need a random algorithm as it's the Apple spawn in Snake i am making right? (Though the random isn't much random, it's always spawn in the same places as long as the resolution is the same:S \$\endgroup\$ – Zerowalker Feb 14 '15 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.