源码如下:
#include<stdio.h>
#include<stdlib.h>
#define ROWS 11
#define COLS 11
int people_cols = 2;
int people_rows = 4;
char map[ROWS][COLS] = {
"##########",
"# #### #",
"# #",
"# X#### #",
"# 0 #",
"##### #",
"# #### #",
"# #",
"# ######",
"# ",
"##########"
};
void showMap();
char enterDirection();
void moveToUp();
void moveToDown();
void moveToLeft();
void moveToRight();
int main()
{
int flag = 1;
while (flag==1) {
system("cls");
showMap();
if (map[9][9] == 'X') {
printf("恭喜你,你通关游戏了,你的智商已经超过3岁小孩了!\nGood!\n");
break;
}
char dir = enterDirection();
printf("你输入的方向是:%c\n", dir);
switch (dir)
{
case 'a':
case 'A':
moveToLeft();
break;
case's':
case 'S':
moveToDown();
break;
case'w':
case 'W':
moveToUp();
break;
case'd':
case 'D':
moveToRight();
break;
case'q':
case 'Q':
printf("你的智商不行啊!这连3岁小孩都会!loser!"),
flag = 0;
break;
}
}
return 0;
}
void showMap() {
for (int i = 0; i < ROWS; i++) {
printf("%s\n", map[i]);
}
}
char enterDirection() {
printf("请输入小人的前进方向(X代表箱子,0代表人) w.上 s.下 a.左 d.右 q.退出游戏(回车确认)\n");
char dir;
rewind(stdin);
dir = getchar();
return dir;
}
void moveToUp()
{
int next_rows=people_rows-1;
int next_cols=people_cols;
if (map[next_rows][next_cols] == ' ') {
map[people_rows][people_cols] = ' ';
map[next_rows][next_cols] = '0';
}
if (map[next_rows][next_cols] == 'X' && map[next_rows - 1][next_cols] == ' ') {
map[people_rows][people_cols] = ' ';
map[next_rows][next_cols] = '0';
map[next_rows - 1][next_cols] = 'X';
}
people_cols = next_cols;
people_rows = next_rows;
}
void moveToDown() {
int next_rows = people_rows+1 ;
int next_cols = people_cols;
if (map[next_rows][next_cols] == ' ') {
map[people_rows][people_cols] = ' ';
map[next_rows][next_cols] = '0';
}
if (map[next_rows][next_cols] == 'X' && map[next_rows +1][next_cols] == ' ') {
map[people_rows][people_cols] = ' ';
map[next_rows][next_cols] = '0';
map[next_rows +1][next_cols] = 'X';
}
people_cols = next_cols;
people_rows = next_rows;
}
void moveToLeft() {
int next_rows = people_rows ;
int next_cols = people_cols-1;
if (map[next_rows][next_cols] == ' ') {
map[people_rows][people_cols] = ' ';
map[next_rows][next_cols] = '0';
}
if (map[next_rows][next_cols] == 'X' && map[next_rows][next_cols - 1] == ' ') {
map[people_rows][people_cols] = ' ';
map[next_rows][next_cols] = '0';
map[next_rows][next_cols - 1] = 'X';
}
people_cols = next_cols;
people_rows = next_rows;
}
void moveToRight() {
int next_rows = people_rows ;
int next_cols = people_cols+1;
if (map[next_rows][next_cols] == ' ') {
map[people_rows][people_cols] = ' ';
map[next_rows][next_cols] = '0';
}
if (map[next_rows][next_cols] == 'X' && map[next_rows ][next_cols+1] == ' ') {
map[people_rows][people_cols] = ' ';
map[next_rows][next_cols] = '0';
map[next_rows ][next_cols+1] = 'X';
}
people_cols = next_cols;
people_rows = next_rows;
}
菜鸟一枚。。。。。。。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。