0

I am creating a game called Simon Says in C++ and want to be able to hit the space bar and display 5 random images that i have already loaded to my game. And every time i hit the space bar i want the images to be in a different order than the previous time therefore i need to be able to have a command to randomly display the 5 different images once i hit the space bar. Here is the code i have to display the images in a set order:

     if(key_down(VK_SPACE))
    {
        clear_screen();
        a();
        refresh_screen();
        delay(1000);
        clear_screen();
        b();
        refresh_screen();
        delay(1000);
        clear_screen();
        e();
        refresh_screen();
        delay(1000);
        clear_screen();
        d();
        refresh_screen();
        delay(1000);
        clear_screen();
        g();
        refresh_screen();
        delay(1000);
        clear_screen();
        c();
        refresh_screen();
        delay(1000);
        clear_screen();
        refresh_screen();

    }

4 Answers4

2

Here is plumbing (without the (SDL) library specifics):


Edit using random_shuffle is a lot better:

#include <iostream>
#include <algorithm>

void a() { std::cout<<"a"<<std::endl; }
void b() { std::cout<<"b"<<std::endl; }
void c() { std::cout<<"c"<<std::endl; }
void d() { std::cout<<"d"<<std::endl; }
void e() { std::cout<<"e"<<std::endl; }

int main()
{
    typedef void(*flashfunc)();
    static flashfunc flashes[] = {a,b,c,d,e};

    std::random_shuffle(flashes, flashes+5);

    for (flashfunc *flash=flashes; flash!=flashes+5; ++flash)
        (*flash)();

    return 0;   
}

I had initially forgotten about random_shuffel and came up with this way of doing a make-shift shuffle:

#include <ctime>

template <typename T>
    bool shuffled(const T&, const T&)
{
int r = rand() / ( RAND_MAX / 2 );
return 0 != r;
}

// ...  
    srand(time(NULL));
    std::stable_sort(flashes, flashes+5, shuffled<flashfunc>);

Note that using this way to sort, you need stable sort because the sort predicate is not deterministic.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • fixed rand() logic according to ["Using `rand()`" article](http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx): **[The reason this comment is important is that there are a number of algorithms that seem at first sight to implement random shuffling of a sequence, but that do not in fact produce a uniform distribution over the N! possible orderings. That is, it's easy to get random shuffle wrong.](http://www.sgi.com/tech/stl/random_shuffle.html)**. Fixing the seeding is out of scope for now :) – sehe Sep 12 '11 at 13:22
1

The standard algorithm std::random_shuffle will put an array (or vector, etc) into random order.

It appears from your code that you have a different function to draw each image, so in that case the things you shuffle should probably be function pointers. Then, loop (either directly or via std::for_each) over your shuffled array (or vector, etc) calling each in turn and doing the clear/refresh/delay.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • good point. is random_shuffle 'new' or something - I haven't seen it before – sehe Sep 12 '11 at 13:02
  • No. It's been in every C++ version since '98. You need `#include ` – MSalters Sep 12 '11 at 13:17
  • @sehe: It isn't. Many algorithms are in [``](http://cplusplus.com/reference/algorithm/random_shuffle/) – Sebastian Mach Sep 12 '11 at 13:24
  • @phresnel: LOL, I kind-a knew _'many algorithms'_ are in there :) Many are in (accumulate, anyone?) by the way. Somehow I just forgot about it/missed it. Hey, I can forget things too, yay! – sehe Sep 12 '11 at 13:26
  • @sehe: I do not even try to memorize them. I think a rough grasp is enough; know where to look it up. With other languages (and with C++0x) it is near to impossible to remember them all, anyways :) – Sebastian Mach Sep 12 '11 at 13:53
  • Well, remembering that _shuffling is covered_ sits strongly in the 'rough grasp' section for me. Still, I managed to have completely blanked that out. This is one of the big reasons for me to keep spending some time around here: sharpen the saw – sehe Sep 12 '11 at 13:56
  • @sehe: In my rough grasp section, there is just a C++ reference. ... Just kidding ;) Sharpening your knives, saws and arrows is definitely great. It is also worth soemtimes to just take out a C++ book and skimming through it :) – Sebastian Mach Sep 12 '11 at 14:01
0

You can a function like the following:

//returns random number between 0 and 4, inclusive
int getRandomInt() {
    return rand() % 5;
}

However, you must first call srand(someNumber) before using it for the first time. The system time is a good candidate for someNumber to ensure that the same number is not used every time you run the program.

WaelJ
  • 2,942
  • 4
  • 22
  • 28
0

srand (http://www.cplusplus.com/reference/clibrary/cstdlib/srand/) and rand (http://www.cplusplus.com/reference/clibrary/cstdlib/rand/) could help you to generate random numbers. I would store all images in an array and draw random indices. But don't draw any index twice. Since generating random numbers is a cheap operation in comparison to displaying images, you could repeat the procedure until you find a valid index. WaelJ*s answer will help you to generate numbers within the arrays boundaries.

alfa
  • 3,058
  • 3
  • 25
  • 36