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

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

//---------------------头文件--------------------
#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;
    		}
    	}
    }
    已采纳该答案
    评论
    解决 1 无用
    打赏 举报

相关推荐 更多相似问题