每日三百行代码 第十五天

照书敲的code,可惜运行不了,后续再更新原因,求大佬帮忙指正。

#include<stdio.h>
#include<stdlib.h>
#include<window.h>
#include<conio.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 BOX 4
#define TARGET_IN 5
#define PERSON 6

void DesignUI();
int WelcomePage();

int PrintMap(int aiMap[][16],int iImp);
void Selectcolor(int iColor);

void MoveBox(int aiMap[][16],int iPlayerX,int iPlayerY,int iSelect,int aiMap2[][16]);

void GetLevel1();
void GetLevel2();
void GetLevel3();
void GetLevel4();

int PlayGame(int aiMap[][16],int aiMap2[][16],int iSum,int iImp);

int main(){
   
	while (1){
   
		DesignUI();
	}
} 

void DesignUI(){
   
	int iCh;
	char cNum;
	iCh=WelcomePage();
	if(iCh==49){
   
		GetLevel1();
	}
	if(iCh==50){
   
	else
	    printf("\n\t\t Please input level!(From 1 to 4):");
	    getchar();
	    cNum=getchar();
		switch (cNum){
   
			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(iCh==51){
   
		system("cls");
		exit(0);
	}
}

int WelcomePage(){
   
	int i=0;
	system("cls");
	system("color OE");
	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 Please number 1 to start new game\n\t\t\n");
	printf("\t\t Please number 2 to choose level\n\t\t\n"); 
	printf("\t\t Please number 3 to quit from game\n\t\t\n"); 
	printf("\t\t Please right number to continue:"); 
	while (1){
   
		i=getchar();
		if(i>=49&&i<=51){
   
			return i;
		}
	}
}

int PrintMap(int aiMap[][16],int iImp){
   
	int i,j;
	int iCount=0;
	for(i=0;i<14;i++){
   
		for(j=0;j<16;j++){
   
			switch(aiMap[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:
					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",iImp);
	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 iCount;
}

void SelectColor(int iColor){
   
	HANDLE hConsole =
	    GetStdHandle((STD_OUTPUT_HANDLE));
	SetConsoleTextAttribute(hConsole,iColor);
}

void MoveBox(int aiMap[][16],int iPlayerX,int iPlayerY,int iSelect,int aiMap2[][16]){
   
	int iplayerX1,iPlayerY1;
	int iplayerX2,iPlayerY2;
	switch(iSelect){
   
		case 1:
			iPlayerX1 = iPlayerX - 1;
			iPlayerY1 = iPlayerY;
			iPlayerX2 = iPlayerX - 2;
			iPlayerY2 = iPlayerY;
			break;
		case 2:
			iPlayerX1 = iPlayerX;
			iPlayerY1 = iPlayerY - 1;
			iPlayerX2 = iPlayerX;
			iPlayerY2 = iPlayerY - 2;
			break;
		case 3:
			iPlayerX1 = iPlayerX;
			iPlayerY1 = iPlayerY + 1;
			iPlayerX2 = iPlayerX;
			iPlayerY2 = iPlayerY + 2;
			break;
		case 4:
			iPlayerX1 = iPlayerX + 1;
			iPlayerY1 = iPlayerY;
			iPlayerX2 = iPlayerX + 2;
			iPlayerY2 = iPlayerY;
			break;
		default:
			break;
	}
	
	switch aiMap([iPlayerX1][iPlayerY1]){
   
		case WALL:
			break;
		case SPACE:
		case BOX:
		case TARGET:
			aiMap[iPlayerX1][iPlayerY1] = PERSON;
			if(aiMap2[iPlayerX][iPlayerY]==TARGET||aiMap2[iPlayerX][iPlayerY]==TARGET_IN){
   
				aiMap[iPlayerX][iPlayerY]=TARGET;
			}else{
   
				aiMap[iPlayerX][iPlayerY]=SPACE;
			}
			break;
		case TARGET_IN:
			if(aiMap[iPlayerX][iPlayerY]==TARGET){
   
				aiMap[iPlayerX2][iPlayerY2]=TARGET_IN;
				aiMap[iPlayerX1][iPlayerY1]=PERSON;
			}else if(aiMap[iPlayerX2][iPlayerY2]==SPACE){
   
				aiMap[iPlayerX2][iPlayerY2]=BOX;
				aiMap[iPlayerX1][iPlayerY1]=PERSON;
			}else{
   
				break;
			}
			
			if(aiMap2[iPlayerX][iPlayerY]==TARGET||aiMap2[iPlayerX][iPlayerY]==TARGET_IN){
   
				aiMap[iPlayerX][iPlayerY]==TARGET;
			}else{
   
				aiMap[iPlayerX][iPlayerY]==SPACE;
			}
			break;
	}
}

void GetLevel1(){
   
	int aiMap2[14][16];
	int i,j,iSum=0;
	int aiMap[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(i=0;i<14;i++){
   
		for(j=0;j<16;j++){
   
			aiMap2[i][j]=aiMap[i][j];
			if(aiMap[i][j]==TARGET||aiMap[i][j]==TARGET_IN){
   
				iSum++;
			}
		}
	}
	
	if(PlayGame(aiMap,aiMap2,iSum,2)!=KEY_RETURN){
   
		GetLevel3();
	}	
}
void GetLevel2(){
   
	int aiMap2[14][16];
	int i,j,iSum=0;
	int aiMap[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,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,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(i=0;i<14;i++){
   
		for(j=0;j<16;j++){
   
			aiMap2[i][j]=aiMap[i][j];
			if(aiMap[i][j]==TARGET||aiMap[i][j]==TARGET_IN){
   
				iSum++;
			}
		}
	}
	
	if(PlayGame(aiMap,aiMap2,iSum,2)!=KEY_RETURN){
   
		GetLevel3();
	}	
}

void GetLevel3(){
   
	int aiMap2[14][16];
	int i,j,iSum=0;
	int aiMap[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,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,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(i=0;i<14;i++){
   
		for(j=0;j<16;j++){
   
			aiMap2[i][j]=aiMap[i][j];
			if(aiMap[i][j]==TARGET||aiMap[i][j]==TARGET_IN){
   
				iSum++;
			}
		}
	}
	
	if(PlayGame(aiMap,aiMap2,iSum,3)!=KEY_RETURN){
   
		GetLevel4();
	}	
}

void GetLevel4(){
   
	int aiMap2[14][16];
	int i,j,iSum=0;
	int aiMap[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,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{
   0,0,0,0,0,0,0,0,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(i=0;i<14;i++){
   
		for(j=0;j<16;j++){
   
			aiMap2[i][j]=aiMap[i][j];
			if(aiMap[i][j]==TARGET||aiMap[i][j]==TARGET_IN){
   
				iSum++;
			}
		}
	}
	
	PlayGame(aiMap,aiMap2,iSum,4);
		
}

int PlayGame(int aiMap[][16],int aiMap2[][16],int iSum,int iImp){
   
	int i,j;
	int iPlayerX,iPlayerY;
	char cOp;
	int iNum=0;
	
	while(1){
   
		for(i=0;i<14;i++){
   
			for(j=0;j<16;j++){
   
				if(aiMap[1][j]==PERSON){
   
					break;
				} 
			}
			if(j<16){
   
				break;
			}
		}
		iPlayerX=i;
		iPlayerY=j;
		system("cls");
		iNum=PrintMap(aiMap,iImp);
		if(iNum==iSum){
   
			printf("Congratulations!\n");
			sleep(3000);
			return 0;
		}
		else{
   
			cOp=getch();
			if((cOp=='n')||(cOp=='N')){
   
				return KEY_NEXT;
			}else if((cOp=='q')||(cOp=='Q')){
   
				return KEY_RETURN;
			}
		}
		switch(cOp){
   
			case KEY_UP:
				MoveBox(aiMap,iPlayerX,iPlayerY,1,aiMap2);
				break;
			case KEY_LEFT:
				MoveBox(aiMap,iPlayerX,iPlayerY,2,aiMap2);
				break;
			case KEY_RIGHT:
				MoveBox(aiMap,iPlayerX,iPlayerY,3,aiMap2);
				break;
			case KEY_DOWN:
				MoveBox(aiMap,iPlayerX,iPlayerY,4,aiMap2);
				break;
			default:
				break;
		}
	}
}