← Back to Demo

Project 003 — Code

C • Grid rendering • Simple pathfinding (coursework)

Key Implemented Functions

The assignment provided skeleton code. I implemented the required functions for map rendering, map creation, and the Level 2/3 pathfinding behaviours.

Snippet: Map printing (Level 1)

void PrintMap(char MAP[][N]) {
    int i, j;
    for(i = 0; i < N; i += 1){
        for(j = 0; j < N; j += 1){
            printf("[%c]", MAP[i][j]);
        }
        printf("\n");
    }
}

Prints the grid in the required format.

Snippet: Input parsing + placing S/E/X (Level 1)

void FillMap(char MAP[][N], int *startRow, int *startColumn, int *endRow,
             int *endColumn) {
    int x_st, y_st;
    scanf("%d %d\n", &x_st, &y_st);
    *startRow = x_st;
    *startColumn = y_st;

    int x_ed, y_ed;
    scanf("%d %d\n", &x_ed, &y_ed);
    *endRow = x_ed;
    *endColumn = y_ed;

    int numOBS = 0;
    scanf("%d/n", &numOBS);

    for(int i = 0; i < numOBS; i++){
        int x, y;
        scanf("%d %d\n", &x, &y);
        MAP[x][y] = 'X';
    }

    MAP[x_st][y_st] = 'S';
    MAP[x_ed][y_ed] = 'E';
}

Reads input and builds the initial map state.

Snippet: SimpleDirections (Level 2)

Level 2 uses a simple strategy (vertical movement then horizontal). The implementation marks the path with '+' and reports when the algorithm gets stuck.

// simplified excerpt
while(cur_x != endRow){
    // stuck check (example)
    if(MAP[cur_x + 1][cur_y] == 'X' && MAP[cur_x][cur_y +1] == 'X'){
        printf("SimpleDirections took %d steps and got stuck.\n", tl_steps);
        tl_steps += STUCK_MARK;
        return tl_steps;
    }
    // movement logic + obstacle handling ...
}

Snippet: ClosestFreeNeighbour (Level 3)

Level 3 follows a fixed priority order when choosing the next free neighbour (Up → Right → Down → Left), marking visited cells with '+'.

// simplified excerpt
if(currentRow > 0 && MAP[currentRow - 1][currentColumn] == EMPTY_SPACE){
    MAP[currentRow - 1][currentColumn] = '+';
    currentRow -= 1;
    continue;
}
// then try Right, then Down, then Left ...

Reflection: Limitations & Improvements

I also included written answers discussing cases where the simple strategies fail (e.g., traps, lack of backtracking), and proposed a “turning back” idea with pseudocode to make the neighbour method smarter.

Download and View Full Source (C)