PDA

View Full Version : Maze game, complete with maze editor and an example maze!



Brian
01-19-2002, 10:36 AM
attached file contains:
maze game. (maze.exe)
maze editor. (maze-edit.exe)
example level. (example)

the source code is embarrasingly messy so I won't publish it for now :/.

Brian
01-19-2002, 10:37 AM
lol forgot to attach the file.
Here it is.

Brian
01-20-2002, 03:27 AM
Okay, here's the source code:

maze.exe


#include <errno.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>


extern int errno;

int main( void )
{



int data[21][21];

char mazename[BUFSIZ];
int buf[442];
FILE *fptr;
int i;
time_t cstart, cend;
float temp;


int k = 0;
int l = 0;
size_t number_of_elements;
size_t size_each;
int j = 0;
/*
* Write data to disk
*/

clrscr();

/*
* Now let's demonstrate grabbing that data
*/
size_each = 4; /* size of each element */
number_of_elements = 441;
printf("Welcome to SimpleMaze 0.04 by Brian.\nUse the arrow keys to guide the \"x\" though the maze\nPress Q to quit the game.");
printf("\nEnter the name of the maze to open: ");
scanf("%s",&mazename);
clrscr();
_setcursortype(_NOCURSOR);
fptr = fopen( mazename, "r" );

if ( !fptr )
{
perror( "fopen()" );
return (errno);
}


fread( buf, size_each, number_of_elements, fptr );

fclose( fptr );
for ( i=0; i < number_of_elements; i++ )
{
if(buf[i] == 1)
{
textbackground(WHITE);
cprintf(" ");
textbackground(BLACK);
}
else if(buf[i] == 0)
{
textbackground(BLACK);
cprintf(" ");

}
else if(buf[i] == 2)
{
textbackground(GREEN);
cprintf(" ");

}
else if(buf[i] == 3)
{
textbackground(RED);
cprintf(" ");

}
// printf( "%d", buf[i] );
data[k][l] = buf[i];
j++;
k++;
if(j == 21)
{
textbackground(BLACK);

cprintf("\n\r");
j=0;
}
if(k == 21)
{
l++;
k=0;
}
}


j=0;
k=0;
l=0;
do
{

if(data[j][k] == 2)
{
printf("%d %d %d",data[j][k],j,k);
}
else
{
k++;
if(k == 21)
{
printf("\n");
j++;
k=0;
}
}
}
while(data[j][k] != 2);
gotoxy(j+1,k+1);
printf("x");
gotoxy(1,22);
printf("Press any key to begin");
getch();
gotoxy(1,22);
printf(" ");
gotoxy(j+1,k+1);
cstart=clock();
do
{
l = getch();
if(l==75) // left
{
if(data[j-1][k] != 1 && j > 0)
{
printf("\b ");
j--;
gotoxy(j+1,k+1);
printf("x");
}
else
{
printf("\a");
}
}
else if(l==77) // right
{
if(data[j+1][k] != 1 && j < 20)
{
printf("\b ");
j++;
gotoxy(j+1,k+1);
printf("x");
}
else
{
printf("\a");
}
}
else if(l==72) // up
{
if(data[j][k-1] != 1 && k > 0)
{
printf("\b ");
k--;
gotoxy(j+1,k+1);
printf("x");
}
else
{
printf("\a");
}
}
if(l==80) // down
{
if(data[j][k+1] != 1 && k < 20)
{
printf("\b ");
k++;
gotoxy(j+1,k+1);
printf("x");
}
else
{
printf("\a");
}
}
else if(l==113) // q
{
clrscr();
return 0;
}
else
{

}
}
while(data[j][k] != 3);
gotoxy(1,23);
cend=clock();
if(cend!=cstart){
temp=((double)cend-cstart)/CLK_TCK;

}

printf("YOU FOUND THE EXIT IN %-5.2f SECONDS!\nPress any key to quit.",temp);
getch();
return 0;
}


urg, you think that looks bad?
well here's the editor:


#include <errno.h>
#include <stdio.h>
#include <conio.h>

extern int errno;

int main( void )
{
FILE *fptr;
int buf[442];
int m=0;
int n=0;
int o=0;

size_t number_of_elements = 441;
size_t size_each = 4;
int data[21][21] = { 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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 };

int i;
int j=0;
int k=0;
int l=0;
char loadWhat[BUFSIZ];

char outputfile[BUFSIZ];
/*
* Write data to disk
*/
clrscr();
_setcursortype(_NOCURSOR);
do
{
if(data[j][k] == 0)
{
textbackground(BLACK);
textcolor(WHITE);
cprintf(" ");
}
else if(data[j][k] == 1)
{
textbackground(WHITE);
textcolor(BLACK);
cprintf(" ");
textcolor(WHITE);
textbackground(BLACK);
}
else if(data[j][k] == 2)
{
textbackground(GREEN);
cprintf(" ");
textbackground(BLACK);
}
else if(data[j][k] == 3)
{
textbackground(RED);
cprintf(" ");
textbackground(BLACK);
}

k++;
l++;
if(k == 21)
{
k=0;
j++;
cprintf("\n\r");
}
}
while(l != 441);
printf("\n(Arrow Keys) Move cursor. (1) Wall. (2) Entrance, (3) Exit, (0) Empty\n(Q) Quit. (S) Save (L) Load");
j=0;
k=0;
gotoxy(j+1,k+1);
printf("x");
do
{
l = getch();

if(l==75) // left
{
if(j > 0)
{
printf("\b ");
j--;
gotoxy(j+1,k+1);
printf("x");
}
else
{
printf("\a");
}
}
else if(l == 77) // right
{
if(j < 20)
{
printf("\b ");
j++;
gotoxy(j+1,k+1);
printf("x");
}
else
{
printf("\a");
}
}
else if(l == 72) // up
{
if(k>0)
{
printf("\b ");
k--;
gotoxy(j+1,k+1);
printf("x");
}
else
{
printf("\a");
}
}
else if(l == 80) // down
{
if(k<20)
{
printf("\b ");
k++;
gotoxy(j+1,k+1);
printf("x");
}
else
{
printf("\a");
}
}
else if(l == 113)
{
clrscr();
return 0;
}
else if(l == 49) // 1
{
printf("\b");
textbackground(WHITE);
textcolor(BLACK);
cprintf(" ");
textcolor(WHITE);
textbackground(BLACK);
data[k][j] = 1;
}
else if(l == 50) // 2
{
printf("\b");
textbackground(GREEN);
cprintf(" ");
textbackground(BLACK);
data[k][j] = 2;
}
else if(l == 51) // 3
{
printf("\b");
textbackground(RED);
cprintf(" ");
textbackground(BLACK);
data[k][j] = 3;
}
else if(l == 48) // 0
{
printf("\b");
textbackground(BLACK);
cprintf(" ");
textbackground(BLACK);
data[k][j] = 0;
}
else if(l == 108) // l
{
clrscr();
printf("Enter name of file to load: ");
scanf("%s", &loadWhat);
fptr = fopen( loadWhat, "r" );

if ( !fptr )
{
printf("LOADING FAILED\n");
return -1;
}
clrscr();
fread( buf, size_each, number_of_elements, fptr );

do
{
data[m][n] = buf[o];
n++;
o++;
if(n == 21)
{
m++;
n=0;
}
}
while(o != 441);
l = 0;
j = 0;
k = 0;
do
{
if(data[j][k] == 0)
{
textbackground(BLACK);
textcolor(WHITE);
cprintf(" ");
}
else if(data[j][k] == 1)
{
textbackground(WHITE);
textcolor(BLACK);
cprintf(" ");
textcolor(WHITE);
textbackground(BLACK);
}
else if(data[j][k] == 2)
{
textbackground(GREEN);
cprintf(" ");
textbackground(BLACK);
}
else if(data[j][k] == 3)
{
textbackground(RED);
cprintf(" ");
textbackground(BLACK);
}

k++;
l++;
if(k == 21)
{
k=0;
j++;
cprintf("\n\r");
}
}
while(l != 441);
printf("\n(Arrow Keys) Move cursor. (1) Wall. (2) Entrance, (3) Exit, (0) Empty\n(Q) Quit. (S) Save (L) Load");
j=0;
k=0;
gotoxy(j+1,k+1);
}
}
while(l != 115);
clrscr();
printf("save as?\n");
scanf("%s",&outputfile);
fptr = fopen( outputfile, "w" ); /* the 'b' has no meaning in POSIX */
if ( !fptr )
{
perror( "fopen()" );
return (errno);
}

size_each = sizeof( int ); /* size of each element */
number_of_elements = sizeof( data ) / size_each; /* number of these elements we're sending to file */

fwrite( data, size_each, number_of_elements, fptr );

fclose( fptr );



return (0);
}