// A heavily modified version of paint.c for Assignment 1
// This is starter code for the Tourist Program
// This program was written by Marc Chee (marc.chee@unsw.edu.au)
// in June 2019
//
#include <stdio.h>
// The dimensions of the map
#define N_ROWS 10
#define N_COLS 10
// Helper Function: Print out the canvas.
void printMap(int map[N_ROWS][N_COLS], int posR, int posC);
int main(void) {
int map[N_ROWS][N_COLS] = {0};
int posR = 0, posC = 0;
// loop and let the user control the Tourist's movement
int exit = 0;
while (!exit) {
// mark the location as having been visited by incrementing
map[posR][posC] = 1;
// show the current status
printMap(map, posR, posC);
printf("Please enter a numpad direction or 0 to exit: ");
int input;
scanf("%d", &input);
if (input == 4) {
posC--;
} else if (input == 8) {
posR--;
} else if (input == 6) {
posC++;
} else if (input == 2) {
posR++;
} else if (input == 0) {
exit = 1;
} else {
printf("Input is not a numpad direction, please use 2,4,6 or 8\n");
}
// Check if we've walked off the map
if (posR < 0) {
posR = 0;
} else if (posR >= N_ROWS) {
posR = N_ROWS - 1;
}
if (posC < 0) {
posC = 0;
} else if (posC >= N_COLS) {
posC = N_COLS - 1;
}
// Check if we've been here before
if (map[posR][posC] == 1) {
printf("We've already been here! How boring!\n");
exit = 1;
}
}
return 0;
}
// Prints the map, by printing the integer value stored in
// each element of the 2-dimensional map array.
// Prints a T instead at the position posR, posC
void printMap(int map[N_ROWS][N_COLS], int posR, int posC) {
int row = 0;
while (row < N_ROWS) {
int col = 0;
while (col < N_COLS) {
if (posR == row && posC == col) {
printf("T ");
} else {
printf("%d ", map[row][col]);
}
col++;
}
row++;
printf("\n");
}
}
Resource created Tuesday 25 June 2019, 12:46:14 AM, last modified Tuesday 25 June 2019, 06:47:06 PM.
file: tourist.c