c-如何在我为学校创建的游戏中实现指针



我正在为我的计算机类创建一个游戏。我应该创建一个50x50数组,用户可以使用"w"a"s"d"在迷宫中移动。然而,我的老师告诉我,我需要回去在程序中执行指针,才能获得全额学分。我可以在程序中使用指针的一些方法是什么?你能解释一下吗?因为我不太了解指针。

#include <stdio.h>
void printMaze(char MAZE[50][50]);
int main(void)
{
int yValue = 9;
int xValue = 0;
int escapeXvalue = 49;
int escapeYvalue = 9;
int escapeYvalue2 = 39;
char movement;
//50 x 50 array MAZE using '*' to represent walls and ' ' to represent a moveable path
char MAZE[50][50] =
{
{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
{'*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
{'*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*'},
{'*',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
{'*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*',' ','*'},
{'*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*',' ',' ',' ',' ',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ','*'},
{'*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*',' ','*',' ','*','*','*','*','*','*','*',' ','*',' ','*'},
{'*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*',' ','*',' ','*','*','*','*','*','*','*',' ','*',' ','*'},
{'*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*',' ',' ',' ','*','*','*',' ','*',' ','*','*','*','*','*','*','*',' ','*',' ','*'},
{'0',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*',' ','*',' ',' '},
{'*',' ','*',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*',' ','*',' ','*'},
{'*',' ','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*',' ','*',' ','*'},
{'*',' ','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ','*'},
{'*',' ','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*'},
{'*',' ','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ','*'},
{'*',' ','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
{'*',' ','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
{'*',' ','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
{'*',' ','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
{'*',' ','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ','*'},
{'*',' ','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
{'*',' ','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
{'*',' ','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
{'*',' ','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
{'*',' ','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ','*'},
{'*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
{' ',' ',' ',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ','*'},
{'*',' ','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*',' ','*'},
{'*',' ','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*',' ','*'},
{'*',' ','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*',' ','*'},
{'*',' ','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*',' ','*'},
{'*',' ','*',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ','*'},
{'*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
{'*',' ','*',' ','*','*','*',' ',' ',' ',' ',' ','*','*',' ',' ',' ',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
{'*',' ','*',' ','*','*',' ',' ','*','*','*',' ','*','*',' ','*','*',' ','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
{'*',' ','*',' ','*','*',' ','*','*','*','*',' ','*','*',' ','*','*',' ','*','*',' ',' ',' ',' ',' ',' ','*','*',' ',' ',' ',' ',' ','*','*',' ','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*'},
{'*',' ','*',' ','*','*',' ','*',' ',' ','*',' ','*','*',' ','*','*','*','*','*','*','*','*','*','*',' ','*','*',' ','*','*','*',' ',' ','*',' ','*','*',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ','*'},
{'*',' ','*',' ','*','*',' ','*','*',' ','*',' ','*','*',' ',' ',' ',' ',' ','*','*','*','*','*','*',' ','*','*',' ','*','*','*','*',' ','*',' ','*','*',' ','*','*','*','*','*','*','*','*','*','*','*'},
{'*',' ','*',' ','*','*',' ','*','*',' ',' ',' ','*','*','*','*','*','*',' ','*','*','*','*','*','*',' ','*','*',' ','*','*','*','*',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*','*','*','*','*'},
{'*',' ','*',' ','*','*',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*',' ',' ',' ',' ','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*',' ','*','*','*',' ',' '},
{'*',' ','*',' ','*','*',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*',' ','*','*','*',' ','*'},
{'*','*','*',' ','*','*',' ','*','*','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*',' ','*','*','*',' ','*'},
{'*',' ',' ',' ','*','*',' ',' ',' ','*',' ','*','*','*','*',' ','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*',' ','*','*','*',' ','*'},
{'*',' ','*','*','*','*',' ','*',' ','*',' ','*','*','*','*',' ','*','*','*','*','*',' ','*','*','*','*','*','*','*','*','*','*','*',' ','*',' ','*','*','*','*','*','*','*','*',' ','*','*','*',' ','*'},
{'*',' ','*','*','*','*',' ','*',' ','*',' ',' ',' ','*','*',' ','*','*','*','*','*',' ','*','*','*','*','*','*','*','*',' ',' ',' ',' ','*',' ','*','*','*','*','*','*','*','*',' ','*','*','*',' ','*'},
{'*',' ','*','*','*','*',' ','*',' ','*','*','*',' ','*','*',' ','*','*','*','*','*',' ','*','*','*','*','*','*','*','*',' ','*','*','*','*',' ','*','*','*','*','*','*','*','*',' ','*','*','*',' ','*'},
{'*',' ','*','*','*','*',' ','*',' ',' ','*','*',' ','*','*',' ','*','*','*','*','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*','*','*','*',' ','*','*','*','*','*','*','*','*',' ',' ',' ',' ',' ','*'},
{'*',' ','*','*','*','*',' ','*','*',' ','*','*',' ','*','*',' ','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*','*','*','*','*','*','*','*',' ','*','*','*','*','*'},
{'*',' ',' ',' ',' ',' ',' ','*','*',' ',' ',' ',' ','*','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
{'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'}};
//infinite loop only untill logic for figuring out how MAZE logic should work
//loop iterates through user choices for movement through the MAZE
while(1){
if ((yValue == escapeYvalue && xValue == escapeXvalue) || (yValue == escapeYvalue2 && xValue == escapeXvalue))
{
system("clear");
printf("You beat the game! n");
return 0;
}
printMaze(MAZE);
printf("Enter direction you want to move A(left) S(down) D(right) W(up)n");
movement = getchar();
/*
the 'if' statements in the cases represent checks to make sure the move
doesn't conflict with out of bounds in the array or traversal through a '*'
*/
switch(movement){
case 'A' :
case 'a' :
//player is moving character to the left
if(xValue > 0 && MAZE[yValue][xValue - 1] == ' '){
printf("A action executedn");
MAZE[yValue][xValue] = ' ';
xValue --;
MAZE[yValue][xValue] = '0';
}
else{
printf("A action could not be executedn");
}
system("clear");
break;

case 'D' :
case 'd' :
//player is moving character to the right
if(xValue < 50 && MAZE[yValue][xValue + 1] == ' '){
printf("D action executedn");
MAZE[yValue][xValue] = ' ';
xValue ++;
MAZE[yValue][xValue] = '0';
}
else{
printf("D action could not be preformedn");
}
system("clear");
break;

case 'S' :
case 's' :
//player is moving character down
if(yValue < 50 && MAZE[yValue + 1][xValue] == ' '){
printf("S action executedn");
MAZE[yValue][xValue] = ' ';
yValue ++;
MAZE[yValue][xValue] = '0';
}
else{
printf("S action could not be executedn");
}
system("clear");
break;
case 'W' :
case 'w' :
//player is moving character up
if(yValue > 0 && MAZE[yValue - 1][xValue] == ' '){
printf("W action executedn");
MAZE[yValue][xValue] = ' ';
yValue --;
MAZE[yValue][xValue] = '0';
}
else{
printf("W action could not be preformedn");
}
system("clear");
break;
}
}
}
void printMaze(char MAZE[][50])
{
for (int x = 0; x < 50; x++)
{
for(int y = 0; y < 50; y++)
{
printf("%c ", MAZE[x][y]);
}
printf("n");
}
}

您可以在这里阅读有关指针的一些基本信息,例如:https://beginnersbook.com/2014/01/c-pointers/

指针基本上只是将另一个变量的地址存储在内存中的变量。你可以把它们想象成某种意义上的箭头,指向记忆中的另一个地方。想象一下,如果记忆是一张地图。假设你这样做:

House myHouse;

在地图上为房子分配空间,你的房子,你可以想象你收到了你的房子在世界上确实占据的位置。然后,如果你像一样指向你的房子

House* pointer;
pointer = &myHouse;

指针就像一张纸,上面写着你的地址(注意&是用来获取地址的(。由于您在创建指针时指定了在指向的方向上保存的数据类型(例如,您输入int*来告诉编译器"此指针指向内存中保存整数的位置"(,因此编译器能够允许您对它们执行一些操作。在数组的情况下,就像在程序中一样,可以通过添加或减去整数来沿数组移动指针。如果我们有前面定义的指向您房子的指针,并且您的房子是房子数组的第一个元素,那么操作"pointer+1"将返回数组中下一个房子的地址。

所以我假设你的老师希望你用一个指针来保存玩家的当前位置,并巧妙地使用这个整数和来移动。

PS:你的代码中有很多重复,这通常是一种糟糕的做法,可以考虑将它更多地划分为一些函数,比如一个用于检查一个地方是否为空,另一个用于指定特定方向移动。

最新更新