Я создаю игру для своего компьютерного класса. Я должен создать массив 50x50, чтобы пользователь мог использовать 'w' 'a' 's' 'd' для перемещения по лабиринту. Тем не менее, мой учитель сказал мне, что мне нужно go вернуться и внедрить указатели в программу, чтобы получить полный кредит. Какими способами я могу использовать указатели в моей программе, и не могли бы вы объяснить, как, потому что я не очень понимаю указатели.
#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 executed\n");
MAZE[yValue][xValue] = ' ';
xValue --;
MAZE[yValue][xValue] = '0';
}
else{
printf("A action could not be executed\n");
}
system("clear");
break;
case 'D' :
case 'd' :
//player is moving character to the right
if(xValue < 50 && MAZE[yValue][xValue + 1] == ' '){
printf("D action executed\n");
MAZE[yValue][xValue] = ' ';
xValue ++;
MAZE[yValue][xValue] = '0';
}
else{
printf("D action could not be preformed\n");
}
system("clear");
break;
case 'S' :
case 's' :
//player is moving character down
if(yValue < 50 && MAZE[yValue + 1][xValue] == ' '){
printf("S action executed\n");
MAZE[yValue][xValue] = ' ';
yValue ++;
MAZE[yValue][xValue] = '0';
}
else{
printf("S action could not be executed\n");
}
system("clear");
break;
case 'W' :
case 'w' :
//player is moving character up
if(yValue > 0 && MAZE[yValue - 1][xValue] == ' '){
printf("W action executed\n");
MAZE[yValue][xValue] = ' ';
yValue --;
MAZE[yValue][xValue] = '0';
}
else{
printf("W action could not be preformed\n");
}
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");
}
}