#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <conio.h>
// 0 -> ' '空地
// 1 -> ▓墙
// 2 -> 人
// 3 -> □箱子
// 4 -> ☆目的地
// 5 -> ★箱子所在目的地
#define M 8
void draw();
int map[M][8] = {
{0, 0, 1, 1, 1, 0, 0, 0},
{0, 0, 1, 4, 1, 0, 0, 0},
{0, 0, 1, 0, 1, 1, 1, 1},
{1, 1, 1, 3, 0, 3, 4, 1},
{1, 4, 0, 3, 2, 1, 1, 1},
{1, 1, 1, 1, 3, 1, 0, 0},
{0, 0, 0, 1, 4, 1, 0, 0},
{0, 0, 0, 1, 1, 1, 0, 0}
};
int indexX = 5, indexY = 5;
int oldValue; // 记录一下小人下一个位置的数值
// 画图函数
__inline void draw()
{
int i, j;
for(i = 0; i < M; i++)
{
for(j = 0; j < 8; j++)
{
switch(map[i][j])
{
case 0:
printf(" ");//空地
break;
case 1:
printf("▓");//墙
break;
case 2:
printf("");//人
break;
case 3:
printf("□");//箱子
break;
case 4:
printf("☆");//目的地
break;
case 5:
printf("★");//箱子在目的地
break;
}
}
printf("\n");
}
}
void fun1()
{
}
void fun2(int v1)
{
printf("fun2 call....\n");
fun1();
}
void move(int row, int colume)
{
if(map[indexX + row][indexY + colume] == 1)
return;
if(map[indexX + row][indexY + colume] == 3)
{
if(map[indexX + 2 * row][indexY + 2 * colume] == 0)
{
map[indexX + 2 * row][indexY + 2 * colume] = 3;
map[indexX + row][indexY + colume] = 0;
}
else if(map[indexX + 2 * row][indexY + 2 * colume] == 4)
{
map[indexX + 2 * row][indexY + 2 * colume] = 5;
map[indexX + row][indexY + colume] = 0;
}
else
return;
}
if(map[indexX + row][indexY + colume] == 5)
{
if(map[indexX + 2 * row][indexY + 2 * colume] == 0)
{
map[indexX + 2 * row][indexY + 2 * colume] = 3;
map[indexX + row][indexY + colume] = 0;
}
else if(map[indexX + 2 * row][indexY + 2 * colume] == 4)
{
map[indexX + 2 * row][indexY + 2 * colume] = 5;
map[indexX + row][indexY + colume] = 0;
}
else
return;
}
// 更改地图小人的位置
map[indexX][indexY] = oldValue;
indexX += row;
indexY += colume;
oldValue = map[indexX][indexY]; // 记录下一个位置的数值
map[indexX][indexY] = 2;
}
int main()
{
int i, j;
char ch;
draw();
while(-1)
{
ch = getch();
switch(ch)
{
case 'w':
move(-1, 0);
break;
case 's':
move(1, 0);
break;
case 'a':
move(0, -1);
break;
default:
move(0, 1);
break;
}
system("cls");
draw();
}
printf("Welcome to the game\n");
system("pause");
return 0;
给你一个新代码!可以看一下注释!望采纳!!
#include<stdio.h>
#include<conio.h>
#include<windows.h>
int map[9][11]={
{0,1,1,1,1,1,1,1,1,1,0},
{0,1,0,0,0,1,0,0,0,1,0},
{0,1,0,0,3,0,0,0,0,1,0},
{0,1,0,3,0,3,3,3,0,1,1},
{0,1,0,0,0,2,0,0,0,0,1},
{1,1,0,0,1,1,1,0,3,0,1},
{1,0,4,4,0,4,0,0,0,0,1},
{1,0,4,4,0,4,4,3,0,1,1},
{1,1,1,1,1,1,1,1,1,1,0}
};//原始的图表,五行六列,其中 0 代表着空白的地方; 1 代表着墙;2 代表着人;
//3 代表着箱子;4 代表着箱子的终点位置。
//图的变化要靠自己来编写数组,通过数字来进行图的构造。
int drawmain();
int tuidong();
int winshu();
int main()//主函数
{
while(1)
{
system("cls");//对其进行清屏
drawmain();
tuidong();
}
printf("shuchu \n");
return 0;
}
//把图形刻画出来
int drawmain()
{
int i,j;
winshu();//调用输赢的函数
for(i=0;i<9;i++)
{
for(j=0;j<11;j++)
{
switch(map[i][j])
{
case 0:
printf(" "); //空白的地方
break;
case 1:
printf("■"); //墙
break;
case 2:
printf("♀"); //人
break;
case 3:
printf("☆"); //箱子
break;
case 4:
printf("◎"); //终点地方
break;
case 6:
printf("♂");//人加终点位置
break;
case 7:
printf("★") ;//箱子加终点位置
break;
}
}
printf("\n");
}
}
//进行小人的移动,整个移动的过程就是数组变化的过程
int tuidong()
{
int count,caw=0;//行和列
int i,j,tui;
for(i=0;i<9;i++){
for (j=0;j<11;j++)
{
if(map[i][j]==2||map[i][j]==6)
{
count=i;
caw=j;
}
}
}
tui=getch();//与getchar()有区别的是:getchar()输入一个字符后需要回车来进行下一个字符的输入,
//比较麻烦 ,getch()则不需要回车就能连续输入多个字符。
switch(tui)
{//上
case 'W':
case 72:
// 1.人的前面是空地;
// 2.人的前面是终点位置;
// 3.人的前面是箱子
//3.1.箱子的前面是空地;
//3.2.箱子的前面是终点位置。
if(map[count-1][caw]==0||map[count-1][caw]==4)
{
map[count][caw]-=2;
map[count-1][caw]+=2;
}
else if(map[count-1][caw]==3||map[count-1][caw]==7)
{
if(map[count-2][caw]==0||map[count-2][caw]==4)
{
map[count][caw]-=2;
map[count-1][caw]-=1;
map[count-2][caw]+=3;
}
}
break;
//下
case 'S':
case 80://键值
if(map[count+1][caw]==0||map[count+1][caw]==4)
{
map[count][caw]-=2;
map[count+1][caw]+=2;
}
else if(map[count+2][caw]==0||map[count+2][caw]==4)
{
if(map[count+1][caw]==3||map[count+1][caw]==7)
{
map[count][caw]-=2;
map[count+1][caw]-=1;
map[count+2][caw]+=3;
}
}
break;
//左
case 'A':
case 75:
if(map[count][caw-1]==0||map[count][caw-1]==4)
{
map[count][caw]-=2;
map[count][caw-1]+=2;
}
else if(map[count][caw-2]==0||map[count][caw-2]==4)
{
if(map[count][caw-1]==3||map[count][caw-1]==7)
{
map[count][caw]-=2;
map[count][caw-1]-=1;
map[count][caw-2]+=3;
}
}
break;
//右
case 'D':
case 77:
if(map[count][caw+1]==0||map[count][caw+1]==4)
{
map[count][caw]-=2;
map[count][caw+1]+=2;
}
else if(map[count][caw+2]==0||map[count][caw+2]==4)
{
if(map[count][caw+1]==3||map[count][caw+1]==7)
{
map[count][caw]-=2;
map[count][caw+1]-=1;
map[count][caw+2]+=3;
}
}
break;
}
}
int winshu()
{
int k = 0;//初始化
int j,i;
for(i=0;i<9;i++)
{
for (j=0;j<11;j++)
{
if(map[i][j]==3)
k++;
}
}
if(k==0)
printf("恭喜你,你赢了!\n");
}