Current location - Education and Training Encyclopedia - Graduation thesis - C language box pushing paper
C language box pushing paper
/* A push box game source code written in C language */

/* This game is in character mode. Please do not run it under Chinese dos. The game has passed the debugging under TURBO C */

# include & ltdos.h & gt

# include & ltstdio.h & gt

# include & ltctype.h & gt

# include & ltconio.h & gt

# include & ltbios.h & gt

# include & ltalloc.h & gt

/* Define a two-dimensional array ghouse to record the status of each point on the screen.

Where: 0 means nothing, "B" means a box, "W" means a wall, "M" means a destination, and "I" means that the box is at the destination. */

char gho use[20][20];

/* The following function is a direct screen writing function, which is cool! My friend told me. */

char far * screen =(char far *)0xb 8000000;

void putchxy(int y,int x,char ch,char fc,char bc)

{

Screen [(x *160)+(y <; & lt 1)+0]= ch;

Screen [(x *160)+(y <; & lt 1)+ 1]=(BC * 16)+fc;

}

/* Define the data structure for judging whether to win or not */

Typedef structure winer {

int x,y;

struct winer * p;

} winer

/* Data structure of box position */

Typedef structural frame {

int x,y;

Structure box * next

} box;

The/* function draws a wall at a specific coordinate and records the state with an array */

void printwall(int x,int y)

{

Putchxy(y- 1, x- 1, 2 19, green, black);

ghouse[x][y]= ' w ';

}

The/* function draws a box at a specific coordinate and records the state with an array */

void printbox(int x,int y)

{

Putchxy(y- 1, x- 1, 10, white, black);

ghouse[x][y]= ' b ';

}

The/* function draws the destination on a specific coordinate and records the state with an array */

void printwhither 1(int x,int y,winer **win,winer **pw)

{

winer * qw

Putchxy(y- 1, x- 1,' *', yellow, black);

ghouse[x][y]= ' m ';

if(*win==NULL)

{

* win = * pw = qw =(winer *)malloc(sizeof(winer));

(* pw)-& gt; x = x(* pw)-& gt; y = y(* pw)-& gt; p = NULL

}

other

{

qw =(winer *)malloc(sizeof(winer));

qw-& gt; x = xqw-& gt; y = y(* pw)-& gt; p = qw(* pw)= qw; qw-& gt; p = NULL

}

}

The/* function draws the destination on a specific coordinate and records the state with an array */

void printwhither(int x,int y)

{

Putchxy(y- 1, x- 1,' *', yellow, black);

ghouse[x][y]= ' m ';

}

/* The function of drawing people on specific coordinates */

void printman(int x,int y)

{

gotoxy(y,x);

_ AL = 02_ CX = 0 1; _ AH = 0xa

gen interrupt(0x 10);

}

/* The function of drawing a box on the specific coordinates of the destination and recording the state with an array */

void printboxin(int x,int y)

{

Putchxy(y- 1, x- 1, 10, yellow, black);

ghouse[x][y]= ' I ';

}

/* Initialize function, initialize array and screen */

Void initialization ()

{

int i,j;

for(I = 0; I & lt20; i++)

for(j = 0; j & lt20; j++)

ghouse[I][j]= 0;

_ AL = 3;

_ AH = 0;

gen interrupt(0x 10);

gotoxy(40,4);

Printf ("Welcome to the world of boxes!" );

gotoxy(40,6);

Printf ("Press up, down, left and right to play." );

gotoxy(40,8);

Printf ("Press Esc to exit." );

gotoxy(40 10);

Printf ("Press spacebar to reset the game." );

gotoxy(40 12);

Printf ("Producer: Wang Dehao." );

gotoxy(40 14);

Printf ("March 30, 2003." );

}

/* First-level image initialization */

winer *inithouse 1()

{

int x,y;

winer *win=NULL,* pw

for(x= 1,y = 5; y & lt=9; y++)

printwall(x+4,y+ 10);

for(y=5,x = 2; x & lt=5; x++)

printwall(x+4,y+ 10);

for(y=9,x = 2; x & lt=5; x++)

printwall(x+4,y+ 10);

for(y= 1,x = 3; x & lt=8; x++)

printwall(x+4,y+ 10);

for(x=3,y = 3; x & lt=5; x++)

printwall(x+4,y+ 10);

for(x=5,y = 8; x & lt=9; x++)

printwall(x+4,y+ 10);

for(x=7,y = 4; x & lt=9; x++)

printwall(x+4,y+ 10);

for(x=9,y = 5; y & lt=7; y++)

printwall(x+4,y+ 10);

for(x=8,y = 2; y & lt=3; y++)

printwall(x+4,y+ 10);

printwall(5+4,4+ 10);

printwall(5+4,7+ 10);

printwall(3+4,2+ 10);

printbox(3+4,6+ 10);

printbox(3+4,7+ 10);

printbox(4+4,7+ 10);

Print Whiteher1(4+4,2 2+ 10/0, & won. pw);

Print Whiteher1(5+4,2 2+ 10/0, & won. pw);

Print Whiteher1(6+4,2 2+ 10/0, & won. pw);

printman(2+4,8+ 10);

Win back;

}

/* Third-level image initialization */

winer *inithouse3()

{int x,y;

winer *win=NULL,* pw

for(x= 1,y = 2; y & lt=8; y++)

printwall(x+4,y+ 10);

for(x=2,y = 2; x & lt=4; x++)

printwall(x+4,y+ 10);

for(x=4,y = 1; y & lt=3; y++)

printwall(x+4,y+ 10);

for(x=5,y = 1; x & lt=8; x++)

printwall(x+4,y+ 10);

for(x=8,y = 2; y & lt=5; y++)

printwall(x+4,y+ 10);

for(x=5,y = 5; x & lt=7; x++)

printwall(x+4,y+ 10);

for(x=7,y = 6; y & lt=9; y++)

printwall(x+4,y+ 10);

for(x=3,y = 9; x & lt=6; x++)

printwall(x+4,y+ 10);

for(x=3,y = 6; y & lt=8; y++)

printwall(x+4,y+ 10);

printwall(2+4,8+ 10);

printwall(5+4,7+ 10);

printbox(6+4,3+ 10);

printbox(4+4,4+ 10);

printbox(5+4,6+ 10);

Print Whiteher1(2+4,5 5+ 10/0, & won. pw);

Print Whiteher1(2+4,6 6+ 10/0, & won. pw);

Print Whiteher1(2+4,7 7+ 10/0, & won. pw);

printman(2+4,4+ 10);

Win back;

}

/* Second-level image initialization */

winer *inithouse2()

{int x,y;

winer *win=NULL,* pw

for(x= 1,y = 4; y & lt=7; y++)

printwall(x+4,y+ 10);

for(x=2,y = 2; y & lt=4; y++)

printwall(x+4,y+ 10);

for(x=2,y = 7; x & lt=4; x++)

printwall(x+4,y+ 10);

for(x=4,y = 1; x & lt=8; x++)

printwall(x+4,y+ 10);

for(x=8,y = 2; y & lt=8; y++)

printwall(x+4,y+ 10);

for(x=4,y = 8; x & lt=8; x++)

printwall(x+4,y+ 10);

for(x=4,y = 6; x & lt=5; x++)

printwall(x+4,y+ 10);

for(x=3,y = 2; x & lt=4; x++)

printwall(x+4,y+ 10);

for(x=4,y = 4; x & lt=5; x++)

printwall(x+4,y+ 10);

printwall(6+4,3+ 10);

printbox(3+4,5+ 10);

printbox(6+4,6+ 10);

printbox(7+4,3+ 10);

Print Whiteher1(5+4,7 7+ 10/0, & won. pw);

Print Whiteher1(6+4,7 7+ 10/0, & won. pw);

Print Whiteher1(7+4,7 7+ 10/0, & won. pw);

printman(2+4,6+ 10);

Win back;

}

/* Initialization of the fourth level image */

winer *inithouse4()

{

int x,y;

winer *win=NULL,* pw

for(x= 1,y = 1; y & lt=6; y++)

printwall(x+4,y+ 10);

for(x=2,y = 7; y & lt=8; y++)

printwall(x+4,y+ 10);

for(x=2,y = 1; x & lt=7; x++)

printwall(x+4,y+ 10);

for(x=7,y = 2; y & lt=4; y++)

printwall(x+4,y+ 10);

for(x=6,y = 4; y & lt=9; y++)

printwall(x+4,y+ 10);

for(x=3,y = 9; x & lt=5; x++)

printwall(x+4,y+ 10);

for(x=3,y = 3; y & lt=4; y++)

printwall(x+4,y+ 10);

printwall(3+4,8+ 10);

printbox(3+4,5+ 10);

printbox(4+4,4+ 10);

printbox(4+4,6+ 10);

printbox(5+4,5+ 10);

printbox(5+4,3+ 10);

Print Whiteher1(3+4,7 7+ 10/0, & won. pw);

Print Whiteher1(4+4,7 7+ 10/0, & won. pw);

Print Whiteher1(5+4,7 7+ 10/0, & won. pw);

Print Whiteher1(4+4,8 8+ 10/0, & won. pw);

Print Whiteher1(5+4,8 8+ 10/0, & won. pw);

printman(2+4,2+ 10);

Win back;

}

/* Move the boxes on the vacant lot to the vacant lot */

movebox(int x,int y,char a)

{

Switch (a)

{

case ' u ':ghouse[x- 1][y]= 0; printf(" ");

printbox(x-2,y); printman(x- 1,y);

ghouse[x-2][y]= ' b '; Break;

Case "D": ghohouse [x+1] [y] = 0; printf(" ");

printbox(x+2,y); printman(x+ 1,y);

ghouse[x+2][y]= ' b '; Break;

case ' l ':ghouse[x][y- 1]= 0; printf(" ");

printbox(x,y-2); printman(x,y- 1);

ghouse[x][y-2]= ' b '; Break;

case ' r ':ghouse[x][y+ 1]= 0; printf(" ");

printbox(x,y+2); printman(x,y+ 1);

ghouse[x][y+2]= ' b '; Break;

Default: break

}

}

/* Move the box on the destination to the open space */

moveinbox(int x,int y,char a)

{

Switch (a)

{

case ' u ':ghouse[x- 1][y]= ' m '; printf(" ");

printbox(x-2,y); printman(x- 1,y);

ghouse[x-2][y]= ' b '; Break;

case ' d ':ghouse[x+ 1][y]= ' m '; printf(" ");

printbox(x+2,y); printman(x+ 1,y);

ghouse[x+2][y]= ' b '; Break;

case ' l ':ghouse[x][y- 1]= ' m '; printf(" ");

printbox(x,y-2); printman(x,y- 1);

ghouse[x][y-2]= ' b '; Break;

case ' r ':ghouse[x][y+ 1]= ' m '; printf(" ");

printbox(x,y+2); printman(x,y+ 1);

ghouse[x][y+2]= ' b '; Break;

Default: break

}

}

/* Move the box on the vacant lot to the destination */

moveboxin(int x,int y,char a)

{

Switch (a)

{

case ' u ':ghouse[x- 1][y]= 0; printf(" ");

printboxin(x-2,y); printman(x- 1,y);

ghouse[x-2][y]= ' I '; Break;

Case "D": ghohouse [x+1] [y] = 0; printf(" ");

printboxin(x+2,y); printman(x+ 1,y);

ghouse[x+2][y]= ' I '; Break;

case ' l ':ghouse[x][y- 1]= 0; printf(" ");

printboxin(x,y-2); printman(x,y- 1);

ghouse[x][y-2]= ' I '; Break;

case ' r ':ghouse[x][y+ 1]= 0; printf(" ");

printboxin(x,y+2); printman(x,y+ 1);

ghouse[x][y+2]= ' I '; Break;

Default: break

}

}

/* Move the box on the target to the target */

moveinboxin(int x,int y,char a)

{

Switch (a)

{

case ' u ':ghouse[x- 1][y]= ' m '; printf(" ");

printboxin(x-2,y); printman(x- 1,y);

ghouse[x-2][y]= ' I '; Break;

case ' d ':ghouse[x+ 1][y]= ' m '; printf(" ");

printboxin(x+2,y); printman(x+ 1,y);

ghouse[x+2][y]= ' I '; Break;

case ' l ':ghouse[x][y- 1]= ' m '; printf(" ");

printboxin(x,y-2); printman(x,y- 1);

ghouse[x][y-2]= ' I '; Break;

case ' r ':ghouse[x][y+ 1]= ' m '; printf(" ");

printboxin(x,y+2); printman(x,y+ 1);

ghouse[x][y+2]= ' I '; Break;

Default: break

}

}

/* Determine the state at a specific coordinate */

int judge(int x,int y)

{

int I;

Switch (ghouse[x][y])

{

Case 0: I =1; Break;

Case "w": I = 0; Break;

Case "b": I = 2; Break;

Case "I": I = 4; Break;

Case "m": I = 3; Break;

Default: break

}

Return I;

}

/* Main function for handling character movement after pressing the keyboard */

move(int x,int y,char a)

{

Switch (a)

{

case 'u':if(! judge(x- 1,y)) {gotoxy(y,x); Break; }

else if(judge(x- 1,y)== 1||judge(x- 1,y)==3)

{if(judge(x,y)==3)

{ printwhither(x,y); printman(x- 1,y); Break; }

other

{ printf(" "); printman(x- 1,y); Break; }

}

else if(judge(x- 1,y)==2)

{ if(judge(x-2,y)== 1)

{movebox(x,y,' u ');

if(judge(x,y)= = 3)print white where(x,y); gotoxy(y,x- 1);

}

else if(judge(x-2,y)==3)

{ moveboxin(x,y,' u ');

if(judge(x,y)= = 3)print white where(x,y); gotoxy(y,x- 1);

}

else gotoxy(y,x);

Break;

}

else if(judge(x- 1,y)==4)

{ if(judge(x-2,y)== 1)

{moveinbox(x,y,' u ');

if(judge(x,y)= = 3)print white where(x,y); gotoxy(y,x- 1);

}

else if(judge(x-2,y)==3)

{ moveinboxin(x,y,' u ');

if(judge(x,y)= = 3)print white where(x,y); gotoxy(y,x- 1);

}

else gotoxy(y,x);

Break;

}

case 'd':if(! judge(x+ 1,y)) {gotoxy(y,x); Break; }

else if(judge(x+ 1,y)== 1||judge(x+ 1,y)==3)

{if(judge(x,y)==3)

{ printwhither(x,y); printman(x+ 1,y); Break; }

other

{ printf(" "); printman(x+ 1,y); Break; }

}

else if(judge(x+ 1,y)==2)

{ if(judge(x+2,y)== 1)

{movebox(x,y,' d ');

if(judge(x,y)= = 3)print white where(x,y); gotoxy(y,x+ 1);

}

else if(judge(x+2,y)==3)

{moveboxin(x,y,' d ');

if(judge(x,y)= = 3)print white where(x,y); gotoxy(y,x+ 1);

}

else gotoxy(y,x);

Break;

}

else if(judge(x+ 1,y)==4)

{ if(judge(x+2,y)== 1)

{moveinbox(x,y,' d ');

if(judge(x,y)= = 3)print white where(x,y); gotoxy(y,x+ 1);

}

else if(judge(x+2,y)==3)

{moveinboxin(x,y,' d ');

if(judge(x,y)= = 3)print white where(x,y); gotoxy(y,x+ 1);

}

else gotoxy(y,x);

Break;

}

case 'l':if(! judge(x,y- 1)) {gotoxy(y,x); Break; }

else if(judge(x,y- 1)== 1||judge(x,y- 1)==3)

{if(judge(x,y)==3)

{ printwhither(x,y); printman(x,y- 1); Break; }

other

{ printf(" "); printman(x,y- 1); Break; }

}

else if(judge(x,y- 1)==2)

{ if(judge(x,y-2)== 1)

{movebox(x,y,' l ');

if(judge(x,y)= = 3)print white where(x,y); gotoxy(y- 1,x);

}

else if(judge(x,y-2)==3)

{moveboxin(x,y,' l ');

if(judge(x,y)= = 3)print white where(x,y); gotoxy(y- 1,x);

}

else gotoxy(y,x);

Break;

}

else if(judge(x,y- 1)==4)

{ if(judge(x,y-2)== 1)

{moveinbox(x,y,' l ');

if(judge(x,y)= = 3)print white where(x,y); gotoxy(y- 1,x);

}

else if(judge(x,y-2)==3)

{moveinboxin(x,y,' l ');

if(judge(x,y)= = 3)print white where(x,y); gotoxy(y- 1,x);

}

else gotoxy(y,x);

Break;

}

case 'r':if(! judge(x,y+ 1)) {gotoxy(y,x); Break; }

else if(judge(x,y+ 1)== 1||judge(x,y+ 1)==3)

{if(judge(x,y)==3)

{printwhither(x,y); printman(x,y+ 1); Break; }

other

{ printf(" "); printman(x,y+ 1); Break; }

}

else if(judge(x,y+ 1)==2)

{ if(judge(x,y+2)== 1)

{movebox(x,y,' r ');

if(judge(x,y)= = 3)print white where(x,y); gotoxy(y+ 1,x);

}

else if(judge(x,y+2)==3)

{moveboxin(x,y,' r ');

if(judge(x,y)= = 3)print white where(x,y); gotoxy(y+ 1,x);

}

else gotoxy(y,x);

Break;

}

else if(judge(x,y+ 1)==4)

{ if(judge(x,y+2)== 1)

{moveinbox(x,y,' r ');

if(judge(x,y)= = 3)print white where(x,y); gotoxy(y+ 1,x);

}

else if(judge(x,y+2)==3)

{moveinboxin(x,y,' r ');

if(judge(x,y)= = 3)print white where(x,y); gotoxy(y+ 1,x);

}

else gotoxy(y,x);

Break;

}

Default: break

}

}

/* Press the space bar to return to the function at the beginning of this level */

Invalid reset (int i)

{

Switch (1)

{

Case 0: init ();

init house 1(); Break;

case 1:init();

init house 2(); Break;

Case 2: init ();

init house 3(); Break;

Case 3: init ();

init house 4(); Break;

Default: break

}

}

/* main function main */

void main()

{

int key,x,y,s,I = 0;

winer *win,* pw

_ AL = 3; _ AH = 0;

gen interrupt(0x 10);

init();

win = init house 1();

Do {

_ AH = 3;

gen interrupt(0x 10);

x = _ DH+ 1; y = _ DL+ 1;

while(BIOS key( 1)= = 0);

key = BIOS key(0);

Switch (key)

{

Case 0x4800:move(x, y,' u'); Break; /* After pressing the up key */

Case 0x5000:move(x, y,' d'); Break; /* After pressing the down key */

case 0x4b00:move(x,y,' l '); Break; /* After pressing the left button */

Case 0x4d00:move(x, y,' r'); Break; /* After pressing the right arrow */

Case 0x3920: Reset (1); Break; /* After pressing the space bar */

Default: break

}

s = 0;

pw = win

while(pw)

{

if(g house[pw-& gt; x][pw-& gt; y]= = ' m ')s++;

pw = pw-& gt; p;

}

If (s==0)

{

Free (win);

gotoxy(25,2);

Printf ("Congratulations! You did a good job! ”);

getch();

i++;

Switch (1)

{

case 1:init();

win = init house 2(); Break;

Case 2: init ();

win = init house 3(); Break;

Case 3: init ();

win = init house 4(); Break;

Case 4: gotoxy (15,21);

Printf ("My dear friend, you are so clever! Welcome to play again! ”);

key = 0x 0 1 1b; getch(); Break;

Default: break

}

}

}while (key! = 0x 0 1 1b);

_ AL = 3;

_ AH = 0;

gen interrupt(0x 10);

}