Skip to content

Instantly share code, notes, and snippets.

@Ge-lx
Last active December 14, 2017 14:17
Show Gist options
  • Save Ge-lx/74cea197f49499a497cfad258600c2ea to your computer and use it in GitHub Desktop.
Save Ge-lx/74cea197f49499a497cfad258600c2ea to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
class Room {
public:
Room (string n, unsigned short times) {name = n; allowedEntries = times; North = this; South = this; East = this; West = this;};
unsigned short allowedEntries;
string name;
Room *North, *South, *East, *West;
//Gibt false zurück, wenn der Raum zu oft betreten wurde
bool enter() {
if (allowedEntries < 1) return false;
cout << "Sie sind im " << name << " und kommen noch " << allowedEntries << "mal hierhinein. Es gibt Wege nach";
if (East != this) cout << " O";
if (West != this) cout << " W";
if (North != this) cout << " N";
if (South != this) cout << " S";
cout << ". Wohin? (X:exit)" << endl;
allowedEntries --;
return true;
}
//'Verbindet' zwei Raume an der angegebenen Stelle.
//nicht gefragt, aber hofftenlich kein Problem ;)
void link(const char& dir, Room* other) {
switch (toupper(dir)) {
case 'N': North = other; other->South = this; break; // toupper() konvertiert zu einem Großbuchstaben
case 'S': South = other; other->North = this; break;
case 'O': East = other; other->West = this; break;
case 'W': West = other; other->East = this; break;
default: cout << "unbekannte Richtung: " << dir << endl;
}
}
};
int main()
{
Room * bad = new Room("Bad", 1);
Room * schlafZ = new Room("Schlafzimmer", 2);
Room * wohnZ = new Room("Wohnzimmer", 3);
Room * essZ = new Room("Esszimmer", 4);
Room * flur = new Room("Flur", 5);
Room * kueche = new Room("Kueche", 6);
Room * exit = new Room("Ausgang", 1);
flur->link('N', wohnZ);
flur->link('O', kueche);
flur->link('W', bad);
flur->link('S', exit);
wohnZ->link('O', essZ);
wohnZ->link('W', schlafZ);
schlafZ->link('S', bad);
essZ->link('S', kueche);
/*bad->North = schlafZ; bad->West = flur;
schlafZ->South = bad; schlafZ->East = wohnZ;
wohnZ->West = schlafZ; wohnZ->South = flur; wohnZ->East = essZ;
essZ->West = wohnZ; essZ->South = kueche;
flur->West = bad; flur->North = wohnZ; flur->East = kueche; flur->South = exit;
kueche->North = essZ; kueche->West = flur;*/
//Startet jetzt im Schlafzimmer
Room * here = schlafZ;
bool done = false;
do {
if (!here->enter()) {
cout << here->name << " zu oft betreten. Sie sind ausgeschieden!" << endl;
break;
}
string in;
cin >> in;
switch (toupper(in[0])) { // in[0] ist der erste Buchstabe der Eingabe.
case 'N': here = here->North; break; // toupper() konvertiert zu einem Großbuchstaben
case 'S': here = here->South; break;
case 'O': here = here->East; break;
case 'W': here = here->West; break;
default: done = true; cout << "Tschuess!\n"; break;
}
if(here == exit) {
cout << "Sie haben den Ausgang gefunden! Glueckwunsch.";
done = true;
}
} while (!done);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment