Home > Back-end >  A C language homework
A C language homework

Time:12-30

#include
#include
#include
#include
25//# define Height the Height of the maze, must be an odd number of
25//# define Width the Width of the maze, must be an odd number of
# define Wall 1
# define Road 0
# define Start 2
# define End 3
# define Esc 5
# define Up 1
# define Down 2
# define Left 3
# define Right 4
Int map [Height + 2] [Width + 2];

Void gotoxy (int x, int y)//mobile coordinate
{
COORD COORD.
Coord. X=X;
Coord. Y=Y;
SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), coord);
}
Void hidden ()//hide the cursor
{
HANDLE hOut=GetStdHandle (STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci.
GetConsoleCursorInfo (hOut, & amp; Cci);
Cci. BVisible=0;//assign 1 to display, fu 0 as hidden
SetConsoleCursorInfo (hOut, & amp; Cci);
}
Void the create (int x, int y)//random maze
{
Int c [4] [2]={
0, 1, 1, 0,
0, 1, 1, 0
};//four directions
int i;
int j;
Int t;//to disrupt direction
for (i=0; I & lt; 4. I++)
{
J=rand () % 4;
T=c [0] [I];
[I] [0] c=c [j] [0];
C [j] [0]=t;
T=c [I] [1];
[I] [1] c=c [j] [1];
C [j] [1]=t;
}
The map [x] [y]=Road;
for (i=0; I & lt; 4. I++)
If (map [x + 2 * c [I] [0]] [[I] y + 2 * c [1]]==Wall)
{
The map [x + c [I] [0]] [[I] y + c [1]]=Road;
Create (x + 2 * c [0], [I] y + 2 * c [I] [1]).
}
}
Int get_key ()//receive keys
{
char c;
While (c=getch ())
{
If (c==27)
Return the Esc;//Esc
if (c !=- 32)
continue;
C=getch ();
If (c==72)
Return Up;//
If (c==80)
Return the Down; Under the//
If (c==75)
Return the Left;//left
If (c==77)
Return the Right;//right
}
return 0;
}
Void paint (int x, int y)//draw a maze
{
Gotoxy (2 * 2, y x - 1);
The switch (map [x] [y])
{
In case the Start:
Printf (" ");
break;//draw entry
Case End:
Printf (" out ");
break;//draw export
Case Wall:
Printf (" wall ");
break;//HuaQiang
Case Road:
Printf (" ");
break;//HuaLu
}
}
Void game ()
{
Int x=2;
Int y=1;//current player position, beginning at the entrance
Int c;//used to receive keys
While (1)
{
Gotoxy (2 * 2, y x - 1);
Printf (" O ");//draw the player's current position
If (map [x] [y]==End)//determine whether to export
{
Gotoxy (30, 24);
Printf (" congratulations to destination, \ n ");
Printf (" press any key to end..." );
getch();
break;
}
C=get_key ();
If (c==Esc)
{
Gotoxy (0, 24);
break;
}
The switch (c)
{
Case
Up://going Up
If (map [1] x [y]!=Wall)
{
Paint (x, y);
X -;
}
break;
Case
Go Down://Down
If (map [x + 1] [y]!=Wall)
{
Paint (x, y);
X++;
}
break;
Case
Left://Left
If (map [x] [1] y!=Wall)
{
Paint (x, y);
Y -;
}
break;
Case
Right/left/Right:
If (map [x] [y + 1)!=Wall)
{
Paint (x, y);
Y++;
}
break;
}
}
}
Int main ()
{
System (the title "yourname");
int i;
int j;
srand((unsigned)time(NULL));//initialize then seed
Hidden ();//hide the cursor
for (i=0; I & lt;=Height + 1; I++)
{
For (j=0; J & lt;=Width + 1; J++)
{
If (I==0 | | I==Height + 1 | | j==0 | | j==Width + 1)//initializes the maze
The map [I] [j]=Road;
The else
The map [I] [j]=Wall;
}
}
Create (2 * (rand () % (Height/2) + 1), 2 * (rand () % (Width/2) + 1));//from one point to start generating random maze, which ranks for the even
for (i=0; I & lt;=Height + 1;
i++)//boundary treatment,{
The map [I] [0]=Wall;
The map [I] [Width + 1]=Wall;
}

For (j=0; J & lt;=Width + 1;
j++)//boundary treatment,{
The map [0] [j]=Wall;
The map [Height + 1] [j]=Wall;
}
The map [2] [1]=Start;//given entry
Map [Height - 1)/Width=End;//given export
For (I=1; I & lt;=Height; I++)
{
For (j=1; J & lt;=Width; J++)//draw a maze
{
Paint (I, j);
}
}
Game ();//start the game
getch();
return 0;
}
  • Related