Hey everyone!
I've been creating a LCD(20x4) game for a while. In the game I'm controlling a car which has to avoid upcoming blocks. Those blocks y coordinates are picked randomly. In one column are placed 2 blocks. Since the random() function can pick only from 0 to 4, it's an often scenario that y coordinates of 2 blocks are the same, so instead of two blocks i get one. In order to avoid that I had to use while() loop.
This is what I came up with:
void SettingCoordinates(){
for(int i=0;i<4;i++){
block_x[i]=19;
randomSeed(analogRead(0));
block_y[i]=random(0,4);
while(block_y[1]==block_y[0]){
randomSeed(analogRead(1));
block_y[1]=random(0,4);
}
while(block_y[3]==block_y[2]){
randomSeed(analogRead(1));
block_y[3]=random(0,4);
}
}
}
That worked just perfectly, but that was only 4 blocks on the entire screen (not enough), I needed 8 more. So I could make that by extending the code, but then it would look too clumsy. I thought that I could shrink that code a little bit:
void SettingCoordinates(){
for(int i=0;i<12;i++){
block_x[i]=19;
randomSeed(analogRead(0));
block_y[i]=random(0,4);
while(i==0||i%2==0&&block_y[i]==block_y[i++]){
randomSeed(analogRead(0));
block_y[i]=random(0,4);
}
}
}
When I uploaded the code the LCD showed nothing( neither blocks were displayed nor the car). There must be something wrong with the while() loop. I believe it is endless, but can't figure out why.
Any help would be appreciated.