在C中遇到迷宫游戏的问题 [英] Having problems with a maze game in C

查看:74
本文介绍了在C中遇到迷宫游戏的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在制作游戏,走过迷宫并为大学项目解决它们。



我一直试图让我的角色移动,奇怪的是在角色移动和步数计数的情况下,它完美地工作了20分钟。之后,它刚停止工作,代码是一样的。这是编译器的问题(DEV C ++)还是代码有问题?



W,A,S,D用于移动,但每次按下一个,角色不会移动,只是垃圾邮件步骤:每次点击都是0。



我尝试了什么:



主要部分(玩家是ascii角色; ex / ey是入口的坐标; sx / sy是结尾的坐标):

 steps =  0 ; 


{

{
key = getch();
printf( 步骤:%d,步骤);
if (key == ' w'
{
步数+ = 1 ;

if (*(lab + pointerpoint(例如,ey- 2 ) )== track)
{
system( cls);
*(lab + pointerpoint(ex,ey))= track;
*(实验室+指针点(例如,ey- 2 ))= player;
printlab();
}
else
{
steps - = 1 ;
继续;
}
}
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 ;
继续;
}
}

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 ;
继续;
}
}
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 ;
继续;
}
}

} while (*(lab + pointerpoint(ex,ey))!= *( lab + pointerpoint(sx,sy)));

} while (key!= ' q');

// 生成迷宫的函数(N是大小):

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

迷宫=( char *)malloc( sizeof char )* N * N + sizeof( char ));
*(迷宫+指针点(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(例如,ey + 1);

}
// 处理积分的函数:

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 ;
返回点;
}

解决方案

您是否注意到每个密钥条目的代码有多相似?这表明您应该从该代码序列中创建一个函数,并将每个键不同的值传递给它,它们似乎是x和y值。我的一般经验法则是,如果我看到一系列代码重复三次(两次更长的序列),那么我认真考虑将其作为一个函数。如果性能是一个问题,有很多方法来处理它。例如,如果您不想在堆栈上推送如此多的数据,则将所有参数包装到结构中并将指针传递给结构。这非常非常简单。它也是处理许多可能被声明为全局的变量的更好方法。我现在必须维护一些程序,其中许多数据变量被声明为全局,当我跨多个线程分发处理时,我不得不做出很多改变。这非常烦人,所以现在我非常小心使用全局变量。我将它们包装到一个名为global的命名空间或数据结构中,只是为了使它们显而易见。


你必须使用调试器来找出你的问题。设置一个断点并移动一些格式化的输出帮助。



重要提示:学习使用结构和数组。你的迷宫是一个二维(不是3,是错字的)数组

  char  * maze = malloc ()
char value = maze [x] [y]; // 访问



这使您的代码更具可读性更快可调试。您可以以某种方式在调试器中查看内存。阅读文档了解详细信息。



bonus-tip:想想像W这样的大写字母


引用:

编译器是否存在问题(DEV C ++)或代码有问题?



奇怪的是你的代码,你是只有一个能告诉,因为我们无法运行你的代码。



您的代码没有按照您的预期行事,或者您不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到bug ,它只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]



调试器仅显示您的代码正在执行的操作,您的任务是与应该执行的操作进行比较。


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) :

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;
}

解决方案

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.


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

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"


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.


这篇关于在C中遇到迷宫游戏的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆