A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 亮~ 中级黑马   /  2014-6-18 21:17  /  1288 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 亮~ 于 2014-6-18 21:19 编辑

:)黑马程序员:)
------- IOS 第二期培训,期待与您交流! ----------

推箱子游戏:victory::victory::victory:
#include <stdio.h>
void changePosition(char map[][10],int oldX,int oldY,int newX,int newY);
void printMap(char map[][10],int rows,int cols);
//推箱子
int main(int argc, const char * argv[])
{
//    1、初始化地图数据,其中 #代表是墙 O代表小人 X 代表箱子  将箱子推到墙边 胜利
    char map[10][10] = {
        {'#','#','#','#','#','#','#','#','#','#'},
        {'#','O',' ','#','#','#','#',' ',' ','#'},
        {'#',' ','X','#','#','#','#',' ',' ','#'},
        {'#',' ',' ',' ',' ',' ',' ',' ',' ','#'},
        {'#','#','#','#','#','#',' ',' ',' ','#'},
        {'#',' ',' ','#','#','#','#',' ',' ','#'},
        {'#',' ',' ',' ',' ',' ',' ',' ',' ','#'},
        {'#',' ',' ',' ','#','#','#','#','#','#'},
        {'#',' ',' ',' ',' ',' ',' ',' ',' ',' '},
        {'#','#','#','#','#','#','#','#','#','#'}
    };
//2、打印地图
//    for (int i = 0; i < 10; i++) {
//        for (int j = 0; j < 10; j++) {
//            printf("%c",map[j]);
//        }
//        printf("\n");
//    }
    printMap(map, 10, 10);
//3、根据用户的输入移动小人和箱子,w 上 s下 a左 d右 e 退出
   
//3.1提示用户输入
    printf("请输入一个字符小人的移动,w 向上,s 向下,a向左,d向右 e退出\n");
//3.2 接收用户输入的值
    char dir;//定义一个变量接收用户输入的方向
    dir = getchar();
//3.2根据用户的输入判断向那个方向移动小人和箱子
    int px = 1;//小人的x坐标的位置
    int py = 1;//小人的y坐标的位置
   
    int exit = 'e';//退出程序
    int person = 'O';//代表人
    int street = ' ';//路
    int box = 'X';//代表箱子
    //定义两个变量记录小人将要去的下个位置的坐标
    int nextX;
    int nextY;
    //如果向右走
//   定义两个变量保存 方块要去的下个位置的坐标
    int nextBoxX;
    int nextBoxY;
   
    while (dir != exit) {
        
        if ('d' == dir) {
            //如果右边下一个位置是路,小人就向右移动一个位置
            nextX = px +1;
            nextY = py;
        }else if('s' == dir){
            nextX = px;
            nextY = py +1;
        }else if ('w' == dir){
            nextX = px;
            nextY = py-1;
         
        }else if('a' == dir){
            nextX = px - 1;
            nextY = py;
        }
        //去下一个位置
//        1、首先计算假设有箱子,箱子的下个位置是坐标是什么
        nextBoxX=  nextX + (nextX - px);
        nextBoxY = nextY + (nextY - py);
        //2、如果小人下个位置路,则交换小人与路的位置,并改变小人的位置坐标
        if (map[nextY][nextX] == street) {
            changePosition(map, px, py, nextX, nextY);
            py = nextY;
            px = nextX;
//        如果小人下个位置是箱子并且箱子的下个位置是路,则先交换箱子与路的位置,在交换路与小人的位置
        }else if(map[nextY][nextX] == box && map[nextBoxY][nextBoxX]){
            changePosition(map, nextX, nextY, nextBoxX, nextBoxY);
            if (nextBoxY == 9||nextBoxX == 9 || nextX == 0 || nextY == 0) {
                printf("恭喜你,闯关了\n");
                return 0;
            }
            changePosition(map, px, py, nextX, nextY);
            py = nextY;
            px = nextX;
        }
        
        
        //2、打印地图
        printMap(map, 10, 10);
        //getchar();
        dir = getchar();
        if (dir == '\n') {
            dir = getchar();
        }
    }
   
    return 0;
}
//打印地图
void printMap(char map[][10],int rows,int cols)
{
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%c",map[j]);
        }
        printf("\n");
    }

}
//交换地图上两个物体的位置
void changePosition(char map[][10],int oldX,int oldY,int newX,int newY)
{
    char temp = map[oldY][oldX];
    map[oldY][oldX] = map[newY][newX];
    map[newY][newX] = temp;


}

分享一下,共同学习,亲你懂了吗,此程序还能再优化吗?
------- IOS 第二期培训,期待与您交流! ----------

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马