Added several commands
This commit is contained in:
@@ -64,6 +64,12 @@ int CommandParser::Parse() {
|
||||
nouns = Noun(c);
|
||||
|
||||
for (const auto &verb: verbs) {
|
||||
|
||||
if (!commands[verb]) {
|
||||
printf("Sorry kid, can't understand ya! \n");
|
||||
break;
|
||||
}
|
||||
|
||||
commands[verb]("");
|
||||
}
|
||||
return 0;
|
||||
|
||||
+55
-6
@@ -3,9 +3,9 @@
|
||||
|
||||
bool isActive;
|
||||
|
||||
int curX, curY;
|
||||
|
||||
Game::Game() {
|
||||
curX = 0;
|
||||
curY = 0;
|
||||
progress = 0;
|
||||
Init();
|
||||
}
|
||||
@@ -25,17 +25,18 @@ bool Game::Init() {
|
||||
rooms = json.get<std::vector<std::vector<Room>>>();
|
||||
player.Init();
|
||||
parser = CommandParser();
|
||||
parser.AddCommand("Exit", Quit);
|
||||
|
||||
currentRoom = rooms[0][0];
|
||||
parser.AddCommand("Exit", Quit);
|
||||
parser.AddCommand("Move", Move);
|
||||
parser.AddCommand("Look", Look);
|
||||
|
||||
currentRoom = rooms[curY][curX];
|
||||
player.ChangeRoom(currentRoom.GetID());
|
||||
currentRoom.Print();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Game::ChangeRoom(Direction dir) { currentRoom.Print(); }
|
||||
|
||||
void Game::Run() {
|
||||
while (isActive) {
|
||||
parser.Parse();
|
||||
@@ -49,3 +50,51 @@ int Quit(std::string n) {
|
||||
isActive = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int Move(std::string c) {
|
||||
|
||||
std::string dirInput;
|
||||
printf("What Direction: ");
|
||||
std::cin >> dirInput;
|
||||
dirInput[0] = toupper(dirInput[0]);
|
||||
|
||||
Direction dir = directions[dirInput.data()];
|
||||
|
||||
if (!Game::instance().GetCurrentRoom().ValidDirection(dir)) {
|
||||
printf("Sorry, can't move that direction kiddo \n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
switch (dir) {
|
||||
case N:
|
||||
curY += 1;
|
||||
break;
|
||||
|
||||
case S:
|
||||
curY -= 1;
|
||||
break;
|
||||
|
||||
case E:
|
||||
curX += 1;
|
||||
break;
|
||||
|
||||
case W:
|
||||
curX -= 1;
|
||||
break;
|
||||
default:
|
||||
printf("Incorrect direction \n");
|
||||
return 0;
|
||||
break;
|
||||
};
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int Look(std::string c) {
|
||||
Room cur = Game::instance().GetCurrentRoom();
|
||||
cur.Print();
|
||||
cur.PrintAvalibleExits();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
+16
-30
@@ -35,6 +35,22 @@ void Room::Print() const {
|
||||
printf("%s\n", description.c_str());
|
||||
}
|
||||
|
||||
void Room::PrintAvalibleExits() {
|
||||
printf("Available exits are: \n");
|
||||
for (const auto &exit: exits) {
|
||||
printf(directionChar[exit.dir] + '\n');
|
||||
}
|
||||
}
|
||||
|
||||
bool Room::ValidDirection(Direction d) {
|
||||
for (const auto &exit: exits) {
|
||||
if (exit.dir == d) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void from_json(const json &j, Exit &e) {
|
||||
j.at("exitID").get_to(e.exitID);
|
||||
j.at("dir").get_to(e.dir);
|
||||
@@ -54,33 +70,3 @@ void from_json(const json &j, Room &r) {
|
||||
void to_json(nlohmann::json &j, const Room &r) {
|
||||
j = json{{"id", r.id}, {"name", r.name}, {"description", r.description}, {"exits", r.exits}};
|
||||
}
|
||||
|
||||
int Move(std::string c) {
|
||||
|
||||
char dir;
|
||||
printf("What Direction: ");
|
||||
std::cin >> dir;
|
||||
|
||||
|
||||
switch (dir) {
|
||||
case 'N':
|
||||
curY += 1;
|
||||
break;
|
||||
|
||||
case 'S':
|
||||
curY -= 1;
|
||||
break;
|
||||
|
||||
case 'E':
|
||||
curX += 1;
|
||||
break;
|
||||
|
||||
case 'W':
|
||||
curX -= 1;
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
break;
|
||||
};
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user