Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
#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 function for creating the games map
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, '#');	//movement of the snakes head around the baord
}

//Function to determine next position of food
int foodLocation(void){
		srand(time(NULL)); 	//Generates random seed

		return (rand()%10); //Return comand of the random number
}
	

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;	//Initialises score value for game
  	char map[][LEN]={" -------------------- ",  	//Defines map boundaries
                   	 "|                    |", 
                   	 "|                    |",
                   	 "|                    |",
                   	 "|                    |",
                   	 "|                    |",
                   	 "|                    |",
                   	 "|                    |",
                   	 "|                    |",
                   	 "|                    |",
                   	 "|                    |",
                   	 "|                    |",
                   	 "|                    |",
                   	 "|                    |",
                   	 "|                    |",
                   	 "|                    |",
				   	 "|                    |",
				   	 "|                    |",
				   	 "|                    |",
				   	 "|                    |",
				   	 "|                    |",
				   	 " -------------------- ",				   
  };
	map[foodLocation()][foodLocation()]='$';
  	keypad(initscr(),TRUE);	//Allows terminal to read from keypad
  	curs_set(0); 	//Removes cursor symbol

	mvprintw(2,23," You are now playing SNAKE'S ");		//Print comments on game screen
	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; 		//initiaised location to the center of board
  	int PosY=11;
  	int inc=1;
  	int snakeLength=5; 	//Make snake initial length
  	int xvalue[100];   	//stores previous position
  	int yvalue[100];								
  	int i;
  	int movement; 		//place holder for movement direction 
  
  	char c;       		//initialised end game charactor 
 
		while(c!='e'){

			mvprintw(10,23," Current score %d", score);		
			boardGeneration(PosX,PosY,map); //updating displayed array
			c=getch(); //reading a single character from keyboard
			map[PosY][PosX]='*';//updates position of curser in array
			
			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;	//increases the length of the snake
				score = score + 1;		//updates score when food is eaten
				if((PosY+PosX)<=20){
					map[foodLocation()+10][foodLocation()+10]='$';	//updates food position on map
				}
			else{
				map[foodLocation()][foodLocation()]='$';
			}
		}

		xvalue[0]=PosX; //current position is first in the array
		yvalue[0]=PosY;
		for(i=snakeLength;i>0;i--){ //loop for transferring past postions in the x position
			xvalue[i]=xvalue[i-1];
		}
		for(i=snakeLength;i>0;i--){	//loop for transferring past postions in the y position
			yvalue[i]=yvalue[i-1];
		}
		if(inc==snakeLength){
			map[yvalue[snakeLength]][xvalue[snakeLength]]=' '; //Removes old position
		}
		if(inc<snakeLength){
			inc=inc+1;
		}
		
		if(map[PosY][PosX]=='-'|| map[PosY][PosX]=='|'){ //game over if the xursor position hits the boundary
		break;
		}		
		if(map[PosY][PosX]=='*' && snakeLength>4){ // game over if cursor runs into body
		break;
		}	
	}
	endwin();
		if (map[PosY][PosX]=='-'|| map[PosY][PosX]=='|'){		// print comands to tell the user how/why the game ended
			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");
  		}
	}	
}
Posted
Updated 30-Oct-16 22:50pm
v2
Comments
Richard MacCutchan 31-Oct-16 4:12am    
And we are supposed to guess what happens, and where?
Zacharry 31-Oct-16 5:23am    
So the snake is meant to move around he board and collect the '$', but i have on issue an that is the body gets left behind
Richard MacCutchan 31-Oct-16 6:20am    
Your code could be easier to understand if you split your main method into separate functions. Create a function that handles snake movements and then run some tests to see how it behaves. You should then be able quickly to spot where it is going wrong.

1 solution

You must redraw all changed content. Or maybe you havent properly changed the position of the snake.

Tips:

1. Use the debugger to find the issue.
2. implement some debug logging (aka TRACE) in your code
3. develop more engament and patience in coding

;-)
 
Share this answer
 
Comments
Zacharry 31-Oct-16 5:24am    
Yeah see I've tried that and can't narrow down the problem. Is there any code advice?
jeron1 31-Oct-16 10:20am    
Stepping through the code using a debugger doesn't help?
Zacharry 31-Oct-16 19:21pm    
Not really? What debuggger do you use?
jeron1 1-Nov-16 10:42am    
Visual studio. Most, if not all debuggers allow you to set a breakpoint and step line-by-line through the code in question, where you can see the values of all your variables, and immediately see the effect of any given function call on the screen and on the variables. It's an exceptionally powerful tool, you can't do too much better than that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900