For a uni assignment I'm required to write a basic snake game in C. We were provided with a random number generator (that I'm not allowed to change/edit, we have to use it) to randomly spawn the snake 'food' ((@) the food <<<) around the map each time the game is launched. I'm having an issue where the 'food' is sometimes not spawning, I believe its because it is spawning in the space of the snake "#---->" (the snake <<<), and then since I spawn the snake after the food, It is entirely replacing the food. I'm fairly certain it's not spawning outside the map or in the map border and just being replaced by the border, since I've ensured it cannot spawn in the border or outside. Just so you know, the snake must spawn in the top left corner of the map (as requested on the task description). So my question is; how can I spawn the food on the map and doing checks before spawning it to ensure it doesn't spawn where the snake is at all, whilst still using the random number generator I was provided with.
Some other things aswell I forgot to mention:
- The map size and snake length are not static as you launch the game with 3 command line arguments. ./snake mapRows mapColumns snakeLength
Random Number generator:
#include<stdlib.h>
#include<time.h>
#include"random.h"
void initRandom()
{
srand(time(NULL));
}
int random(int low, int high)
{
int number = -1;
if(low <= high)
{
number = (rand() % (high-low 1)) low;
}
return number;
}
My current code to try counter-act this (doesn't stop '@' from spawning in the snake):
void makeMap(char **playerboard, int rowMap, int colMap, int snakeLength)
{
int i, j;
int x, y;
initRandom();
for(i = 0; i < rowMap; i )
{
for(j = 0; j < colMap; j )
{
playerboard[i][j] = ' ';
playerboard[i][0] = '*';
playerboard[i][colMap-1] = '*';
playerboard[0][j] = '*';
playerboard[rowMap-1][j] = '*';
}
}
if(playerboard[1][snakeLength] == '#' || playerboard[1][snakeLength] == '-' || playerboard[1][snakeLength] == '>')
{
x = random(3, rowMap - 2);
y = random(snakeLength, colMap - 2);
playerboard[x][y] = '@';
}
else
{
x = random(1, rowMap - 2);
y = random(1, colMap - 2);
playerboard[x][y] = '@';
}
loadSnake(playerboard, snakeLength);
}
What I had before I tried to counter-act the issue:
void makeMap(char **playerboard, int rowMap, int colMap, int snakeLength)
{
int i, j;
int x, y;
initRandom();
x = random(1, rowMap - 2);
y = random(1, colMap - 2);
for(i = 0; i < rowMap; i )
{
for(j = 0; j < colMap; j )
{
playerboard[i][j] = ' ';
playerboard[i][0] = '*';
playerboard[i][colMap-1] = '*';
playerboard[0][j] = '*';
playerboard[rowMap-1][j] = '*';
}
}
playerboard[x][y] = '@';
loadSnake(playerboard, snakeLength);
}
Cheers for any help.
CodePudding user response:
You can place your food only on blank tiles, so check that:
x = random(1, rowMap - 2);
y = random(1, colMap - 2);
if (playerboard[x][y] == ' ') {
playerboard[x][y] = '@';
}
Of course, that means that you will not place any food when you produce an already occupied position, so you must try again until you find a free tile:
do {
x = random(1, rowMap - 2);
y = random(1, colMap - 2);
} while (playerboard[x][y] != ' ');
playerboard[x][y] = '@';