C语言版极简推箱子(数组版)
Posted BEI_TIAN_XUAN
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言版极简推箱子(数组版)相关的知识,希望对你有一定的参考价值。
源码如下:
#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;
}
菜鸟一枚。。。。。。。
以上是关于C语言版极简推箱子(数组版)的主要内容,如果未能解决你的问题,请参考以下文章