Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implement a Keyboard-Controlled Console Maze Game in C

Tech 1

Required Header Files

#include <stdio.h>
#include <getch.h>
#include <stdlib.h>
#include <time.h>
  • stdio.h: Proivdes standard input and output functions for rendering the maze and printing status messages.
  • getch.h: Offers unbuffered, non-echoing single character input to detect arrow key presses with out requiirng the user to press enter.
  • stdlib.h: Supplies system call functions like system("clear") to refresh the terminal screen between game frames.
  • time.h: Used to track elapsed play time from game start too completion.

Maze Grid Initialization

The game uses a 10x10 2D character array to represent the maze layout:

char labyrinth[10][10] = {
    {'#','#','#','#','#','#','#','#','#','#'},
    {'#','@','#','#','#','#','#','#',' ','#'},
    {'#',' ','#',' ',' ',' ',' ',' ',' ','#'},
    {'#',' ','#',' ','#',' ','#','#',' ','#'},
    {'#',' ',' ',' ','#','#',' ','#',' ','#'},
    {'#',' ','#','#','#',' ',' ',' ',' ','#'},
    {'#',' ','#',' ','#',' ','#','#','#','#'},
    {'#',' ','#',' ','#',' ',' ',' ',' ',' '},
    {'#',' ',' ',' ','#','#','#','#','#','#'},
    {'#','#','#','#','#','#','#','#','#','#'}
};

Grid character definitions:

  • #: Impassable wall
  • (whitespace): Traversable path
  • @: Player character avatar

Game State Variables

int player_x = 1, player_y = 1;
time_t game_start_ts = time(NULL);
  • player_x and player_y: Store the current coordinates of the player character, initialized to the maze entrance at (1,1).
  • game_start_ts: Records the Unix timestamp when the game starts, used to calcluate total play time on completion.

Core Game Loop

The infinite loop runs until the player reaches the maze exit:

while(1) {
    system("clear");
    for(int row = 0; row < 10; row++) {
        for(int col = 0; col < 10; col++) {
            printf("%c ", labyrinth[row][col]);
        }
        putchar('\n');
    }

    if(player_x == 7 && player_y == 9) {
        printf("Maze completed! Total play time: %u seconds\n", (unsigned int)(time(NULL) - game_start_ts));
        break;
    }

    switch(getch()) {
        case 183:
            if(player_x > 0 && labyrinth[player_x - 1][player_y] == ' ') {
                labyrinth[player_x][player_y] = ' ';
                labyrinth[--player_x][player_y] = '@';
            }
            break;
        case 184:
            if(player_x < 9 && labyrinth[player_x + 1][player_y] == ' ') {
                labyrinth[player_x][player_y] = ' ';
                labyrinth[++player_x][player_y] = '@';
            }
            break;
        case 185:
            if(player_y < 9 && labyrinth[player_x][player_y + 1] == ' ') {
                labyrinth[player_x][player_y] = ' ';
                labyrinth[player_x][++player_y] = '@';
            }
            break;
        case 186:
            if(player_y > 0 && labyrinth[player_x][player_y - 1] == ' ') {
                labyrinth[player_x][player_y] = ' ';
                labyrinth[player_x][--player_y] = '@';
            }
            break;
    }
}

The loop first clears and redraws the maze every frame, then checks for the win condition. It then reads user input to move the player, only allowing movement into empty path tiles and blocking movement through walls or out of the maze bounds.

Full Compilable Code

#include <stdio.h>
#include <getch.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, const char* argv[]) {
    char labyrinth[10][10] = {
        {'#','#','#','#','#','#','#','#','#','#'},
        {'#','@','#','#','#','#','#','#',' ','#'},
        {'#',' ','#',' ',' ',' ',' ',' ',' ','#'},
        {'#',' ','#',' ','#',' ','#','#',' ','#'},
        {'#',' ',' ',' ','#','#',' ','#',' ','#'},
        {'#',' ','#','#','#',' ',' ',' ',' ','#'},
        {'#',' ','#',' ','#',' ','#','#','#','#'},
        {'#',' ','#',' ','#',' ',' ',' ',' ',' '},
        {'#',' ',' ',' ','#','#','#','#','#','#'},
        {'#','#','#','#','#','#','#','#','#','#'}
    };
    int player_x = 1, player_y = 1;
    time_t game_start_ts = time(NULL);

    while(1) {
        system("clear");
        for(int row = 0; row < 10; row++) {
            for(int col = 0; col < 10; col++) {
                printf("%c ", labyrinth[row][col]);
            }
            putchar('\n');
        }

        if(player_x == 7 && player_y == 9) {
            printf("Maze completed! Total play time: %u seconds\n", (unsigned int)(time(NULL) - game_start_ts));
            break;
        }

        switch(getch()) {
            case 183:
                if(player_x > 0 && labyrinth[player_x - 1][player_y] == ' ') {
                    labyrinth[player_x][player_y] = ' ';
                    labyrinth[--player_x][player_y] = '@';
                }
                break;
            case 184:
                if(player_x < 9 && labyrinth[player_x + 1][player_y] == ' ') {
                    labyrinth[player_x][player_y] = ' ';
                    labyrinth[++player_x][player_y] = '@';
                }
                break;
            case 185:
                if(player_y < 9 && labyrinth[player_x][player_y + 1] == ' ') {
                    labyrinth[player_x][player_y] = ' ';
                    labyrinth[player_x][++player_y] = '@';
                }
                break;
            case 186:
                if(player_y > 0 && labyrinth[player_x][player_y - 1] == ' ') {
                    labyrinth[player_x][player_y] = ' ';
                    labyrinth[player_x][--player_y] = '@';
                }
                break;
        }
    }
    return 0;
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.