// 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
#define EXPLORED 1
#define UNEXPLORED 0
// Helper Function: Print out the map as a 2D grid
void printMap(int map[N_ROWS][N_COLS], int posR, int posC);
int countScore(int map[N_ROWS][N_COLS]);
int main(void) {
int map[N_ROWS][N_COLS] = {UNEXPLORED};
int touristR = N_ROWS/2;
int touristC = N_COLS/2;
printMap(map, touristR, touristC);
int keepRunning = 1;
while (keepRunning) {
// mark where we've been already
map[touristR][touristC] = 1;
int input = -1;
scanf("%d", &input);
if (input == 8) {
// up
touristR--;
} else if (input == 2) {
// down
touristR++;
} else if (input == 4) {
// left
touristC--;
} else if (input == 6) {
// right
touristC++;
} else if (input == 0) {
keepRunning = 0;
}
// Stop the tourist from walking off the edge
if (touristR < 0) {
// off the top
touristR = 0;
} else if (touristR >= N_ROWS) {
// off the bottom
touristR = N_ROWS - 1;
} else if (touristC < 0) {
// off the left
touristC = 0;
} else if (touristC >= N_COLS) {
// off the right
touristC = N_COLS - 1;
}
printMap(map, touristR, touristC);
// have I been here before?
if (map[touristR][touristC] == EXPLORED) {
printf("I've been here before . . . how boring!\n");
keepRunning = 0;
}
}
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");
}
}
// Possible Challenge:
// Write a function that gives the tourist a score
// by counting the number of places they visited after
// they stopped travelling
int countScore(int map[N_ROWS][N_COLS]) {
int score = 0;
// TODO: Calculate the score here
return score;
}
Resource created Monday 19 October 2020, 05:56:58 PM.
file: tourist.c