// The Tourist
// A game that represents a board as a 2D grid
// We have a tourist that starts at a certain position
// The tourist can then move to other locations on the grid
// Tourists score points for going places they haven't been before
// and lose points for returning to places they've already seen

#include <stdio.h>

#define WIDTH 8
#define HEIGHT 6

void displayGrid(int width, int height, int grid[width][height], int posX, int posY);
int calculateScore(int width, int height, int grid[width][height]);

int main (void) {
    int grid[WIDTH][HEIGHT] = {0};
    int posX = 0, posY = 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
        grid[posX][posY]++;

        // show the current status
        displayGrid(WIDTH, HEIGHT, grid, posX, posY);
        
        printf("Please enter a numpad direction or 0 to exit: ");
        int input;
        scanf("%d", &input);
        if (input == 4) {        // left
            posX--;
        } else if (input == 8) { // up
            posY--;
        } else if (input == 6) { // right
            posX++;
        } else if (input == 2) { // down
            posY++;
        } else if (input == 0) { // exit
            exit = 1;
        } else {                 // invalid
            printf("Input is not a numpad direction, please use 2,4,6 or 8\n");
        }
        
        // Check if we've walked off the map
        if (posX < 0) {
            posX = 0;
        } else if (posX >= WIDTH) {
            posX = WIDTH;
        }
        if (posY < 0) {
            posY = 0;
        } else if (posY >= HEIGHT) {
            posY = HEIGHT;
        }        
    }
    
    // Journey's over
    printf("Your journey ended. you scored %d tourist points.\n", 
                                           calculateScore(WIDTH, HEIGHT, grid));
    
    return 0;
}

// output the grid to the user
void displayGrid(int width, int height, int grid[width][height], int posX, int posY) {
    int y = 0;
    while (y < height) {
        int x = 0;
        while (x < width) {
            if (x == posX && y == posY) { // we're at the Tourist's location
                printf("T");
            } else { // print the number of times it's been visited
                printf("%d", grid[x][y]);
            }
            x++;
        }
        printf("\n");
        y++;
    }
    return;
}

// Calculate score
// The tourist likes to see new things . . . but if they see the same thing
// more than once, that's boring, so they lose points
int calculateScore(int width, int height, int grid[width][height]) {
    int score = 0;
    int y = 0;
    while (y < height) {
        int x = 0;
        while (x < width) {
            if (grid[x][y] == 1) { // only visited once
                score++;
            } else if (grid[x][y] > 1) { // visited too many times
                score--;
            }
            x++;
        }
        y++;
    }
    return score;
}

Resource created Wednesday 20 March 2019, 12:11:43 AM.

file: grid.c


Back to top

COMP1511 19T1 (Programming Fundamentals) is powered by WebCMS3
CRICOS Provider No. 00098G