Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have been making a game to walk through mazes and solve them for a college project.

I have been trying to make my character move and, oddly it was working perfectly for like 20 minutes with the character moving and the steps counting. After that, it just stopped working and the code was the same. Was it a problem with the compiler (DEV C++) or the code is faulty?

W, A, S, D is for moving but everytime i press one, the character does not move and just spams steps: 0 for every click.

What I have tried:

The main part (player is a ascii character; ex/ey are the coordinates of the entrance; sx/sy are the coordinates of the end) :
C
steps=0;

do
{
	do
	{
		key=getch();
		printf("steps: %d", steps);
		if(key=='w')
		{
			steps+=1;
					
			if(*(lab+pointerpoint(ex, ey-2))==track)
			{
				system("cls");
				*(lab+pointerpoint(ex, ey))=track;
				*(lab+pointerpoint(ex, ey-2))=player;
				printlab();						
			}
			else
			{
				steps-=1;
				continue;
			}
		}
		if(key=='s')
		{
			steps+=1;
			if(*(lab+pointerpoint(ex, ey+2))==track)
			{
				system("cls");
				*(lab+pointerpoint(ex, ey))=track;
				*(lab+pointerpoint(ex, ey+2))=player;
				printlab();						
			}
			else
			{
				steps-=1;
				continue;
			}
		}
				
		if(key=='a')
		{
			steps+=1;
		        if(*(lab+pointerpoint(ex-2, ey))==track)
			{
				system("cls");
				*(lab+pointerpoint(ex, ey))=track;
				*(lab+pointerpoint(ex-2, ey))=player;
				printlab();
			}
			else
			{
				steps-=1;
				continue;
			}
		}
		if(key=='d')
		{
			steps+=1;
			if(*(lab+pointerpoint(ex+1, ey))==track)
			{
				system("cls");
				*(lab+pointerpoint(ex, ey))=track;
				*(lab+pointerpoint(ex+2, ey))=player;
				printlab();	
			}
			else
			{
				steps-=1;
				continue;
			}
		}
				
	} while( *(lab+pointerpoint(ex, ey)) != *(lab+pointerpoint(sx, sy)) );
			
} while(key!='q');
		
//The function that generates the maze (N is size):

void labgen(int x, int y)
{
	if(N%2==0) 
		N++;
	sx=N-1; 
	sy=N;

	maze = (char *)malloc(sizeof(char)*N*N+sizeof(char));
	*(maze+pointerpoint(N, N)+1)='\0';

	int i=0;
	for( ; i<N*N ; i++)
	{
		*(maze+i)=wall;
	}

	*(lab+pointerpoint(ex, ey))=player;
	*(lab+pointerpoint(ex, ey+1))=track;
	*(lab+pointerpoint(sx, sy))=track;
	nextblock(ex, ey+1); 
			
}
//Functions that deal with points:

char point(int x, int y)
{
	if(x<1||y<1||x>N||y>N) return '0';
	int point = N*(y-1)+x -1;
	return *(lab+point);
}

int pointerpoint(int x, int y)
{
	int point = N*(y-1)+x -1;
	return point;
}
Posted
Updated 23-Dec-18 11:33am
v3

Have you noticed how similar the code is for each key entry? That is an indication that you should make a function out of that sequence of code and pass the values that differ for each key to it which appear to be the x and y values. My general rule of thumb is if I see a sequence of code repeated three times (twice for longer sequences) then I seriously consider making it a function. If performance is an issue there are many ways to handle it. For example, if you don't want to push so much data on the stack then wrap all the parameters into a structure and pass a pointer to the structure. That is very, very simple. It is also a much better way to deal with lots of variables that might be declared as global. I have had to maintain a few programs now where numerous data variables were declared as global and I had to make lots of changes when I distributed the processing across multiple threads. It was very annoying so now I am very careful about using global variables. I wrap them into a namespace or data structure called "global" just to make them obvious.
 
Share this answer
 
v2
You must use the debugger to find out what is your problem. Set a breakpoint and move maybe some formatted output helps.

Important tip: learn to work with structs and arrays. Your maze is a two-dimensional (not 3, was typo) array
C++
char *maze = malloc()
char value = maze[x][y];//access

this make your code more readable faster and debuggable for you. You can view memory in the debugger somehow. Read the documentation for details.

bonus-tip: Think about the capital chars like "W"
 
Share this answer
 
v2
Comments
Member 14092627 23-Dec-18 13:40pm    
Thank you for your answer, i have found that the problem lies in the
if(*(lab+pointerpoint(ex, ey-2))==track) (for the W), it seems that the code does not enter that if.
Quote:
Was it a problem with the compiler (DEV C++) or the code is faulty?

Oddities are against your code, you are the only one able to tell because we can't run your code.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

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