Code:
/* Written by datainjector
Maze generator and game */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
/*#include <conio.c> use only if needed for getch() */
#define ROW 20
#define COL 75
void creatmaze (int [][COL], int *, int * );
void printarea(int [][COL]);
void movement( int [][COL], int , int *, int * );
void gotoxy ( int , int );
void kill_cursor( void );
int main(void)
{
int maze[ROW][COL]={0};
int col=1, row=0, trackend, trackstart;
srand(time(0));
kill_cursor();
creatmaze(maze, &trackend, &trackstart);
printarea(maze);
while ( maze[trackend][(COL-2)] == -1 ){
movement ( maze, tolower(getch()), &trackstart, &col );
}
system("cls");
printf("You won\nTo lay a different mare just run the program again...\n");
system("pause");
return 0;
}
void creatmaze ( int maze[][COL] , int *trackend, int *trackstart)
{
int cnt_main, cnt_sub;
int row, col, tf;
int rowt, colt;
rowt = 0, colt = 0;
row = 2, col = 1;
for ( cnt_main = 0; cnt_main <= COL-1; cnt_main+=COL-1 ){
for ( cnt_sub = 0; cnt_sub <= ROW-1; cnt_sub++ )
maze[cnt_sub][cnt_main] = 2;
}
for ( cnt_main = 0; cnt_main <= ROW-1; cnt_main+=ROW-1 ){
for ( cnt_sub = 0; cnt_sub <= COL-1; cnt_sub++ )
maze[cnt_main][cnt_sub] = 2;
}
for ( cnt_main = 1; cnt_main <= ROW-2; cnt_main++ ){
for ( cnt_sub = 1; cnt_sub <= COL-2; cnt_sub++ ){
maze[cnt_main][cnt_sub] = 1;
}
}
row = 1 + rand() % 19;
*trackstart = row;
tf = 1;
maze[row][1] = -2;
cnt_main = 0;
while ( col < COL-2 ){
if ( tf == 1 ){
if ( maze[row][col+=1] == 2){
col-=1;
}
else{
maze[row][col] = 0;
}
}
else if ( tf == 2 ){
if ( maze[row-=1][col] == 2 ){
row+=1;
}
else{
maze[row][col] = 0;
}
}
else{
if ( maze[row+=1][col] == 2 ){
row-=1;
}
else
maze[row][col] = 0;
}
tf = 1 + rand() /(RAND_MAX / 3 );
}
*trackend = row;
cnt_main = 0;
while ( ++cnt_main != 1000 ){
/*rowt = 1 + rand() / (RAND_MAX / (ROW-2)); this creats an easier maze i guess
colt = 1 + rand() / (RAND_MAX /(COL-2)); */
rowt = 1 + rand() % (ROW-2);
colt = 1 + rand() % (COL-2);
maze[rowt][colt] = 0;
}
maze[*trackstart][1] = -2;
maze[*trackend][(COL-2)] = -1;
}
void movement ( int array[ROW][COL], int choice, int *row, int *col )
{
int store;
switch(choice){
case 'w' :
store = array[--(*row)][(*col)];
if ( store != 2 && store != 1 ){
array[(*row)][*col] = -2;
gotoxy(*col,*row);
putchar(2);
array[++(*row)][*(col)] = 0;
gotoxy(*col,*row);
putchar(255);
--(*row);
}
else{
array[++(*row)][*col] = -2;
gotoxy(*col,*row);
putchar(2);
}
break;
case 's' :
store = array[++(*row)][(*col)];
if ( store != 2 && store != 1 ){
array[*row][*col] = -2;
gotoxy(*col,*row);
putchar(2);
array[--(*row)][(*col)] = 0;
gotoxy(*col,*row);
putchar(255);
++(*row);
}
else{
array[--(*row)][*col] = -2;
gotoxy(*col,*row);
putchar(2);
}
break;
case 'a' :
store = array[(*row)][--(*col)];
if ( store != 2 && store != 1 ){
array[*row][*col] = -2;
gotoxy(*col,*row);
putchar(2);
array[*row][++(*col)] = 0;
gotoxy(*col,*row);
putchar(255);
--(*col);
}
else{
array[*row][++(*col)] = -2;
gotoxy(*col,*row);
putchar(2);
}
break;
case 'd' :
store = array[(*row)][++(*col)];
if ( store != 2 && store != 1 ){
array[*row][*col] = -2;
gotoxy(*col,*row);
putchar(2);
array[*row][--(*col)] = 0;
gotoxy(*col,*row);
putchar(255);
++(*col);
}
else{
array[*row][--(*col)] = -2;
gotoxy(*col,*row);
putchar(2);
}
break;
}
}
void printarea(int maze[][COL])
{
int cnt_main, cnt_sub;
for ( cnt_main = 0; cnt_main < ROW; cnt_main++ ){
for ( cnt_sub = 0; cnt_sub < COL; cnt_sub++ ){
if ( maze[cnt_main][cnt_sub] == 2 )
putchar(176);
else if ( maze[cnt_main][cnt_sub] == 1 )
putchar(178);
else if ( maze[cnt_main][cnt_sub] == 0 )
putchar(255);
else if ( maze[cnt_main][cnt_sub] == -2 )
putchar(2);
else
putchar(6);
}
printf("\n");
}
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void kill_cursor( void )
{
CONSOLE_CURSOR_INFO cci;
cci.dwSize = 1;
cci.bVisible = FALSE;
SetConsoleCursorInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &cci );
}
Please do give some feedback ...Cheers