C 语言推箱子gcc编译通过

0X01 遇到的问题

Windows 中能使用 getch() 函数,这个函数是以输入流的方式输入。(简单地说就是按下去一个按键就能有反应,而不用点击回车)。但是在 Linux 环境下没有这个函数也没有 connio.h 的头文件。但是每次按一下还要按回车还是挺逆天的。不过我在网上找到了替代品(感谢幽鬼)
http://my.oschina.net/yougui/blog/111345

0X02 代码实现

/*******************************************************************
 * Project name     : push the boxs
 * Create date      : 2015.10.17
 * Last modify date : 2015.10.19
 * Auther name      : mouse_ts
 * E-mail address   : michaelhaozi@hotmail.com
 * Description      : this is game, you control a boy push the boxs
 * to the destination. but you can't push the stone and two boxs.
 * if you'r box touch the wall , you can't pull it.
 * ****************************************************************/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <termios.h>//using getch()
#include <unistd.h>
#include <assert.h>
//this is constant
#define TRUE   1
#define FALSE  0
#define MAX    10
#define WALL   6
#define PLAYER 7
#define BOX    8
#define BLANK  5
#define DES    9
#define W      'w'
#define A      'a'
#define S      's'
#define D      'd'
//this is game map
int map[MAX][MAX];
int ok = 0;
//player
struct player
{
    int x;
    int y;
}player;
//boxs
struct box
{
    int x;
    int y;
}box_1, box_2, box_3;
//des
struct des
{
    int x;
    int y;
}des_1, des_2, des_3;
//statement function
void initMap();         //init the map
void initPlayer();      //init the player
void initBox();         //init the boxs
void initDes();         //init the des
void printMap();        //print the map
void setMap();          //set the player, boxs, des
char getch();           //getch()
void goUP();            //go up
void goDown();          //go down
void goLeft();          //go left
void goRight();         //go right
int  computingSuccess();//computing how many box seccessd
int main()
{
    char ch;
    system("clear");
    //init the game
    initMap();
    initPlayer();
    initBox();
    setMap();
    printMap();
    //control the boy
    while (ch = getch())
    {
        switch(ch)//where is the boy move
        {
            case W:
                goUP();
                break;
            case A:
                goLeft();
                break;
            case S:
                goDown();
                break;
            case D:
                goRight();
                break;
            defualt:
                printf ("You should press w, a, s, d to control the boy to move\n");
        }
        setMap();
        system("clear");
        printMap();
        if (computingSuccess() == 3)
            break;
        else
            continue;
    }
    system("clear");
    printf ("\n\n\n\n\n\n\n             You win the game!\n");
    getch();
    system("clear");
    return 0;
}
//getch()   by.YouGui   http://my.oschina.net/yougui/blog/111345
char getch()
{
    int c = 0;
    struct termios org_opts, new_opts;
    int res = 0;
    res = tcgetattr(STDIN_FILENO, &org_opts);
    assert(res == 0);
    memcpy(&new_opts, &org_opts, sizeof(new_opts));
    new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
    tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
    c = getchar();
    res = tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
    assert(res == 0);
    return c;
}
//init this map
void initMap()
{
    int i, j;
    for (i = 0; i < MAX; i++)
    {
        for (j = 0; j < MAX; j++)
        {
            map[i][j] = WALL;
        }
    }
    for (i = 2; i < 8; i++)
    {
        map[i][2] = BLANK;
        map[i][3] = BLANK;
        map[i][5] = BLANK;
        map[i][6] = BLANK;
        map[i][7] = BLANK;
    }
    map[5][4] = BLANK;
    initDes();
}
//print map
void printMap()
{
    printf ("This is a game !\n");
    int i, j;
    for (i = 0; i < MAX; i++)
    {
        for (j = 0; j < MAX; j++)
        {
            if (map[i][j] == WALL)
                printf (" # ");
            else if (map[i][j] == BOX)
                printf (" @ ");
            else if (map[i][j] == PLAYER)
                printf (" X ");
            else if (map[i][j] == BLANK)
                printf ("   ");
            else if (map[i][j] == DES)
                printf (" O ");
        }
        printf ("\n");
    }
}
//init the player
void initPlayer()
{
    player.x = 2;
    player.y = 2;
}
//init the boxs
void initBox()
{
    box_1.x = 3;
    box_1.y = 6;
    box_2.x = 4;
    box_2.y = 3;
    box_3.x = 6;
    box_3.y = 3;
}
//init the des
void initDes()
{
    des_1.x = 5;
    des_1.y = 7;
    des_2.x = 6;
    des_2.y = 7;
    des_3.x = 7;
    des_3.y = 7;
}
//set map
void setMap()
{
    int i, j;
    //set blank
    for (i = 2; i < 8; i++)
    {
        map[i][2] = BLANK;
        map[i][3] = BLANK;
        map[i][5] = BLANK;
        map[i][6] = BLANK;
        map[i][7] = BLANK;
    }
    map[5][4] = BLANK;
    //set des
    map[des_1.x][des_1.y] = DES;
    map[des_2.x][des_2.y] = DES;
    map[des_3.x][des_3.y] = DES;
    //set player
    map[player.x][player.y] = PLAYER;
    //set box
    map[box_1.x][box_1.y] = BOX;
    map[box_2.x][box_2.y] = BOX;
    map[box_3.x][box_3.y] = BOX;
}
//computing the success move the box to the des
int computingSuccess()
{
    int num = 0;
    if (map[des_1.x][des_1.y] == BOX)
        num++;
    if (map[des_2.x][des_2.y] == BOX)
        num++;
    if (map[des_3.x][des_3.y] == BOX)
        num++;
    return num;
}
/*
 * after this is control your boy to move up down left and right
 * all of the  functions to control the boy to move
 */
//control the boy go up
void goUP()
{
    if (map[player.x - 1][player.y] == BLANK ||
        map[player.x - 1][player.y] == DES)
    {
        player.x--;
        return ;
    }
    if (player.x - 1 == box_1.x && player.y == box_1.y &&
        map[box_1.x - 1][box_1.y] == BLANK ||
        player.x - 1 == box_1.x && player.y == box_1.y &&
        map[box_1.x - 1][box_1.y] == DES)
    {
        box_1.x--;
        player.x--;
        return ;
    }
    else if (player.x - 1 == box_2.x && player.y == box_2.y &&
             map[box_2.x - 1][box_2.y] == BLANK ||
             player.x - 1 == box_2.x && player.y == box_2.y &&
             map[box_2.x - 1][box_2.y] == DES)
    {
        box_2.x--;
        player.x--;
        return ;
    }
    else if (player.x - 1 == box_3.x && player.y == box_3.y &&
             map[box_3.x - 1][box_3.y] == BLANK ||
             player.x - 1 == box_3.x && player.y == box_3.y &&
             map[box_3.x - 1][box_3.y] == DES)
    {
        box_3.x--;
        player.x--;
        return ;
    }
}
//control the boy go down
void goDown()
{
    if (map[player.x + 1][player.y] == BLANK ||
        map[player.x + 1][player.y] == DES)
        player.x++;
    if (player.x + 1 == box_1.x && player.y == box_1.y &&
        map[box_1.x + 1][box_1.y] == BLANK ||
        player.x + 1 == box_1.x && player.y == box_1.y &&
        map[box_1.x + 1][box_1.y] == DES)
    {
        box_1.x++;
        player.x++;
        return ;
    }
    else if (player.x + 1 == box_2.x && player.y == box_2.y &&
             map[box_2.x + 1][box_2.y] == BLANK ||
             player.x + 1 == box_2.x && player.y == box_2.y &&
             map[box_2.x + 1][box_2.y] == DES)
    {
        box_2.x++;
        player.x++;
        return ;
    }
    else if (player.x + 1 == box_3.x && player.y == box_3.y &&
             map[box_3.x + 1][box_3.y] == BLANK ||
             player.x + 1 == box_3.x && player.y == box_3.y &&
             map[box_3.x + 1][box_3.y] == DES)
    {
        box_3.x++;
        player.x++;
        return ;
    }
}
//control the boy go left
void goLeft()
{
    if (map[player.x][player.y - 1] == BLANK ||
        map[player.x][player.y - 1] == DES)
        player.y--;
    if (player.x == box_1.x && player.y - 1 == box_1.y &&
        map[box_1.x][box_1.y - 1] == BLANK ||
        player.x == box_1.x && player.y - 1 == box_1.y &&
        map[box_1.x][box_1.y - 1] == DES)
    {
        box_1.y--;
        player.y--;
        return ;
    }
    else if (player.x == box_2.x && player.y - 1 == box_2.y &&
             map[box_2.x][box_2.y - 1] == BLANK ||
             player.x == box_2.x && player.y - 1 == box_2.y &&
             map[box_2.x][box_2.y - 1] == DES)
    {
        box_2.y--;
        player.y--;
        return ;
    }

时间: 2024-12-09 09:48:27

C 语言推箱子gcc编译通过的相关文章

c语言-C语言的推箱子在推箱子移动的时候存在一个错误,求大牛解答,并且修改程序绝对不麻烦,谢谢了。

问题描述 C语言的推箱子在推箱子移动的时候存在一个错误,求大牛解答,并且修改程序绝对不麻烦,谢谢了. #include #include #include //定义墙壁的高和宽 #define H 11 #define W 26 int scr[H][W] = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1}, {1,0,0,0,0,0,0

vs5015-求大神vs2015 环境下纯c语言编程推箱子游戏,QQ或微信红包重谢

问题描述 求大神vs2015 环境下纯c语言编程推箱子游戏,QQ或微信红包重谢 本人c语言初学者,求推向资源代码.我已写了部分,但不会将其联系起来,将其显示出来,刷屏出来...就是将地图呈现出.希望看到大神的源代码有所感悟 #ifndef _DITEM #define _DITEM #define X 8 #define Y 8 enum Kind{nul=0,wall,human,box,well,finish,empty,qiu}; typedef struct _item { enum K

C语言实现的推箱子游戏

/* 这是彭搏同学的推箱子游戏,大家试试玩,谁有更好的Idea?*/ #include"stdio.h" #include"bios.h" #define LEFT 75 #define RIGHT 77 #define UPPER 72 #define DOWN 80 #define ESC 27 struct Boxss /*定义箱子结构体,其中包含坐标属性*/ { int x,y; }; union keyboard /*定义读取键盘码的共用体类型*/ { u

c语言-Linux下使用gcc编译c11程序是不是过于麻烦了?

问题描述 Linux下使用gcc编译c11程序是不是过于麻烦了? 每次都要输入-std=c11 是不是过于麻烦了?怎么让它默认为-std=c11? 解决方案 因为它默认是c89等标准,所以需要指定 你可以写一个makefile ,这样每次只用make来编译

c语言编程-gcc编译没有错,就出不了结果,谁能帮我看下,多谢

问题描述 gcc编译没有错,就出不了结果,谁能帮我看下,多谢 #include int main(int argc, char *argv[]) { int n,x,y,i=0,a[100]; scanf("%d",&n); for(i=0;i<n;i++) { a[i]=i+1; } x=0; y=n; while(y>1) { for(i=0;i<n;i++) if(a[i]!=0) { x++; if(x%3==0) { a[i] = 0; y--; }

c语言-aix下使用gcc编译c代码,不支持tls?

问题描述 aix下使用gcc编译c代码,不支持tls? aix下使用gcc编译c代码,不支持tls error: thread-local storage not supported for this target 代码中使用了"__thread"关键字 有人知道怎么解决吗? 解决方案 linux下使用gcc命令编译代码Linux系统中使用GCC 对CPU参数优化代码编译GCC编译C/C++/汇编代码

解析C语言与C++的编译模型_C 语言

首先简要介绍一下C的编译模型:限于当时的硬件条件,C编译器不能够在内存里一次性地装载所有程序代码,而需要将代码分为多个源文件,并且分别编译.并且由于内存限制,编译器本身也不能太大,因此需要分为多个可执行文件,进行分阶段的编译.在早期一共包括7个可执行文件:cc(调用其它可执行文件),cpp(预处理器),c0(生成中间文件),c1(生成汇编文件),c2(优化,可选),as(汇编器,生成目标文件),ld(链接器).1. 隐式函数声明为了在减少内存使用的情况下实现分离编译,C语言还支持"隐式函数声明&

java 3D 推箱子

问题描述 我想做一个3D版的推箱子,自己又不太会,麻烦会的人将使用Java语言编写出的3D效果的推箱子,发给我一下,谢谢.QQ:773853402@qq.com 解决方案 解决方案二:自己试着做,过程就是学习解决方案三:可是现在我啥都不会,时间有点紧呢,你知道Java和3DMAX结合制作出推箱子,需要怎么弄不?

和菜鸟一起学c之gcc编译过程及其常用编译选项【转】

转自:http://blog.csdn.net/eastmoon502136/article/details/8162626 版权声明:本文为博主东月之神原创文章,未经博主允许不得转载.        上篇文章,知道了,C代码编译后存放在内存中的位置,那么C代码的整个编译过程又是怎样的呢?一条命令gcc hello.c就可以编译成可执行程序a.out,然后./a.out之后就可以执行hello.c这个程序的代码了.下面的文章分析的不错,就整理了下. hello.c:     [html] vie