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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© muyan091115 中级黑马   /  2016-5-17 22:52  /  700 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

main.c

//
//  main.c
//  Game
//
//  Created by 蒋伟 on 16/5/15.
//  Copyright (c) 2016年 蒋伟. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include "caoZuo.h"

int main(int argc, const char * argv[]) {


    while(1){

        //清屏
        system("clear");
        //打印地图
        printfMap();
        //判断是否胜利
        if(isfinish()){
            printf("你赢了");
            return 0;
        }
        //获得用户下一步操作
        char caoZuo = getCaoZuo();
        //进行操作
        switch(caoZuo){
            case 'W':
            case 'w':
                moveUp();
                break;
            case 'S':
            case 's':
                moveDown();
                break;
            case 'A':
            case 'a':
                moveLeft();
                break;
            case 'D':
            case 'd':
                moveRight();
                break;
            case 'Q':
            case 'q':
                printf("你弱爆了!");
                return 0;
        }
    }
    return 0;
}


caoZuo.c

//
//  caoZuo.c
//  Day11
//
//  Created by 蒋伟 on 16/5/15.
//  Copyright (c) 2016年 蒋伟. All rights reserved.
//

#include "caoZuo.h"
#define ROWS 10
#define COLS 10

char map[ROWS][COLS] = {{'#','#','#','#','#','#','#','#','#','#'},
                                    {'#','O',' ' ,'#','#','#','#',' ' ,' ' ,'#'},
                                    {'#',' ' ,'X','#','#','#','#',' ' ,' ' ,'#'},
                                    {'#',' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,'#'},
                                    {'#','#','#','#','#','#',' ' ,' ' ,' ' ,'#'},
                                    {'#',' ' ,' ' ,'#','#','#','#',' ' ,' ' ,'#'},
                                    {'#',' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,'#'},
                                    {'#',' ' ,' ' ,' ' ,'#','#','#','#','#','#'},
                                    {'#',' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' },
                                    {'#','#','#','#','#','#','#','#','#','#'}};


int peopleRows = 1;
int peopleCols = 1;
int boxRows = 2;
int boxCols = 2;

/**
*  打印地图
*/
void printfMap(){
    for(int i = 0; i < ROWS; i++){
        for(int j = 0; j < COLS; j++){
            printf("%c ",map[j]);
        }
        printf("\n");
    }
}

/**
*  判断是否赢得胜利
*
*  @return 判断结果
*/
int isfinish(){
    if(map[8][9] == 'X'){
        return 1;
    }else{
        return 0;
    }
}

/**
*  获得用户下一步的操作
*
*  @return 用户下一步操作
*/
char getCaoZuo(){
    printf("W:向上\nS:向下\nA:向左\nD:向右\nQ:退出\n请输入:\n ");
    char ch ;
    rewind(stdin);
    scanf("%c",&ch);
    getchar();
    return ch;
}

/**
*  向上移动
*/
void moveUp(){
    int nextRows = peopleRows - 1;
    int nextCols = peopleCols;
    if(map[nextRows][nextCols] == ' '){
        map[peopleRows][peopleCols] = ' ';
        map[nextRows][nextCols] = 'O';
        peopleRows = nextRows;
        peopleCols = nextCols;
    }
    if((map[nextRows][nextCols] == 'X') && (map[nextRows - 1][nextCols] == ' ')){
        map[peopleRows][peopleCols] = ' ';
        map[nextRows][nextCols] = 'O';
        map[nextRows - 1][nextCols] = 'X';
        peopleRows = nextRows;
        peopleCols = nextCols;
        boxRows = nextRows - 1;
        boxCols = nextCols;
    }
}

/**
*  向下移动
*/
void moveDown(){
    int nextRows = peopleRows + 1;
    int nextCols = peopleCols;
    if(map[nextRows][nextCols] == ' '){
        map[peopleRows][peopleCols] = ' ';
        map[nextRows][nextCols] = 'O';
        peopleRows = nextRows;
        peopleCols = nextCols;
    }
    if((map[nextRows][nextCols] == 'X') && (map[nextRows + 1][nextCols] == ' ')){
        map[peopleRows][peopleCols] = ' ';
        map[nextRows][nextCols] = 'O';
        map[nextRows + 1][nextCols] = 'X';
        peopleRows = nextRows;
        peopleCols = nextCols;
        boxRows = nextRows + 1;
        boxCols = nextCols;
    }
}

/**
*  向左移动
*/
void moveLeft(){
    int nextRows = peopleRows;
    int nextCols = peopleCols - 1;
    if(map[nextRows][nextCols] == ' '){
        map[peopleRows][peopleCols] = ' ';
        map[nextRows][nextCols] = 'O';
        peopleRows = nextRows;
        peopleCols = nextCols;
    }
    if((map[nextRows][nextCols] == 'X') && (map[nextRows][nextCols - 1] == ' ')){
        map[peopleRows][peopleCols] = ' ';
        map[nextRows][nextCols] = 'O';
        map[nextRows][nextCols - 1] = 'X';
        peopleRows = nextRows;
        peopleCols = nextCols;
        boxRows = nextRows;
        boxCols = nextCols - 1;
    }
}

/**
*  向右移动
*/
void moveRight(){
    int nextRows = peopleRows;
    int nextCols = peopleCols + 1;
    if(map[nextRows][nextCols] == ' '){
        map[peopleRows][peopleCols] = ' ';
        map[nextRows][nextCols] = 'O';
        peopleRows = nextRows;
        peopleCols = nextCols;
    }
    if((map[nextRows][nextCols] == 'X') && (map[nextRows][nextCols + 1] == ' ')){
        map[peopleRows][peopleCols] = ' ';
        map[nextRows][nextCols] = 'O';
        map[nextRows][nextCols + 1] = 'X';
        peopleRows = nextRows;
        peopleCols = nextCols;
        boxRows = nextRows;
        boxCols = nextCols + 1;
    }
}


caoZuo.h

//
//  caoZuo.h
//  Day11
//
//  Created by 蒋伟 on 16/5/15.
//  Copyright (c) 2016年 蒋伟. All rights reserved.
//

#ifndef __Day11__caoZuo__
#define __Day11__caoZuo__

#include <stdio.h>

void printfMap();
int isfinish();
char getCaoZuo();
void moveUp();
void moveDown();
void moveLeft();
void moveRight();

#endif /* defined(__Day11__caoZuo__) */



1 个回复

倒序浏览
!!!!!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马