I want to generate a random number between 1 and 2. For that I use:
srand(time(0));
randomnumber = rand() % 2 + 1
But it always returns me the number 2. What is the best way to generate random numbers?
I modified the hello world example to get this:
#include "Pokitto.h"
Pokitto::Core mygame;
int main ()
{
mygame.begin();
srand(time(0));
while (mygame.isRunning())
{
if (mygame.update())
{
auto r = rand() % 2 + 1;
mygame.display.print(r);
}
}
}
And it worked.
So Iâd inclined to believe that either youâre always calling srand
just before you call rand
(youâre only supposed to call srand
once because it resets the sequence), or for some reason something else is affecting your results, or perhaps time
isnât working as it should on whatever youâre doing.
If you showed the rest of your code it might become clearer what the problem is.
Yeah I noticed that its a different problem. I make a game where you breed an egg at the start and after it should be random if you get a male or female from the egg. Thats why I need a random number. First I thought that random number doesnât work but after I set random number to a fix value it still didnât work. So here is my code, I hope I made a simple mistake (Im new to C++):
Graphic.h:
#include <stdint.h>
const uint16_t graphic_palette[] = {61450,65535,0,65351,64164,53605,62226,43575,52967,36361,6884,1855,951,31401,40179,16904};
const uint8_t background[] =
{
110,88,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34
};
const uint8_t pokochi_male_right[] =
{
22,18,
0,0,0,0,0,0,0,0,0,0,0,
0,0,2,34,34,34,34,34,0,0,0,
0,0,34,34,34,34,34,34,32,0,0,
0,2,34,34,34,34,34,34,34,0,0,
0,34,34,34,34,34,34,34,34,32,0,
2,34,34,34,34,34,34,17,34,34,0,
34,34,33,18,34,34,34,17,34,34,32,
34,34,33,18,34,34,34,34,34,34,32,
34,34,34,34,34,34,34,34,34,34,32,
34,34,34,34,33,17,18,34,34,34,32,
34,34,34,34,34,34,34,34,34,34,32,
34,34,34,34,34,34,34,34,34,34,32,
34,34,34,34,34,34,34,34,34,34,32,
34,34,34,34,34,34,34,34,34,34,32,
34,34,34,34,34,34,34,34,34,34,32,
34,34,34,34,34,34,34,34,34,34,32,
34,34,34,34,34,34,34,34,34,34,32,
2,34,34,34,34,34,34,34,34,34,0
};
const uint8_t pokochi_female_right[] =
{
22,18,
0,0,0,0,0,0,0,0,0,0,0,
0,0,1,17,17,17,17,17,0,0,0,
0,0,17,17,17,17,17,17,16,0,0,
0,1,17,17,17,17,17,17,17,0,0,
0,17,17,17,17,17,17,17,17,16,0,
1,17,17,17,17,17,17,34,17,17,0,
17,17,18,33,17,17,17,34,17,17,16,
17,17,18,33,17,17,17,17,17,17,16,
17,17,17,17,17,17,17,17,17,17,16,
17,17,17,17,18,34,33,17,17,17,16,
17,17,17,17,17,17,17,17,17,17,16,
17,17,17,17,17,17,17,17,17,17,16,
17,17,17,17,17,17,17,17,17,17,16,
17,17,17,17,17,17,17,17,17,17,16,
17,17,17,17,17,17,17,17,17,17,16,
17,17,17,17,17,17,17,17,17,17,16,
17,17,17,17,17,17,17,17,17,17,16,
1,17,17,17,17,17,17,17,17,17,0
};
main.cpp:
#include "Graphic.h"
enum Sex {male, female};
Sex sex;
uint8_t positionX;
uint8_t positionY;
void handlePokochi()
{
uint8_t maxFrameCount = 10;
uint8_t frameCount = maxFrameCount;
positionX = 30;
positionY = 30;
// For test purpose I set the random number to 1
uint8_t randomNumber = 1;
switch (randomNumber)
{
case 1:
sex = male;
break;
case 2:
sex = female;
break;
}
while (true)
{
if (game.update())
{
if (frameCount == maxFrameCount)
{
game.display.drawBitmap(0, 0, background);
switch(sex)
{
case male:
// Draw male
game.display.drawBitmap(positionX, positionY, pokochi_male_right);
case female:
// Draw female
game.display.drawBitmap(positionX, positionY, pokochi_female_right);
}
frameCount = 0;
}
frameCount++;
}
}
}
But the funny thing is that it always draws the female, so although the number is 1, it still thinks the sex is female⌠The code isnât complete so it wonât work. There must be something wrong with the sex part.
Youâre missing a break;
before case female:
!
Otherwise the execution will execute the female
case as well (which is brought up by my compiler as a warning usually by the way).
By the way, that might be the intended way (for example, when you need the same behavior for the same cases, and when defaulting to a known case too).
Thank you so much!!!
@carbonacat beat me to it because I was busy today.
@zer0 Iâll give you a little tip though:
You would be better off using 0 and 1 instead of 1 or 2 for your cases.
Offsetting it by 1 might be humanly easier to read if you arenât used to zero-based indexing yet, but keeping it as 0 and 1 will be easier for the computer (the Pokitto) to work with.
In fact you could do something like this:
enum Sex
{
Male = 0,
Female = 1,
};
Sex getRandomSex(void)
{
return static_cast<Sex>(rand() % 2);
}
Then instead of that first large switch statement you can do:
sex = getRandomSex();
(Iâd also like to suggest using an enum class
instead of a plain enum
, but that would take some explaining.)
Yes! Warnings are your friend, eliminate them whenever you see them.
I try to make sure my code doesnât produce any warnings.
(Unfortunately thereâs a lot of warning noise from the library at the moment, so it can be hard to spot.)
Case fallthrough is generally considered bad in almost any case.
It makes the code harder to read and can sometimes hurt performance.
Some modern languages forbid it entirely or only permit it by using explicit goto
statements.
If youâre trying to use case fallthrough, you should really stop and think âis there a better way I could do this?â.
(You donât want to end up writing a version of Duffâs Device.)
I agree! I rarely use it myself, or never at all. And thatâs a pain to maintain anyway haha
Goto gives me shivers too
Whats is the difference if I say for example Sex(0) and cast? Why should I use enum class instead of a normal enum?
Sex(0)
is equivalent to (Sex)0
, which is a C style cast.
C style casts are different from static_cast
.
Thereâs a nice comprehensive explanation of all the different cast types here.
(Sex(0)
is only a C-style cast in this case because itâs an enumeration type. If it was a class
or a struct
, that would call its constructor.)
Firstly because unscoped enumerations (plain enum
) can result in name clashes.
Slightly modifying an example taken from here:
// this code won't compile!
enum Colour { Red, Green, Blue };
enum Feeling { Excited, Moddy, Blue };
The two cases of Blue
clash with each other because plain enumerations arenât scoped.
If you change it to a scoped enumeration:
enum class Colour { Red, Green, Blue };
enum class Feeling { Excited, Moddy, Blue };
Then it works properly because the different enum values have to be accessed through the name of the enum type (i.e. they are scoped).
So Colour::Blue
doesnât clash with Feeling::Blue
.
Secondly, type safety.
Unscoped enumeration values are implicitly convertible to and from integer types, so this can happen:
// Not a valid value but it compiles anyway
Colour colour = 200;
A scoped enumeration is strongly typed, so youâd get a compiler error instead
// Error, integer not implicitly convertible to Colour
Colour colour = 200;
Although you can still override that if you genuinely mean it.
// You'd have to actually be trying to get this wrong on purpose
Colour colour = static_cast<Colour>(200);
Iâm currently gathering interesting things like these into my resource collection.
This is one of the best explanations i have read for the need in â::â, nice post Pharap.
So many examples use abstract code to explain the concept but as you follow this whole topic this makes a lot of sense, using the colour and an emotion as comparrison makes it much clearer as to why the difference is important; that part is usually confused when an author uses stuff like This::That, Fuu::Bar or A::B - they always looked strange and original programs had no real purpose or goal.
Thanks.
Technically scoped enumerations are just one case of scoping, but yeah, the same principle applies: avoiding name clashes.
To give a Pokitto-specific example, Pokitto::Buttons, Pokitto::Display and many other Pokitto classes have a begin function (e.g. Pokitto::Buttons::begin() and Pokitto::Display::begin()).
If it werenât for scoping, the names would clash and it would be impossible to tell which begin you were trying to use.
By the way, the technical name for :: is the âscope resolution operatorâ (an informally itâs sometimes called the âdouble colon operatorâ).
I know what you mean.
I think thatâs because experienced programmers tend to think more abstractly, but they often forget that less experienced programmers havenât gotten to that point yet, hence they end up with âfooâ and âbarâ everywhere instead of coming up with a tangible example.
Even a slightly convoluted example can work (like the case here - a Feeling enumeration isnât something youâd come across every day, let alone needing a Colour enumeration at the same time).