在森林中麋了鹿 2021-06-19 11:14 采纳率: 100%
浏览 39
已采纳

大佬们我这个推箱子代码哪里错了?

//---------------------头文件--------------------
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h> 
#include<conio.h>
#include<windows.h>
//------------------符号常量----------------------
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77
#define KEY_RETURN 2
#define KEY_NEXT 4
#define SPACE_OUT 0
#define WALL 1
#define SPACE 2
#define TARGET 3
#define BOX 4
#define TARGET_IN 5
#define PERSON 6
//--------------函数声明---------------------
void designUI();
int welcomePage(); 
int printMap(int map1[][16], int imp);
void selectColor(int color);
void getLevel1();
void getLevel2();
void getLevel3();
void getLevel4();
int playGame(int map1[][16], int map2[][16], int sum, int imp);
void moveBox(int map1[][16], int x, int y, int select, int map2[][16]);
//---------------主函数-----------------------
int main() {
	while (1) {
 	  designUI();
    }
}
void designUI() {
	int ch;
	char num;
	ch = welcomePage();
	if (ch == 49) {
		getLevel1();
	}
	if (ch == 50) {
		printf("\n\t\tPlease input level!(From 1 to 4):");
		getchar();
		num = getchar();
		switch(num) {
			case '1' : 
			  getLevel1();
			  break;
			case '2' : 
			  getLevel2();
			  break;
			case '3' :
			  getLevel3();
			  break;
			case '4' :
			  getLevel4();
			  break;
			default :
			  printf("Enter error!\n");
			  Sleep(1000);
			  exit(0);
			  break;  
		}
	}
	else if (ch == 51) {
		system("cls");
		exit(0);
	}
}

int welcomePage() {
	int i = 0;
	system("cls");
	system("color 0E");
	printf("\n\n\t\t Welcome to play box!\n\n");
	printf("\t\t Person:♀Wall:■BOX:●Target:○Reach target:☆\n"); 
	printf("\t\t Up:↑\n\t\t Down:↓\n\t\t Left:←\n\t\t Right:→\n\n");
	printf("\t\t Press number 1 to start new game\n\t\t\n");
	printf("\t\t Press number 2 to choose level\n\t\t\n");
	printf("\t\t Press number 3 to quit from game\n\t\t\n");
	printf("\t\t Press right number to continue:"); 
	while (1) {
		i = getchar();
		if (i >= 49 && i <= 51) 
		   return i;
	}
}
int printMap(int map1[][16], int imp) {
	int i, j;
	int count = 0;
	for (i = 0; i < 14; i ++ ) {
		for (j = 0; j < 16; j ++ ) {
			switch(map1[i][j]) {
			  case 0 : 
			    printf("  ");
				break;
			  case 1 :
			    selectColor(14);
				printf("■");
				break;
			  case 2 : 
			    printf("  ");
				break;
			  case 3 : 
			    selectColor(11);
				printf("○");
				break;
			  case 4 : 
			    selectColor(11);
				printf("●");
				break;
			  case 5 :
			    count ++ ;
				selectColor(9);
				printf("☆");
				break;
			  case 6 :
			    selectColor(10);
				printf("♀");
				break;		   
		    }
	    }
	    printf("\n"); 
    } 
    selectColor(14);
    printf("\n");
    printf("\tYou are in Level %d!\t\t\n", imp);
    printf("\tPress arrow keys to play the game!\t\n");
    printf("\tPress N to the next level!\t\n");
    printf("\tPress Q to return the home page!\t\n");
    return count;
}
void selectColor(int color) {
	HANDLE console = GetStdHandle((STD_OUTPUT_HANDLE));
	SetConsoleTextAttribute(console, color);
}
void getLevel1() {
	int map2[14][16];
	int i, j, sum = 0;
	int map1[14][16]={
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0},
		{0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 1, 0, 0, 0, 0},
		{0, 0, 0, 1, 1, 3, 4, 2, 1, 1, 2, 1, 1, 0, 0, 0},
		{0, 0, 0, 1, 2, 3, 4, 6, 4, 2, 2, 3, 1, 0, 0, 0},
		{0, 0, 0, 1, 2, 3, 2, 4, 2, 4, 3, 1, 1, 0, 0, 0},
		{0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 1, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	};
	for (int i = 0; i < 14; i ++ ) {
		for (int j = 0; j < 16; j ++ ) {
			map2[i][j] = map1[i][j];
			if (map1[i][j] == TARGET || map1[i][j] == TARGET_IN) {
				sum ++ ;
			}
		}
	}
    if (playGame(map1, map2, sum, 1) != KEY_RETURN) 
       getLevel2(); 
}
void getLevel2() {
	int map2[14][16];
	int i, j, sum = 0;
	int map1[14][16] = {
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0},
		{0, 0, 0, 1, 2, 2, 1, 1, 2, 2, 2, 1, 0, 0, 0, 0}, 
		{0, 0, 0, 1, 2, 2, 2, 4, 2, 2, 2, 1, 0, 0, 0, 0},
		{0, 0, 0, 1, 2, 2, 1, 1, 1, 2, 4, 1, 0, 0, 0, 0},
		{0, 0, 0, 1, 2, 1, 3, 3, 3, 1, 2, 1, 0, 0, 0, 0},
		{0, 0, 1, 1, 2, 1, 3, 3, 1, 1, 2, 1, 1, 0, 0, 0},
		{0, 0, 1, 2, 4, 2, 2, 4, 2, 2, 4, 2, 1, 0, 0, 0},
		{0, 0, 1, 2, 2, 2, 2, 2, 2, 2, 6, 2, 1, 0, 0, 0},
		{0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	};
	for (int i = 0; i < 14; i ++ ) {
	    for (int j = 0; j < 16; j ++ ) {
	    	map2[i][j] = map1[i][j];
	 	    if (map1[i][j] == TARGET || map1[i][j] == TARGET_IN) {
				sum ++ ;
			}
		}
	}
    if (playGame(map1, map2, sum, 2) != KEY_RETURN) 
       getLevel3(); 
}
void getLevel3() {
	int map2[14][16];
	int i, j, sum = 0;
	int map1[14][16] = {
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0},
		{0, 0, 1, 1, 2, 2, 1, 2, 2, 1, 2, 2, 2, 1, 0, 0},
		{0, 0, 1, 2, 4, 2, 1, 1, 1, 1, 4, 2, 2, 1, 0, 0},
		{0, 0, 1, 2, 2, 4, 3, 3, 3, 3, 2, 4, 2, 1, 0, 0},
		{0, 0, 1, 1, 2, 2, 2, 2, 1, 2, 6, 2, 1, 1, 0, 0},
		{0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
	};
	for (int i = 0; i < 14; i ++ ) {
	    for (int j = 0; j < 16; j ++ ) {
	    	map2[i][j] = map1[i][j];
	 	    if (map1[i][j] == TARGET || map1[i][j] == TARGET_IN) {
				sum ++ ;
			}
		}
	}
	if (playGame(map1, map2, sum, 3) != KEY_RETURN) 
       getLevel4(); 
}
void getLevel4() {
	int map2[14][16];
	int i, j, sum = 0;
    int map1[14][16]={
      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
      {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0},
      {0, 0, 0, 0, 0, 1, 2, 2, 2, 1, 3, 2, 1, 0, 0, 0},
      {0, 0, 0, 0, 1, 1, 2, 2, 4, 3, 3, 3, 1, 0, 0, 0},
      {0, 0, 0, 0, 1, 2, 2, 4, 2, 1, 5, 3, 1, 0, 0, 0},
      {0, 0, 0, 1, 1, 2, 1, 1, 4, 1, 2, 1, 1, 0, 0, 0},
      {0, 0, 0, 1, 2, 2, 2, 4, 2, 2, 4, 2, 1, 0, 0, 0},
      {0, 0, 0, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 0, 0, 0},
      {0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 6, 2, 1, 0, 0, 0},
      {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    };
    for (int i = 0; i < 14; i ++ ) {
	    for (int j = 0; j < 16; j ++ ) {
	    	map2[i][j] = map1[i][j];
	 	    if (map1[i][j] == TARGET || map1[i][j] == TARGET_IN) {
				sum ++ ;
			}
		}
	}
	playGame(map1, map2, sum, 4);
} 
int playGame(int map1[][16], int map2[][16], int sum, int imp) {
	int i, j;
	int x, y;
	char op;
	int num = 0;
	while (1) {
		for (int i = 0; i < 14; i ++ ) {
			for (int j = 0; j < 16; j ++ ) {
				if (map1[i][j] == PERSON) {
					break;
				} 
			}
			if (j < 16) {
				break;
			}
		}
		x = i;
		y = j;
		system("cls");
		num = printMap(map1, imp);
		if (num == sum) {
			printf("Congratulations!\n");
			Sleep(3000);
			return 0;
		}
		else {
		    op = getch();
		    if ((op == 'n') || (op == 'N')) {
				return KEY_NEXT;
	     	}
			else if ((op == 'q') || (op == 'Q')) {
				return KEY_RETURN; 
			}	
		}
		switch(op) {
			case KEY_UP : 
				moveBox(map1, x, y, 1, map2);
				break;
			case KEY_LEFT : 
				moveBox(map1, x, y, 2, map2);
				break;
			case KEY_RIGHT : 
			    moveBox(map1, x, y, 3, map2);
				break;
			case KEY_DOWN : 
				moveBox(map1, x, y, 4, map2); 
				break;
			default : 
				break;     
		}
    }
}
void moveBox(int map1[][16], int x, int y, int select, int map2[][16]) {
	int x1, y1;
	int x2, y2;
	switch(select) {
		case 1 : 
		   x1 = x - 1;
		   y1 = y;
		   x2 = x - 2;
		   y2 = y;
		   break;
		case 2 :
		   x1 = x;
		   y1 = y - 1;
		   x2 = x;
		   y2 = y - 2;
		   break;
		case 3 :
		   x1 = x;
		   y1 = y + 1;
		   x2 = x;
		   y2 = y + 2;
		   break;
		case 4 :
		   x1 = x + 1;
		   y1 = y;
		   x2 = x + 2;
		   y2 = y;
		   break;
		default : 
		   break; 
	}
	switch(map1[x1][y1]) {
		case WALL : 
		     break;
	    case SPACE : 
	    case TARGET : 
		     map1[x1][x2] = PERSON;
			 if (map2[x][y] == TARGET || map2[x][y] == TARGET_IN) {
			 	map1[x][y] = TARGET;
			 } 
			 else {
			 	map1[x][y] = SPACE;
			 }
			 break;
		case BOX : 
		case TARGET_IN : 
		    if (map1[x2][y2] == TARGET)	{
		    	map1[x2][y2] = TARGET_IN;
		    	map1[x1][y1] = PERSON;
			} 
			else if (map1[x2][y2] == SPACE) {
				map1[x2][y2] = BOX;
				map1[x1][y1] = PERSON;
			}
			else {
				 break;
			}
	        if (map2[x][y] == TARGET || map2[x][y] == TARGET_IN) {
	        	map1[x][y] = TARGET;
			}
			else {
				map1[x][y] = SPACE;
			}
			break;
	}
}

	

选择关卡后,按下方向键,小人不会移动,而且程序自动结束。

  • 写回答

1条回答 默认 最新

  • 抬头见山 2021-06-19 11:43
    关注

     你的这个函数有问题,int i,j;初始化不对; 

    287行的op = getchar();

    int playGame(int map1[][16], int map2[][16], int sum, int imp) {
    	int i=0, j=0;
    	int x, y;
    	char op;
    	int num = 0;
    	while (1) {
    		for (; i < 14; i++) {
    			for (; j < 16; j++) {
    				if (map1[i][j] == PERSON) {
    					break;
    				}
    			}
    			if (j < 16) {
    				break;
    			}
    		}
    		x = i;
    		y = j;
    		system("cls");
    		num = printMap(map1, imp);
    		if (num == sum) {
    			printf("Congratulations!\n");
    			Sleep(3000);
    			return 0;
    		}
    		else {
    			op = getchar();
    			if ((op == 'n') || (op == 'N')) {
    				return KEY_NEXT;
    			}
    			else if ((op == 'q') || (op == 'Q')) {
    				return KEY_RETURN;
    			}
    		}
    		switch (op) {
    		case KEY_UP:
    			moveBox(map1, x, y, 1, map2);
    			break;
    		case KEY_LEFT:
    			moveBox(map1, x, y, 2, map2);
    			break;
    		case KEY_RIGHT:
    			moveBox(map1, x, y, 3, map2);
    			break;
    		case KEY_DOWN:
    			moveBox(map1, x, y, 4, map2);
    			break;
    		default:
    			break;
    		}
    	}
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python