A Few Saturn Programming Questions

slinga

Established Member
1) What do I use to seed srand()? I tried using something from time.h, but interstingly enough, whenever I used it, my controller input stopped :huh I need this for placement of the food.

2) I'm using dynamically allocated linked-lists for my snake clone. When the snake dies, I use the following code:

Code:
void killSnake(struct location* snakeHead)

{

	if(snakeHead->next!=NULL)

	{

 killSnake(snakeHead->next);

	}

	slPrint(" ", slLocate(snakeHead->x, snakeHead->y));

	//free(snakeHead); // gives me weird errors

}

I commented out the free() because it was giving me weird, fluky errors. An example would be if the snake dies by hitting the top or the bottom of the screen, and the player comes back, there would be no problem. But if ther snake hit the left or right side of the screen, the player would be initialized to some bogus position on the screen (I could tell by printing out the coordinates).
 
1. Random

I use slRandom to get random FIXED in a range, as well as integer (see code below).

When launched at init, slRandom gives the same result everytime . To get a really random number, I use something like illustrated in this pseudo-code:

Code:
while (no key pressed){

  slRandom();

  slSynch();

}

Its call is needed only one time (probably at init time). The idea is that user action is an unpredicable action (slSynch also should, but it does not seem to be the case).

code for FIXED and int in a (min-max) range :

Code:
/* 

 * RANDOM FUNCTIONS

 */

FIXED minmaxrnd(FIXED min, FIXED max){

 FIXED resu,mmax,mmin;

 mmax = (max>min)?max:min;

 mmin = (max>min)?min:max;

 resu = min + slMulFX(slRandom(),max-min);

 return resu;

}

int minmaxrndint(int min,int max){

  int a = (int)(slRandom());

  a = (a&(~0x8000));

  a = min + (a%(max-min+1));

  return (a);

}

2. I don't see how your algorithm is affected by the left/right or bottom/top collision. This said, are you sure you free the right thing (structure or pointer) ?
 
When you allocate your list elements, are you sure you init them correctly? C does not guarantee anything about the state of memory allocated with malloc. You didn't provide the code, but at a guess you're being allocated a previously used block of memory which still contains whatever was being stored there before.
 
Back
Top