C language Push box GCC compile through

Source: Internet
Author: User
Tags assert

The problems that 0X01 encountered

Windows can use the Getch () function, which is entered as an input stream. (simply by pressing a button, you can react without clicking return). But in a Linux environment, there is no connio.h header file for this function. But every time you press the return of the car is still quite inverse days. But I found a replacement on the Internet (thank you Ghost)
http://my.oschina.net/yougui/blog/111345

0X02 Code Implementation

/*******************************************************************
* 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, your control a boy push the Boxs
* to the destination. But you can ' t push the stone and two boxs.
* If you are "R box Touch" 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" 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 '
* All of the ' functions to ' control ' 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 of 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 to 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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.