黑马程序员技术交流社区
标题:
迷宫小游戏~~~~~哈哈
[打印本页]
作者:
ydy2511000
时间:
2016-1-9 12:54
标题:
迷宫小游戏~~~~~哈哈
//
// main.c
// 迷宫小游戏
/*
###########
#0### #
# ### ### #
# ### ### #
# ### # #
# ### # #
### # #
######### #
*/
#include <stdlib.h>
#include <stdio.h>
#define cows 8
#define cols 11
void showMap(char map[][cols]);
char enterDirection();
int moveUp();
int moveDown();
int moveLeft();
int moveRight();
char map [cows][cols]=
{
{'#','#','#','#','#','#','#','#','#','#','#',},
{'#','0','#','#','#',' ',' ',' ',' ',' ','#',},
{'#',' ','#','#','#',' ','#','#','#',' ','#',},
{'#',' ','#','#','#',' ','#','#','#',' ','#',},
{'#',' ','#','#','#',' ',' ',' ','#',' ','#',},
{'#',' ',' ',' ','#','#','#',' ','#',' ','#',},
{'#','#','#',' ',' ',' ',' ',' ','#',' ','#',},
{'#','#','#','#','#','#','#','#','#',' ','#',}
};
int main(int argc, const char * argv[])
{
//显示地图
//让用户输入控制方向
//判断移动,并不断重复,判断是否走出,直到走出为止
while(1)
{
system("clear"); //先用system-clear函数清除终端的显示
showMap(map); //显示地图
char dir = enterDirection(); //取得用户的移动指令,用char型数据接收
switch(dir) //根据移动的指令,将“小人”进行移动
{
case'w':
moveUp();
break;
case's':
moveDown();
break;
case'a':
moveLeft();
break;
case'd':
moveRight();
break;
}
if(map[7][9] == '0')
{
break;
}
}
system("clear");
showMap(map);
printf("牛逼牛逼!\n");
return 0;
}
//显示地图
void showMap(char map[][cols])
{
for(int i = 0; i < cows; i++)
{
for(int j = 0; j < cols; j++)
{
printf("%2c\t",map[i][j]);
}
printf("\n");
}
}
//接收用户的移动指令
char enterDirection()
{
char dir = 'o';
printf("输入移动方向(w 上,s 下,a 左,d 右):\n");
rewind(stdin);
scanf("%c",&dir);
return dir;
}
//上移
int moveUp()
{
for(int i = 0; i < cows; i++)
{
for(int j = 0; j < cols; j++)
{
if(map[i][j] == '0' && map[i-1][j] != '#')
{
int temp = map[i][j];
map[i][j] = map[i - 1][j];
map[i - 1][j] = temp;
return 0;
}
}
}
return 0;
}
//下移
int moveDown()
{
for(int i = 0; i < cows; i++)
{
for(int j = 0; j < cols; j++)
{
if(map[i][j] == '0' && map[i+1][j] != '#')
{
int temp = map[i][j];
map[i][j] = map[i + 1][j];
map[i + 1][j] = temp;
return 0;
}
}
}
return 0;
}
//左移
int moveLeft()
{
for(int i = 0; i < cows; i++)
{
for(int j = 0; j < cols; j++)
{
if(map[i][j] == '0' && map[i][j-1] != '#')
{
int temp = map[i][j];
map[i][j] = map[i][j - 1];
map[i][j - 1] = temp;
return 0;
}
}
}
return 0;
}
//右移
int moveRight()
{
for(int i = 0; i < cows; i++)
{
for(int j = 0; j < cols; j++)
{
if(map[i][j] == '0' && map[i][j + 1] != '#')
{
int temp = map[i][j];
map[i][j] = map[i][j + 1];
map[i][j + 1] = temp;
return 0;
}
}
}
return 0;
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2