Hack Forums
[C] [C++ - Win32] Maze Blaze - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] [C++ - Win32] Maze Blaze (/showthread.php?tid=2797183)



[C++ - Win32] Maze Blaze - Irritiable - Aug 20, 2012

Hello. This is also a fun, and addicting game I'd wrote for myself.
It's very ugly, poor, but it runs! I do not see any sense in (personally) fixing it up.
It is what it is! 😎



- main.cpp
Code
#include <stdio.h>
#include <TextControl.h>

#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3

void RandomlyGenerateMaze(int Walls[][16]) {

    int temp = 0;

    for (int x = 0; x < 15; x++) {
        for (int y = 0; y < 15; y++) {
            temp = rand() % 7;
            if (Walls[x][y] == 0 && temp > 5) {
                Walls[x][y] = 1;
            }
        }
    }

    return;
}

void DisplayMaze(int Walls[][16]) {

    SetColor(DARK_WHITE, BLACK);

    for (int x = 0; x < 15; x++) {
        for (int y = 0; y < 15; y++) {
            if (Walls[x][y] == 1) {
                PlaceCursor(x + 25, y + 10);
                printf("#");
            }
        }
    }

    return;
}

void ResetMaze(int Walls[][16]) {

    for (int x = 1; x < 14; x++) {
        for (int y = 1; y < 14; y++) {
            PlaceCursor(x + 25, y + 10);
            printf(" ");
            Walls[x][y] = 0;
        }
    }

    return;
}

bool MovePlayer(const int Direction, const int Walls[][16], int &PlayerX, int &PlayerY) {

    switch (Direction) {
        case UP: {
            if (Walls[PlayerX - 25][(PlayerY + 1) - 12] != 1) {
                PlaceCursor(PlayerX, PlayerY);
                printf(" ");

                PlayerY -= 1;
                PlaceCursor(PlayerX, PlayerY);
                printf("X");
            }
        } break;

        case DOWN: {
            if (Walls[PlayerX - 25][(PlayerY + 1) - 10] != 1) {
                PlaceCursor(PlayerX, PlayerY);
                printf(" ");

                PlayerY += 1;
                PlaceCursor(PlayerX, PlayerY);
                printf("X");
            }
        } break;

        case LEFT: {
            if (Walls[(PlayerX - 1) - 25][PlayerY - 10] != 1) {
                PlaceCursor(PlayerX, PlayerY);
                printf(" ");

                PlayerX -= 1;
                PlaceCursor(PlayerX, PlayerY);
                printf("X");
            }
        } break;

        case RIGHT: {
            if (Walls[(PlayerX + 1) - 25][PlayerY - 10] != 1) {
                PlaceCursor(PlayerX, PlayerY);
                printf(" ");

                PlayerX += 1;
                PlaceCursor(PlayerX, PlayerY);
                printf("X");
            }
        } break;

        default: return false;
    }

    return true;
}

int main() {

    HANDLE OutHandle = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleTitle("Snake - AI Module");
    SMALL_RECT windowSize = {0, 0, 79, 49};
    SetConsoleWindowInfo(OutHandle, TRUE, &windowSize);

    ClearConsole(GREY, BLACK);
    RemoveCursor();

    int Walls[16][16] = {
        { 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, },
        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1  },
    };

GAME_START:

    srand(time(NULL));

    PlaceCursor(25, 12);
    SetColor(BLACK, GREEN);
    printf(" ");
    SetColor(GREY, BLACK);

    Walls[1][2] = 15;
    Walls[13][11] = 5; // The player

    RandomlyGenerateMaze(Walls);
    DisplayMaze(Walls);

    PlaceCursor(39, 21);
    SetColor(BLACK, RED);
    printf(" ");
    SetColor(GREY, BLACK);

    int PlayerX = 38;
    int PlayerY = 21;

    PlaceCursor(PlayerX, PlayerY);
    SetColor(CYAN, BLACK);
    printf("X");

    while (true) {
        Sleep(50);

        if (GetAsyncKeyState(VK_UP)) { MovePlayer(UP, Walls, PlayerX, PlayerY); } else
        if (GetAsyncKeyState(VK_DOWN)) { MovePlayer(DOWN, Walls, PlayerX, PlayerY); } else
        if (GetAsyncKeyState(VK_LEFT)) { MovePlayer(LEFT, Walls, PlayerX, PlayerY); } else
        if (GetAsyncKeyState(VK_RIGHT)) { MovePlayer(RIGHT, Walls, PlayerX, PlayerY); }

        if (Walls[(PlayerX) - 25][PlayerY - 10] == 3) {
            SetColor(GREEN, BLACK);
            PlaceCursor(PlayerX, PlayerY);
            printf("X");
            SetColor(GREY, BLACK);
            break;
        }
    }

    PlaceCursor(0, 0);
    printf("YOU WIN!\n\n");

    int temp = 0;

    printf("Play again (0/1)? ");
    scanf("%d", &temp);

    if (temp >= 1) {
        PlaceCursor(0, 0);
        printf("        \n\n                    ");

        ResetMaze(Walls);
        goto GAME_START;
    }

    std::cin.get();
    return 0;
}

There's also a (very dumb) AI version that solves itself 3 times out of 5.


RE: [C++ - Win32] Maze Blaze - MatiasMunk - Mar 2, 2019

This can be absolutely fantastic for beginners to learn from. If they just saw what was right in front of their eyes :)) Bump rep+


RE: [C++ - Win32] Maze Blaze - skid1234567 - Mar 18, 2019

this is beyond garbage code


RE: [C++ - Win32] Maze Blaze - morph - Mar 19, 2019

Good first project. I'd recommend looking into C++'s standard library. Also, don't use labels and goto.


RE: [C++ - Win32] Maze Blaze - Irritiable - May 7, 2019

(Mar 2, 2019 08:40 AM)MatiasMunk Wrote:  This can be absolutely fantastic for beginners to learn from. If they just saw what was right in front of their eyes :)) Bump rep+

That was the idea (2012 is when it was posted). It's to show you that even the infamous console is nothing special.

Thanks! Although the ~7 year old bump wasn't necessary.


(Mar 18, 2019 12:40 AM)skid1234567 Wrote:  this is beyond garbage code

The design and everything else is poorly written, yes. I agree with you.


(Mar 19, 2019 10:40 AM)morph Wrote:  Good first project. I'd recommend looking into C++'s standard library. Also, don't use labels and goto.

This is hardly my first project. I believe this was written sometime in 2008 (~11 years ago).


RE: [C++ - Win32] Maze Blaze - morph - May 8, 2019

(May 7, 2019 10:11 PM)Irritiable Wrote:  That was the idea (2012 is when it was posted). It's to show you that even the infamous console is nothing special.

Thanks! Although the ~7 year old bump wasn't necessary.



The design and everything else is poorly written, yes. I agree with you.



This is hardly my first project. I believe this was written sometime in 2008 (~11 years ago).

Lol, didn't see that this was posted in 2012.


RE: [C++ - Win32] Maze Blaze - MaximumPain - May 8, 2019

(May 8, 2019 04:18 AM)morph Wrote:  Lol, didn't see that this was posted in 2012.

LMAO. bumping threads from 2012 🤣 🤣