// A heavily modified version of the starter code 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
//

// TODO: The Tourist can still walk off the map in this version.
// Can you figure out a way to detect if the tourist is out of
// bounds of the map and put them back on the map somewhere?

#include <stdio.h>

// The dimensions of the map
#define N_ROWS 10
#define N_COLS 10

// Helper Function: Print out the map as a 2D grid
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;
    int posC = 0;
    
    printMap(map, posR, posC);
    
    // main game loop, take input and print the map each time
    int exitLoop = 0;
    while (!exitLoop) {
        map[posR][posC]++;
        
        printf("Please enter a direction using the numpad: ");
        int input = 0;
        scanf("%d", &input);
        
        if (input == 8) { // up
            posR--;
        } else if (input == 2) { // down
            posR++;
        } else if (input == 4) { // left
            posC--;
        } else if (input == 6) { // right
            posC++;
        } else if (input == 0) { // exit
            exitLoop = 1;
        }
        
        if (map[posR][posC] > 0) { // we've been here before
            exitLoop = 1;
            printf("I've seen this before . . . how boring!\n");
        }
        
        printMap(map, posR, posC);
    }
    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 23 June 2020, 12:01:24 PM.

file: tourist.c


Back to top

COMP1511 20T2 (Programming Fundamentals) is powered by WebCMS3
CRICOS Provider No. 00098G