Code:
#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);
}