So the body has been initialised to the initial position and isn't being removed from the board when the game begins. Leaving to this acting as a an obstacle.
To run the code:
gcc -o Snakes Snakes.c -lncurses
Then
./Snakes
What I have tried:
#include <curses.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define LEN 22 //this will be the size of the game
#define DOWN 2 //defines the movement value sequentially
#define UP 3
#define LEFT 4
#define RIGHT 5
void boardGeneration(int x, int y, char map[][LEN])
{
int Rows;
int Cols;
for(Rows = 0; Rows < LEN ; Rows++)
{
for(Cols = 0; Cols < LEN ; Cols++)
{
mvaddch(Rows+1,Cols+1,map[Rows][Cols]);
}
}
mvaddch(y+1, x+1, '#');
}
int foodLocation(void){
srand(time(NULL));
return (rand()%10);
}
int main(){
int start = 1;
printf("Do you want to start playing Snakes?\nType 1 for Yes or 2 for No: ");
scanf("%d", &start);
while (start == 1){
int score=0;
char map[][LEN]={" -------------------- ",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
" -------------------- ",
};
map[foodLocation()][foodLocation()]='$';
keypad(initscr(),TRUE);
curs_set(0);
mvprintw(2,23," You are now playing SNAKE'S ");
mvprintw(3,23," Move around the screen with your arrows ");
mvprintw(4,23," But, be mindful of your tail and the walls! ");
mvprintw(11,23," End game by pressing 'e' ");
int PosX=11;
int PosY=11;
int inc=1;
int snakeLength=5;
int xvalue[100];
int yvalue[100];
int i;
int movement;
char c;
while(c!='e'){
mvprintw(10,23," Current score %d", score);
boardGeneration(PosX,PosY,map);
c=getch();
map[PosY][PosX]='*';
switch(c){
case UP:
PosY--;
movement=UP;
break;
case DOWN:
PosY++;
movement=DOWN;
break;
case LEFT:
PosX--;
movement=LEFT;
break;
case RIGHT:
PosX++;
movement=RIGHT;
break;
}
if(map[PosY][PosX]=='$'){
snakeLength = snakeLength + 1;
score = score + 1;
if((PosY+PosX)<=20){
map[foodLocation()+10][foodLocation()+10]='$';
}
else{
map[foodLocation()][foodLocation()]='$';
}
}
xvalue[0]=PosX;
yvalue[0]=PosY;
for(i=snakeLength;i>0;i--){
xvalue[i]=xvalue[i-1];
}
for(i=snakeLength;i>0;i--){
yvalue[i]=yvalue[i-1];
}
if(inc==snakeLength){
map[yvalue[snakeLength]][xvalue[snakeLength]]=' ';
}
if(inc<snakeLength){
inc=inc+1;
}
if(map[PosY][PosX]=='-'|| map[PosY][PosX]=='|'){
break;
}
if(map[PosY][PosX]=='*' && snakeLength>4){
break;
}
}
endwin();
if (map[PosY][PosX]=='-'|| map[PosY][PosX]=='|'){
printf("\nYou just ran into a wall and died\nYour score was %d\n\n",score);
}
else if ('e'){
printf("\nThanks for playing\nYour score was %d\n\n", score);
}
else if (map[PosY][PosX]=='*'){
printf("\nYou just tried to eat yourself and died\nYor score was %d\n\n", score);
}
printf("Do you want to play Snakes again?\nType 1 for Yes or 2 for No: ");
scanf("%d", &start);
if (start == 2){
printf("\nYou chose not to play right now\nBYE\n");
}
}
}