so I tried making my first game, it is supposed to be a text-based multi-option game but it doesn't stop when it should. this is the best way I could figure out how to make what I wanted.
#include <iostream>
using namespace std;
int main()
{
int choise = 0;
int place = 0;
int ending =0;
while (ending < 1){
switch (place){
case 0:// place
cout << "you wake up to the smell of fire, what do you do?" << endl;
cout << " 1 go back to sleep \n 2 rush towards door \n 3 rush towards window \n" << endl;
cin >> choise;
switch (choise){
case 1:
cout << "that was stupid. \nrestarting game\n" << endl;
place = 0;
break;
case 2:
cout << "you rush towards the door\n" << endl;
place = 1;
break;
case 3:
cout << "you rush towards the window\n" << endl;
place = 2;
break;
default:
cout << "invalid number \ntry again\n" << endl;
place = 0;
break;
}
break;
case 1:// place
cout << "you reach the door, what do you do?" << endl;
cout << " 1 open door and leave \n 2 return to the bed\n" << endl;
cin >> choise;
switch (choise){
case 1:
cout << "you leave the room and escape safely" << endl;
ending = 1;
break;
case 2:
cout << "you return to the bed\n" << endl;
place = 0;
break;
}
case 2:// place
cout << "you reach the window, what do you do" << endl;
cout << " 1 leave trough the window \n 2 return to the bed" << endl;
cin >> choise;
switch (choise){
case 1:
cout << "you climb trough the window and escape safely" << endl;
ending = 1;
break;
case 2:
cout << "you return to the bed\n" << endl;
place = 0;
break;
}
break;
}
}
return 0;
}
if you can't help me solve my problem maybe guide me to a tutorial that can help me make it differently.
#include <map>
#include <string>
int main() {
typedef map<string, string> MapGame; // game relations
typedef map<string, string> MapDescription; // object descriptions
typedef map<string, string> MapCommand; // commands (user input)
typedef MapGame::iterator ItMapGame;
typedef MapDescription::iterator ItMapDescription;
typedef MapCommand::iterator ItMapCommand;
MapGame g; // game relations
MapDescription d; // object descriptions
MapCommand c; // commands (user input), prerequisits and actions (workflow)
// A simple game:
// There is a room, a user, a key and a door.
// The User have to "get the key" and "open the door".
g["game"] = "started"; // game state
g["room"] = "user;key;door"; // room has user, key and door
g["door"] = "closed"; // door is closed (state)
d["room"] = "This is the room where you woke up this morning.";
d["door"] = "The room has a door where you can escape!";
d["key" ] = "This is a key."
d["user"] = "You are the user trying to find the way out."
// Pseudo-Code
// if command is one of "open door;unlook door" then
// check condition "user has key"
// check condition "door is closed"
// if all conditions ok
// then "door is open" and "game is finished"
c["open door"] = "[user key;door closed][door open;game finished]";
c["unlook door"] = "ref: open door"]
// if command is on of "get key;take key" then
// check condition "room has key"
// if all conditions ok
// then "user has key", delete "room has key"
c["get key" ] = "[room key][user key from room]";
c["take key" [ = "ref: get key";
// if command is on of "look around;where i am" then
// find all relations to "user" and show the descriptions
// example: room-user : cout << d["room"] << endl;
// room-key : cout << d["key"] etc.
c["look arround" ] = "describe";
c["where i am" ] = "ref: look arroung";
while (true) {
ItMapGame itG = g.find("game");
if (itG == g.end())
return -1; // assert
if (itG->second == "finished")
return 1; // success
string strInput;
cin >> strInput;
ItMapCommand itC = c.find(strInput);
if (itC == c.end()) {
cout << "I am sorry?" << endl;
continue;
}
// PseudoCode:
string strCommand = itC->second;
// split strCommand in 2 strings, enclosed in [ ... ]
// e.g. strPreReq = "user key;door closed"
// strAction = "door open;game finished"
// check the conditions in strPreReq (splitting again in : "user key" and "door closed"
// e.g. itG = g.find("user");
// if (itG == g.end()) return -1; // assert
// if (itG->second != "key") { cout << "prereq. failed. user has not the key to open the door!"; continue; }
// if the code is here, all conditions were fullfilled
// Now do strAction : split string in "door open", "game finised"
// itG = g.find("door"); // check assert ...
// itG->second = "open"; cout << "door open!" << endl;
// itG = g.find("game"); // check assert ...
// itG->second = "finished"; cout << "game finished!" << endl;
}
return 0;
}