Displaying 5 images in random order in C++
I am creating a game c开发者_StackOverflow社区alled 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();
}
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.
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.
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.
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.
精彩评论