推箱子源代码初步:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define boolean int
#define true 1
#define false 0 #define ROWS 10
//之所以定义为11,是因为字符串的尾部的\0
#define COLUMNS 11 //根据程序定义或者文件读入来构建地图,然后把他绘制到屏幕上
void DrawMap(char map[][COLUMNS]);
void SwapXY(char map[][COLUMNS], int *x1, int *y1, int *x2, int *y2);
boolean IsLose(char map[][COLUMNS], int boxPositionX, int boxPositionY, char condition);
//读取文件中的地图和人物、箱子的位置
void LoadMap(char Map[][COLUMNS], int *boxPositionX, int *boxPositionY, int *manPositionX, int *manPositionY);
//在推箱子的过程中接收用户的输入,然后做出相应的处理
void PushBox(int *boxPositionX, int *boxPositionY, int *manPositionX, int *manPositionY); int main()
{
char wall = '#';
char box = 'X';
char street = ' ';
int boxPositionX,boxPositionY;
int manPositionX,manPositionY; //定义地图的出口,也就是箱子到达此处就赢了
int winPositionX = ROWS -;
int winPositionY = COLUMNS - ; boolean IsWin = false; //int manNextPositionX,manNextPositionY; char Map[ROWS][COLUMNS] =
{
"##########",
"#0 #### #",
"# X#### #",
"# #",
"###### #",
"# #### #",
"# #",
"# ######",
"# ",
"##########"
};
DrawMap(Map);
LoadMap(Map, &boxPositionX, &boxPositionY, &manPositionX, &manPositionY); while(!IsWin)
{
int boxNextPositionX = boxPositionX;
int boxNextPositionY = boxPositionY;
int manNextPositionX = manPositionX;
int manNextPositionY = manPositionY;
//获取用户的输入
PushBox(&boxNextPositionX, &boxNextPositionY, &manNextPositionX, &manNextPositionY);
//
system("CLS"); //这里处理走之后的效果 TODO:
if(Map[boxNextPositionX][boxNextPositionY] == street && Map[manNextPositionX][manNextPositionY] == box)
{
SwapXY(Map,&boxPositionX, &boxPositionY, &boxNextPositionX, &boxNextPositionY);
SwapXY(Map,&manPositionX, &manPositionY, &manNextPositionX, &manNextPositionY); }
if(Map[manNextPositionX][manNextPositionY] == street)
{
SwapXY(Map,&manPositionX, &manPositionY, &manNextPositionX, &manNextPositionY); } DrawMap(Map); if(boxPositionX == winPositionX && boxPositionY == winPositionY)
{
printf("You Are Win!\n");
IsWin = true; }
if(IsLose(Map,boxPositionX, boxPositionY, wall))
{
printf("You Are Lose!\n");
break;
}
}
system("Pause");
return ; } void DrawMap(char map[][COLUMNS])
{
for(int i = ; i < ROWS; i++)
{
printf("%s\n", map[i]);
}
}
void SwapXY(char map[][COLUMNS], int *x1, int *y1, int *x2, int *y2)
{
char temp = map[*x1][*y1];
map[*x1][*y1] = map[*x2][*y2];
map[*x2][*y2] = temp; *x1 = *x2;
*y1 = *y2;
}
boolean IsLose(char map[][COLUMNS], int boxPositionX, int boxPositionY, char condition)
{
if((map[boxPositionX + ][boxPositionY] == condition && map[boxPositionX][boxPositionY - ] == condition)
||(map[boxPositionX - ][boxPositionY] == condition && map[boxPositionX][boxPositionY - ] == condition)
||(map[boxPositionX - ][boxPositionY] == condition && map[boxPositionX][boxPositionY + ] == condition)
||(map[boxPositionX + ][boxPositionY] == condition && map[boxPositionX][boxPositionY + ] == condition)
)
{
return true;
}
return false;
}
void LoadMap(char Map[][COLUMNS], int *boxPositionX, int *boxPositionY, int *manPositionX, int *manPositionY)
{
*boxPositionX = ;
*boxPositionY = ;
*manPositionX = ;
*manPositionY = ;
}
void PushBox(int *boxPositionX, int *boxPositionY, int *manPositionX, int *manPositionY)
{
char direction;
printf("请输入一个方向 [w,a,s,d]分别代表[上下左右]\n\n");
direction = getch();
switch(direction)
{
case 'W':
case 'w':
(*manPositionX)--;
(*boxPositionX)--;
break;
case 'A':
case 'a':
(*manPositionY)--;
(*boxPositionY)--;
break;
case 'S':
case 's':
(*manPositionX)++;
(*boxPositionX)++;
break;
case 'D':
case 'd':
(*manPositionY)++;
(*boxPositionY)++;
break;
default:
break;
//DO NOTHING:
}
}