-1

I am building a console based, text based, dungeon game that has been going pretty well so far. I was trying to add a entrance in a 'Developer Mode' of sorts and after that once my room1() function starts running it continuously outputting my text to the console. It was working fine before I added this feature, so I took out the code I put into the program but it still happens. It also completely skips over the getline(cin, playerMove) and doesn't give you the option to enter a string. Also I know it's probably super bad coding strategies but i've only been at this for less than three weeks, although any tips are welcome as this is hopefully going to be my career.

Thank you in advance!

Code:

#include <iostream>
#include <windows.h>
#include <locale>
#include <fstream>
#include <string>

using namespace std;

//Declaring Handles
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

//Declaring Functions
void startText(), room1(), room2(), room3(), room4(), playerMoveQ(), invalidAnswer(), playerMoveOptions(), gameEngine(), healthBar(), root();

//Declaring Strings
string enterCave, playerName, bag = "Currently, your bag is empty.", playerMove, getRoot;

//Declaring Integers
int cash = 0;
int playerLocation = 1;

//Declaring Booleans
bool gameOn = true, firstTurn = true, swordObtained = false, armorObtained = false, bagFilled = false;

void gameEngine()
{
    room1();
}

void startText()
{
    SetConsoleTextAttribute(hConsole, 14);
    cout << "Enter your name : ";
    getline(cin, playerName);
    endl(cout);

    SetConsoleTextAttribute(hConsole, 1);
    cout << "################################" << endl;
    cout << "#                              #" << endl;
    cout << "#          Dr. Snidley         #" << endl;
    cout << "#                              #" << endl;
    cout << "################################" << endl;
    endl(cout);

    SetConsoleTextAttribute(hConsole, 13);
    cout << "The Princess of the Kingdom, Percia, has been kidnapped by the evil Dr. Snidley." << endl;
    cout << "It is up to you, brave traveler, to enter this evil dungeon and save the Princess of Percia," << endl << "and bring peace to her Kingdom. ";
    cout << "There is 9 rooms in this dungeon and one exit," << endl << "there is only ever one monster and/or items in a room.";
    cout << "Enter the cave now, or cower away in disgrace!" << endl;
    endl(cout);
}

void room1()
{
        SetConsoleTextAttribute(hConsole, 8);
        cout << "Currently you are in Room 1. As you look gameEngine();around you notice a series of canvas' around the room." << endl;
        cout << "You see that all of the pictures are torn out, all but the Princess'. You know you don't have time and you must hurry!" << endl;

        SetConsoleTextAttribute(hConsole, 11);
        cout << "There is a room to your South. There is a room to your East." << endl;

        healthBar();

        SetConsoleTextAttribute(hConsole, 2);
        cout << "Your bag contains the following items :" << endl;
        cout << bag;
        endl(cout);

        SetConsoleTextAttribute(hConsole, 1);
        cout << "Current cash is : " << cash << endl;
        endl(cout);

        playerMoveQ();
}

void room2()
{
    SetConsoleTextAttribute(hConsole, 8);
    cout << "Currently you are in Room 2. This room is shockingly lighten up, at least more than the others." << endl << "You hear an abundance of growls coming from the room to your South." << endl;

    SetConsoleTextAttribute(hConsole, 11);
    cout << "There is a room to your East. There is a room to your South. There is a room to your West." << endl;

    healthBar();

    SetConsoleTextAttribute(hConsole, 2);
    cout << "Your bag contains the following items :" << endl << bag << endl;

    SetConsoleTextAttribute(hConsole, 1);
    cout << "Current cash is : " << cash << endl;
    endl(cout);

    playerMoveQ();
}

void room3()
{
    if (armorObtained == false)
    {
        //Story / Room Loacation
        SetConsoleTextAttribute(hConsole, 8);
        cout << "Currently you are in Room 3. In this room you see a shimmering coming from the corner." << endl << "You decide to check it out and find a rugged looking set of armor." << endl;
        //Saying Rooms
        SetConsoleTextAttribute(hConsole, 11);gameEngine();
        cout << "There is a room to your South. There is a room to your West." << endl;
        //Items on floor
        SetConsoleTextAttribute(hConsole, 15);
        cout << "The ARMOR is in its stand on the other side of the room." << endl;
        //Health Bar
        healthBar();
        //Items in bag
        SetConsoleTextAttribute(hConsole, 2);
        cout << "Your bag contains the following items :" << endl << bag << endl;
        //Total cash
        SetConsoleTextAttribute(hConsole, 1);
        cout << "Current cash is : " << cash << endl;
        endl(cout);
        //Ask players next move
        playerMoveQ();
    } else if (armorObtained == true)
    {
        //Story / Room Loacation
        SetConsoleTextAttribute(hConsole, 8);
        cout << "Currently you are in Room 3. In this room you see a shimmering coming from the corner." << endl << "You decide to check it out and find a rugged looking set of armor." << endl;
        //Saying Rooms
        SetConsoleTextAttribute(hConsole, 11);
        cout << "There is a room to your South. There is a room to your West." << endl;
        //Displaying player health
        healthBar();
        //Items in bag
        SetConsoleTextAttribute(hConsole, 2);
        cout << "Your bag contains the following items :" << endl << bag << endl;
        //Total cash
        SetConsoleTextAttribute(hConsole, 1);
        cout << "Current cash is : " << cash << endl;
        endl(cout);
        //Ask players next move
        playerMoveQ();
    }
}

void room4()
{
    if (swordObtained == false)
    {
        SetConsoleTextAttribute(hConsole, 8);
        cout << "Currently you are in Room 4. This room looks nothing like the last, nothing but a single podium is in the center of the room. Although something does not feel right." << endl;

        SetConsoleTextAttribute(hConsole, 11);
        cout << "There is a room to your North. " << endl;

        SetConsoleTextAttribute(hConsole, 15);
        cout << "The SWORD is lying at the feet of the podium." << endl;

        healthBar();

        SetConsoleTextAttribute(hConsole, 2);
        cout << "Your bag contains the following items :" << endl << bag << endl;

        SetConsoleTextAttribute(hConsole, 1);
        cout << "Current cash is : " << cash << endl;
        endl(cout);

        playerMoveQ();
    } else if (swordObtained == true)
    {
        SetConsoleTextAttribute(hConsole, 8);
        cout << "Currently you are in Room 4. This room looks nothing like the last, nothing but a single podium is in the center of the room. Although something does not feel right." << endl;

        SetConsoleTextAttribute(hConsole, 11);
        cout << "There is a room to your North. " << endl;

        healthBar();

        SetConsoleTextAttribute(hConsole, 2);
        cout << "Your bag contains the following items :" << endl << bag << endl;

        SetConsoleTextAttribute(hConsole, 1);
        cout << "Current cash is : " << cash << endl;
        endl(cout);

        playerMoveQ();
    }
}

void room5()
{
    if (armorObtained == false)
    {
        //Story / Room Location
        SetConsoleTextAttribute(hConsole, 8);
        cout << "Currently you are Room 5. As you enter it is hard to divert your eyes from the Cyberus laying by your feet. " << endl << "You look to see the other side of the room and see a locked door. You thne see the Key around the Cyberus' neck.";
        //Outputing Rooms
        SetConsoleTextAttribute(hConsole, 11);
        cout << "There is a room to your East. There is a ";
        SetConsoleTextAttribute(hConsole, 8);
        cout << "LOCKED";
        SetConsoleTextAttribute(hConsole, 11);
        cout << "to your South.";gameEngine();
    }
}

void playerMoveQ()
{
    SetConsoleTextAttribute(hConsole, 14);
    cout << "What would you like to do : ";
    getline(cin, playerMove);
    if (firstTurn == true)
    {
        getline(cin, playerMove);
    }
    firstTurn = false;
    playerMoveOptions();
}

void playerMoveOptions()
{
    if (playerMove == "move south" && playerLocation == 1)
    {
        endl(cout);
        SetConsoleTextAttribute(hConsole, 12);
        cout << "You have successfully moved to Room : 4" << endl;
        playerLocation = 4;
        room4();
    } else if (playerMove == "pick sword" && playerLocation == 4)
    {
        endl(cout);
        SetConsoleTextAttribute(hConsole, 11);
        cout << "You have successfully picked up the SWORD!" << endl;
        endl(cout);
        if (bagFilled == true)
        {
            bag += "\nSWORD";
        } else if (bagFilled == false)
        {
            bag = "SWORD";
        }
        cash += 500;
        swordObtained = true;
        bagFilled = true;
        room4();
    } else if (playerMove == "move north" && playerLocation == 4)
    {
        endl(cout);
        SetConsoleTextAttribute(hConsole, 12);
        cout << "You have successfully moved to Room : 1" << endl;
        playerLocation = 1;
        room1();
    } else if (playerMove == "move east" && playerLocation == 1)
    {
        endl(cout);
        SetConsoleTextAttribute(hConsole, 12);
        cout << "You have successfully moved to Room : 2" << endl;
        playerLocation = 2;
        room2();
    } else if (playerMove == "move east" && playerLocation == 2)
    {
        endl(cout);
        SetConsoleTextAttribute(hConsole, 12);
        cout << "You have successfully moved to Room : 3" << endl;
        playerLocation = 3;
        room3();
    } else if (playerMove == "move west" && playerLocation == 2)
    {
        endl(cout);
        SetConsoleTextAttribute(hConsole, 12);
        cout << "You have successfully moved to Room : 1" << endl;
        playerLocation = 1;
        room1();
    } else if (playerMove == "move west" && playerLocation == 3)
    {
        endl(cout);
        SetConsoleTextAttribute(hConsole, 12);
        cout << "You have successfully moved to Room : 2" << endl;
        playerLocation = 2;
        room2();
    } else if (playerMove == "pick armor" && playerLocation == 3)
    {
        endl(cout);
        SetConsoleTextAttribute(hConsole, 11);
        cout << "You have successfully equiped the ARMOR! You may have 1 extra hit now!" << endl;
        endl(cout);
        if (bagFilled == true)
        {
            bag += "\nARMOR";
        } else if (bagFilled == false)
        {
            bag = "ARMOR";
        }
        cash += 1000;
        armorObtained = true;
        bagFilled = true;
        room3();
    } else if (playerMove == "move south" && playerLocation == 2)
    {
        endl(cout);
        SetConsoleTextAttribute(hConsole, 12);
        cout << "You have successfully move to Room : 5" << endl;
        playerLocation = 5;
        room5();
    } else
    {
        invalidAnswer();
    }
}

void invalidAnswer()
{
    SetConsoleTextAttribute(hConsole, 12);
    endl(cout);
    cout << "INVALID COMMAND. Please enter a valid command!" << endl;
    playerMoveQ();
}

void healthBar()
{
    if (armorObtained == false)
    {
        SetConsoleTextAttribute(hConsole, 15);
        cout << "[";
        SetConsoleTextAttribute(hConsole, 192);
        cout << " ";
        SetConsoleTextAttribute(hConsole, 15);
        cout << "|";gameEngine();
        SetConsoleTextAttribute(hConsole, 192);
        cout << " ";
        SetConsoleTextAttribute(hConsole, 15);
        cout << "|";
        SetConsoleTextAttribute(hConsole, 192);
        cout << " ";
        SetConsoleTextAttribute(hConsole, 15);
        cout << "]" << endl;
    } else if (armorObtained == true)
    {
        SetConsoleTextAttribute(hConsole, 15);
        cout << "[";
        SetConsoleTextAttribute(hConsole, 192);
        cout << " ";
        SetConsoleTextAttribute(hConsole, 15);
        cout << "|";
        SetConsoleTextAttribute(hConsole, 192);
        cout << " ";
        SetConsoleTextAttribute(hConsole, 15);
        cout << "|";
        SetConsoleTextAttribute(hConsole, 192);
        cout << " ";
        SetConsoleTextAttribute(hConsole, 15);
        cout << "|";
        SetConsoleTextAttribute(hConsole, 192);
        cout << " ";
        SetConsoleTextAttribute(hConsole, 15);
        cout << "|";
        SetConsoleTextAttribute(hConsole, 192);
        cout << " ";
        SetConsoleTextAttribute(hConsole, 15);
        cout << "]" << endl;
    }
}

void root()
{
    system("CLS");
    getline(cin, getRoot);
    if (getRoot == "room 1")
    {
        room1();
    } else if (getRoot == "room 2")
    {
        room2();
    } else if (getRoot == "room 3")
    {
        room3();
    } else if (getRoot == "room 4")
    {
        room4();
    } else if (getRoot == "room 5")
    {
        room5();
    }
}

int main()
{
    //Story & Start Text
    startText();
    //Game Engine
    if (playerName == "sudo root")
    {
        root();
    } else 
    {
        gameEngine();
    }

    return 0;
}
Keaton H
  • 25
  • 5
  • 2
    StackOverflow is not a debugging service. What have you done so far to troubleshoot the code yourself? Have you stepped through it with a debugger? That is a vital skill to have. – Remy Lebeau Nov 18 '18 at 23:47
  • 1
    I suspect a typo is responsible: In `healthBar()`: `cout << "|";gameEngine();` should probably be `cout << "|";`. This is a good use case for a debugger. Learn about it and use the one for your platform: https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems –  Nov 18 '18 at 23:47
  • Yes that is a typo, have tried reversing the code I entered that made it happen. That did not undo it. I then went and tried to change the user input method to cin >> instead of getline(cin, playerMove). I then also tried to change the way I called my room code thinking it may be the way I am calling it but when called alone it still happens. – Keaton H Nov 19 '18 at 00:07
  • @KeatonH So is the output still the same after fixing the typo? Because I suspect it will not be. In any case, as you were told, learn to use a debugger to step through the code and see what actually happens. –  Nov 19 '18 at 00:16
  • No I changed the typo and that was the issue, I will take a look into some common debugging techniques. Thank you! – Keaton H Nov 20 '18 at 00:04

1 Answers1

1

I suggest you learn how to debug. Run the code in a debugger and set a break point where you suspect the code to take a wrong turn.

Since you are writing a Windows application, I assume you are using Visual Studio. Press F5 to start your program in a debugger. There are tons of tutorial on how to debug. Check them out.

uceumern
  • 848
  • 9
  • 12