Fixed randomness-related crash in snake.

This commit is contained in:
Jonas 'Sortie' Termansen 2011-11-17 13:11:09 +01:00
parent a7de7b4905
commit 5bde040295
2 changed files with 8 additions and 6 deletions

View File

@ -56,8 +56,8 @@ void Reset()
case 3: velx = 0; vely = -1; break; case 3: velx = 0; vely = -1; break;
} }
animalx = 2 + (rand() % width-4); animalx = 2 + (rand() % (width-4));
animaly = 2 + (rand() % height-4); animaly = 2 + (rand() % (height-4));
taillen = 0; taillen = 0;
tailmax = 3; tailmax = 3;
@ -141,8 +141,10 @@ void Update()
if ( newx == animalx && newy == animaly ) if ( newx == animalx && newy == animaly )
{ {
tailmax++; tailmax++;
animalx = 2 + (rand() % width-4); animalx = 2 + (rand() % (width-4));
animaly = 2 + (rand() % height-4); animaly = 2 + (rand() % (height-4));
ASSERT(0 <= animalx && animalx < width);
ASSERT(0 <= animaly && animaly < height);
if ( maxspeed < speed ) { speed += speedincrease; } if ( maxspeed < speed ) { speed += speedincrease; }
} }

View File

@ -28,11 +28,11 @@ namespace Maxsi
{ {
namespace Random namespace Random
{ {
int random_seed=1337; unsigned random_seed = 1337;
extern "C" int rand() extern "C" int rand()
{ {
random_seed = random_seed + 37 * 1103515245 + 12345; random_seed = random_seed + 37 * 1103515245 + 12345;
return random_seed / 65536; return random_seed >> 16;
} }
} }
} }