Code:
#include <stdio.h>
#include <string.h>
void setMap(int map[100][100], int num_row, int num_col); /*prototype*/
void printMap(int map[100][100], int num_row, int num_col);
void playGame(int map[100][100], int num_row, int num_col);
main()
{
int map[100][100]; /*variable declaraion*/
char name[50];
int num_row, num_col, choice;
printf("Enter name: "); /*input name*/
scanf("%s", name);
printf("Hello %s!\n", name);
do
{
printf("[1] Game Start\n[2] Logout\n"); /*menu*/
printf("Enter choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: do /*loop until num_row>=5*/
{
printf("Enter number of rows(greater than or equal to 5)): ");
scanf("%d", &num_row);
if(num_row<5)
{
printf("Must be greater than or equal to 5\n");
}
} while(num_row<5);
do /*loop until num_col>=5*/
{
printf("Enter number of columns(greater than or equal to 5)): ");
scanf("%d", &num_col);
if(num_col<5)
{
printf("Must be greater than of equal to 5\n");
}
} while(num_col<5);
setMap(map, num_row, num_col); /*credits to user 'dmh2000' of Cboard for pointing the error here*/
printMap(map, num_row, num_col);
playGame(map, num_row, num_col);
break;
case 2: printf("Bye!\n");
break;
default: printf("Error! Choice invalid!\n");
}
} while(choice!=2);
}
void setMap(int map[100][100], int num_row, int num_col)
{
int i, j;
for(i=0; i<1; i++) /*border on first row*/
{
for(j=0; j<num_col; j++)
{
map[i][j]=8;
}
}
for(j=0; j<1; j++) /*border on first column*/
{
for(i=0; i<num_row; i++)
{
map[i][j]=8;
}
}
for(i=num_row-1; i<num_row; i++) /*border on last row*/
{
for(j=0; j<num_col; j++)
{
map[i][j]=8;
}
}
for(j=num_col-1; j<num_col; j++) /*border on last column*/
{
for(i=0; i<num_row; i++)
{
map[i][j]=8;
}
}
for(i=0; i<num_row; i++) /*set 0 to walkable path*/
{
for(j=0; j<num_col; j++)
{
if(map[i][j]!=8)
{
map[i][j]=0;
}
}
}
}
void printMap(int map[100][100], int num_row, int num_col)
{
int i, j;
for (i=0; i<num_row; i++) /*print map*/
{
for(j=0; j<num_col; j++)
{
printf("%d ", map[i][j]);
}
putchar('\n');
}
}
void playGame(int map[100][100], int num_row, int num_col)
{
int i, j, a, b, choice2; /*variable declaration*/
char direction[10];
do /*loop if coordinates invalid*/
{
printf("Plant the character (row col): "); /*starting point*/
scanf("%d %d", &a, &b);
if(a==0 || a==num_row-1 || a<0 || a>num_row-1 || b==0 || b<0 || b>num_col-1 || b==num_col-1) /*borders and coordinates not in map*/
{
printf("You cannot plant your character there.\n");
}
else
{
for (i=0; i<num_row; i++)
{
for(j=0; j<num_col; j++)
{
if(i==a && j==b) /*plant character in (a,b)*/
{
map[i][j]=1;
}
printf("%d ", map[i][j]);
}
putchar('\n');
}
}
} while(a==0 || a==num_row-1 || a<0 || a>num_row-1 || b==0 || b<0 || b>num_col-1 || b==num_col-1);
do
{
printf("[1] Move Player\n[2] Sign Out\n"); /*menu*/
printf("Enter choice: ");
scanf("%d", &choice2);
switch(choice2)
{
case 1: printf("Where do you want to go (up, down, left, right)? ");
scanf("%s", direction);
if(strcmp(direction, "up")==0) /*check for input*/
{
if(a==1) /*border*/
{
printf("Obstacle detected. Can't move up.\n");
}
else
{
for (i=0; i<num_row; i++)
{
for(j=0; j<num_col; j++)
{
if(i==a-1 && j==b) /*set 1 to new coordinate*/
{
map[i][j]=1;
}
map[a][b]=0; /*reset (a,b) to 0*/
printf("%d ", map[i][j]);
}
putchar('\n');
}
a--; /*set a to the new value of a for next move*/
}
}
else if(strcmp(direction, "down")==0) /*check for input*/
{
if(a==num_row-2) /*border*/
{
printf("Obstacle detected. Can't move down.\n");
}
else
{
for (i=0; i<num_row; i++)
{
for(j=0; j<num_col; j++)
{
if(i==a+1 && j==b) /*set 1 to new coordinate*/
{
map[i][j]=1;
}
map[a][b]=0; /*reset (a,b) to 0*/
printf("%d ", map[i][j]);
}
putchar('\n');
}
a++; /*set a to the new value of a for next move*/
}
}
else if(strcmp(direction, "left")==0) /*check for input*/
{
if(b==1) /*border*/
{
printf("Obstacle detected. Can't move left.\n");
}
else
{
for (i=0; i<num_row; i++)
{
for(j=0; j<num_col; j++)
{
if(i==a && j==b-1) /*set 1 to new coordinate*/
{
map[i][j]=1;
}
map[a][b]=0; /*reset (a,b) to 0*/
printf("%d ", map[i][j]);
}
putchar('\n');
}
b--; /*set b to the new value of a for next move*/
}
}
else if(strcmp(direction, "right")==0) /*check for input*/
{
if(b==num_col-2)
{
printf("Obstacle detected. Can't move right.\n");
}
else
{
for (i=0; i<num_row; i++)
{
for(j=0; j<num_col; j++)
{
if(i==a && j==b+1) /*set 1 to new coordinate*/
{
map[i][j]=1;
}
map[a][b]=0; /*reset (a,b)*/
printf("%d ", map[i][j]);
}
putchar('\n');
}
b++; /*set b to the new value of b for next move*/
}
}
break;
case 2: printf("Bye!\n");
break;
default: printf("Error! Invalid choice!\n");
}
} while(choice2!=2);
}