Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

[C++ - Win32] Maze Blaze

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.
Is there any such thing as a woman of honor, and sincerity?

https://www.PutItOnMute.com/

4K HDR, WDR time-lapsed truck driving videos!
This can be absolutely fantastic for beginners to learn from. If they just saw what was right in front of their eyes :)) Bump rep+
this is beyond garbage code
Good first project. I'd recommend looking into C++'s standard library. Also, don't use labels and goto.
(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).
Is there any such thing as a woman of honor, and sincerity?

https://www.PutItOnMute.com/

4K HDR, WDR time-lapsed truck driving videos!
(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.
(May 8, 2019 04:18 AM)morph Wrote:  Lol, didn't see that this was posted in 2012.

LMAO. bumping threads from 2012 🤣 🤣


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by xZalurexMay 29, 2021 05:24 PM 2 148 May 29, 2021 05:24 PM
Last Post: xZalurex
[C#] by notahackermanApr 26, 2020 11:32 PM 17 772 Apr 26, 2020 11:32 PM
Last Post: Shadow_Cloud
[C] by SIM-3991524Mar 11, 2020 02:03 PM 11 347 Mar 11, 2020 02:03 PM
Last Post: some1