Hi, I need help with my c++ maze game. The problem that I'm having is my keys and doors. I don't really know how to unlock a door after obtaining a key.

Heres my code, its a bit messy:

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <process.h>
#include <cstdlib>
#include <ctime>
#include "maze.h"
using namespace std;

const int mazeWidth = 45;
const int mazeHeight = 20;
const int mazeArray[] = 
{
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
    1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 
    1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1,
    1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 4, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1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, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1,
    1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1,
    1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
    1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
    3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,

};


//------------------------------------------------------
// These are the we set the colors
//  this color is 4 bit for the forground and 4 bit for the background
//------------------------------------------------------

/*

ONLY Settable Colors for the console

FOREGROUND_BLUE
FOREGROUND_GREEN
FOREGROUND_RED
FOREGROUND_INTENSITY

BACKGROUND_BLUE
BACKGROUND_GREEN
BACKGROUND_RED
BACKGROUND_INTENSITY

*/


//------------------------------------------------------
// Function Prototypes
//------------------------------------------------------
unsigned int __stdcall UpdateScreenThread(void *pThreadData);
void ClearScreen(WORD CharAttribute);
void DrawASCIICharacter(int x, int y, unsigned char ch, WORD CharAttribute);
void DrawPerson();
//void DrawBorder();
//void DrawBox();
//void DrawMaze();
//void DrawText();
void DrawMaze1234();
bool CanPlayerEnterSpace(int x, int y);
//void TimerUpdate();


//------------------------------------------------------
// Constant Global Variables
//------------------------------------------------------
const int WIDTH = 45;
const int HEIGHT = 30;
const WORD BACKGROUND_COLOR = 0;
const WORD BACKGROUND_COLOR_WIN = BACKGROUND_BLUE | BACKGROUND_INTENSITY;
const WORD PERSON_COLOR = FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE | BACKGROUND_COLOR;
const WORD END_COLOR = FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_GREEN | BACKGROUND_COLOR;
const WORD KEY_COLOR = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | BACKGROUND_COLOR;
const WORD DOOR_COLOR = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_RED | BACKGROUND_COLOR;
const WORD WALL_COLOR = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
const WORD FINISH_COLOR1 = FOREGROUND_INTENSITY | FOREGROUND_GREEN | BACKGROUND_COLOR;
const WORD FINISH_COLOR2 = FOREGROUND_RED | BACKGROUND_COLOR;
const WORD MAZE_COLOR = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE | BACKGROUND_COLOR;

CHAR_INFO SCREEN_BUFFER[HEIGHT * WIDTH];
COORD BUFFER_SIZE = {WIDTH, HEIGHT};
COORD CHARACTER_POSITION = {0,0};
SMALL_RECT WINDOW_SIZE = {0, 0, WIDTH-1, HEIGHT-1};


//------------------------------------------------------
// Global Variables
//------------------------------------------------------
HANDLE g_hWin;   // Handle to the Standard Output
HANDLE g_hInput; // Handle to the Standard Input

bool g_isRunning = true;
int g_personX = 1;
int g_personY = 1;
int g_boxX = 10;
int g_boxY = 9;
int g_mazeBorderX = 10;
int g_mazeBorderY = 35;
int g_iBoxUpdate = 0;
//int g_FINISHX = 9;
//int g_FINISHY = 10;
//DWORD g_currentTextColor = FINISH_COLOR1;
bool keyObtained = false;
bool isGameDone = false;

//------------------------------------------------------
// Main
//------------------------------------------------------
int main(void)
{
    //------------------------------------------
    // variables
    //------------------------------------------
    DWORD numEvents, numEventsRead = 0;  // number of Events that have happened
    INPUT_RECORD inputEvent;             // Event from the input (or windows)


    // setup the random number generator
    unsigned int seed = time(0);
    srand(seed);


    //------------------------------------------
    // Setup the windows console
    //------------------------------------------

    // get the handles to the standard windows
    g_hWin = GetStdHandle(STD_OUTPUT_HANDLE);
    g_hInput = GetStdHandle(STD_INPUT_HANDLE);

    // Set the title of the window
    SetConsoleTitle(TEXT("The Labyrinth"));

    // Set the console window size
    SetConsoleWindowInfo(g_hWin, TRUE, &WINDOW_SIZE);

    // Set the buffer size
    SetConsoleScreenBufferSize(g_hWin, BUFFER_SIZE);

    ClearScreen(BACKGROUND_COLOR);


    //------------------------------------------
    // This starts a thread that draws the screen
    //  ever 50 milliseconds
    //------------------------------------------
    unsigned int threadID = 0;
    _beginthreadex(NULL, 0, UpdateScreenThread, NULL, 0, &threadID);


    //------------------------------------------
    // This is the main Loop
    //------------------------------------------
    while (g_isRunning)
    {
        // Check the events
        GetNumberOfConsoleInputEvents(g_hInput, &numEvents);

        if (numEvents)
        {
            ReadConsoleInputA(g_hInput, &inputEvent, 1, &numEventsRead);

            if (numEventsRead)
            {
                // An event has been read in

                // Check the event type: was it a key?
                switch (inputEvent.EventType)
                {
                    case KEY_EVENT:
                        if (inputEvent.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
                        {
                            g_isRunning = false;
                        }

                        if (inputEvent.Event.KeyEvent.wVirtualKeyCode == VK_DOWN)
                        {
                            if (CanPlayerEnterSpace(g_personX, g_personY + 1))
                                g_personY++;
                        }
                        if (inputEvent.Event.KeyEvent.wVirtualKeyCode == VK_UP)
                        {
                            if (CanPlayerEnterSpace(g_personX, g_personY - 1))
                                g_personY--;
                        }
                        if (inputEvent.Event.KeyEvent.wVirtualKeyCode == VK_LEFT)
                        {
                            if (CanPlayerEnterSpace(g_personX - 1, g_personY))
                                g_personX--;
                        }
                        if (inputEvent.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
                        {
                            if (CanPlayerEnterSpace(g_personX + 1, g_personY))
                                g_personX++;
                        }
                        break;


                }
            } 
        } 


        //------------------------------------------
        // You Game code that you want to run "fast" goes here
        //------------------------------------------
        if (mazeArray[g_personY*mazeWidth + g_personX] == 3)
        {
            isGameDone = true;
        }

    **  
        if(mazeArray[g_personY*mazeWidth + g_personX] == 4)
        {
            keyObtained = true;
        }**





    } //while

    return 0;
}

void DrawMaze1234()
{
    int r, c;
    for (r = 0; r < mazeHeight; r++)
    {
        for (c=0; c < mazeWidth; c++)
        {
            switch (mazeArray[r*mazeWidth + c])
            {
                case 1: DrawASCIICharacter(c, r, 219, MAZE_COLOR); break;
                case 2: DrawASCIICharacter(c, r, 212, MAZE_COLOR); break;
                case 3: DrawASCIICharacter(c, r, '*', END_COLOR); break;
            **  case 4: DrawASCIICharacter(c, r, 'K', KEY_COLOR); break;**
                case 5: DrawASCIICharacter(c, r, 68, DOOR_COLOR); break;

            }

        }
    }

}

bool CanPlayerEnterSpace(int x, int y)
{
    if (mazeArray[y*mazeWidth + x] == 1) return false;
    return true;


}

//------------------------------------------
// This is the thread that updates the screen every 50 milliseconds
//------------------------------------------
unsigned int __stdcall UpdateScreenThread(void *pThreadData)
{
    while(g_isRunning)
    {
        // call out timer update function
      //  TimerUpdate();
        if (isGameDone)
        {
            // Update the Screen
            ClearScreen(BACKGROUND_COLOR_WIN);
            DrawASCIICharacter(17, 9, 'Y', PERSON_COLOR);
            DrawASCIICharacter(18, 9, 'o', PERSON_COLOR);
            DrawASCIICharacter(19, 9, 'u', PERSON_COLOR);
            DrawASCIICharacter(20, 9, ' ', PERSON_COLOR);
            DrawASCIICharacter(21, 9, 'W', PERSON_COLOR);
            DrawASCIICharacter(22, 9, 'i', PERSON_COLOR);
            DrawASCIICharacter(23, 9, 'n', PERSON_COLOR);
            DrawASCIICharacter(24, 9, '!', PERSON_COLOR);
        }
        else
        {
            **if(keyObtained)
            {
                switch(mazeArray[g_personY*mazeWidth+g_personX])
                {
                case 'K':
                    {
                        mazeArray[g_personY*mazeWidth+g_personX] == 0;
                    }
                }**

            }
            // Update the Screen
            ClearScreen(BACKGROUND_COLOR);
            DrawMaze1234();
            DrawPerson();
            //DrawBorder();
            //DrawMaze();
            //DrawBox();

            DrawASCIICharacter(9, 21, 'K', KEY_COLOR);
            DrawASCIICharacter(10, 21, ' ', KEY_COLOR);
            DrawASCIICharacter(11, 21, '=', KEY_COLOR);
            DrawASCIICharacter(12, 21, ' ', KEY_COLOR);
            DrawASCIICharacter(13, 21, 'K', KEY_COLOR);
            DrawASCIICharacter(14, 21, 'e', KEY_COLOR);
            DrawASCIICharacter(15, 21, 'y', KEY_COLOR);

            DrawASCIICharacter(9, 23, 'D', DOOR_COLOR);
            DrawASCIICharacter(10, 23, ' ', DOOR_COLOR);
            DrawASCIICharacter(11, 23, '=', DOOR_COLOR);
            DrawASCIICharacter(12, 23, ' ', DOOR_COLOR);
            DrawASCIICharacter(13, 23, 'D', DOOR_COLOR);
            DrawASCIICharacter(14, 23, 'o', DOOR_COLOR);
            DrawASCIICharacter(15, 23, 'o', DOOR_COLOR);
            DrawASCIICharacter(16, 23, 'r', DOOR_COLOR);

            DrawASCIICharacter(19, 21, 2, PERSON_COLOR);
            DrawASCIICharacter(21, 21, ' ', PERSON_COLOR);
            DrawASCIICharacter(21, 21, '=', PERSON_COLOR);
            DrawASCIICharacter(22, 21, ' ', PERSON_COLOR);
            DrawASCIICharacter(23, 21, 'Y', PERSON_COLOR);
            DrawASCIICharacter(24, 21, 'o', PERSON_COLOR);
            DrawASCIICharacter(25, 21, 'u', PERSON_COLOR);

            DrawASCIICharacter(19, 23, '*', END_COLOR);
            DrawASCIICharacter(21, 23, ' ', END_COLOR);
            DrawASCIICharacter(21, 23, '=', END_COLOR);
            DrawASCIICharacter(22, 23, ' ', END_COLOR);
            DrawASCIICharacter(23, 23, 'G', END_COLOR);
            DrawASCIICharacter(24, 23, 'o', END_COLOR);
            DrawASCIICharacter(25, 23, 'a', END_COLOR);
            DrawASCIICharacter(26, 23, 'l', END_COLOR);
        }

        // WriteConsoleOutputA is WriteConsoleOutput ASCII
        WriteConsoleOutputA(g_hWin, SCREEN_BUFFER, BUFFER_SIZE, CHARACTER_POSITION, &WINDOW_SIZE);

        // Wait 50 milli-seconds
        Sleep(50);
    }

    return 0;
}


//------------------------------------------
// This clears the screen to the CharAttribute
//  specifically to the background color of the CharAttribute
//------------------------------------------
void ClearScreen(WORD CharAttribute)
{
    int r, c;
    for (r = 0; r < HEIGHT; r++)
    {
        for (c = 0; c < WIDTH; c++)
        {
            SCREEN_BUFFER[r*WIDTH + c].Char.AsciiChar = ' ';
            SCREEN_BUFFER[r*WIDTH + c].Attributes = CharAttribute;
        }
    }
}


//------------------------------------------
// Put an ASCII Character at point x and y
//------------------------------------------
void DrawASCIICharacter(int x, int y, unsigned char ch, WORD CharAttribute)
{
    if (x < 0) return;
    if (y < 0 ) return;
    if (x >= WIDTH) return;
    if (y >= HEIGHT) return;

    SCREEN_BUFFER[y*WIDTH + x].Char.AsciiChar = ch;
    SCREEN_BUFFER[y*WIDTH + x].Attributes = CharAttribute;
}


//------------------------------------------
// Draws our ASCII Person to the screen
//------------------------------------------
void DrawPerson()
{
    // New Box
//    DrawASCIICharacter(g_personX-1, g_personY - 1, 220, PERSON_COLOR);
//  DrawASCIICharacter(g_personX-2, g_personY - 1, 220, PERSON_COLOR);
    // New Box
//    DrawASCIICharacter(g_personX-2, g_personY, 223, PERSON_COLOR);
//    DrawASCIICharacter(g_personX-1, g_personY, 223, PERSON_COLOR);

    DrawASCIICharacter(g_personX, g_personY, 2, PERSON_COLOR);

} 

From line 293, you have:

 **if(keyObtained)
{
switch(mazeArray[g_personY*mazeWidth+g_personX])
{
case 'K':
{
mazeArray[g_personY*mazeWidth+g_personX] == 0;
}
}**
}

But I don't understand. in case 'K' you just make a comparison. Is that supposed to be an assignment? If so, simply delete one of the equals signs. I also don't understand the asterisks next to the if() statement. Anyway, I have feeling that making that comparison into an assignment will fix your problem.

Oh I was trying to make that part of the code bold but I kind of failed at that. Just ignore the astericks. When I delete one of the equal signs, I get an error "expression must be a modifiable value."

"const int mazeArray" may have something to do with that.

Just as an aside, you can remove the #includes for <stdio.h> and <stdlib.h>; you never use <stdio.h>, and <stdlib.h> is the C version of <cstdlib>. Just something I figured I would point out.

Given the number of places where you are writing out a series of characters on a given line in a given color, I would recommend a function like this:

int DrawString(int xStart, int yStart, string text, WORD CharAttribute)
{
    int count;
    int len = text.length();

    for (count = xStart; count < xStart + len; count++)
    {
        DrawASCIICharacter(count, yStart, text.CharAt(count), CharAttribute);
    }

    return count;
}

You will need to #include <string> in order to use this code as it is written. For characters that are normally non-printing, you could use the backslash+numeric code approach:

DrawString(19, 21, "\002 = You", PERSON_COLOR);

Oops, that should have been

        DrawASCIICharacter(count, yStart, text[count], CharAttribute);

Apparently, I'm getting confused between languages. This is what happens when you post at 3AM, I guess.

Thanks, I got it fixed now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.