Fixed several issues with Parser and enabled room navigation

This commit is contained in:
2026-08-21 20:02:11 -05:00
parent b1e87868c8
commit 3e50d46284
9 changed files with 91 additions and 43 deletions
+1 -6
View File
@@ -1,6 +1,5 @@
#include "CommandParser.h"
#include <algorithm>
#include <cstring>
#include <iostream>
#include <sstream>
@@ -24,10 +23,6 @@ std::vector<std::string> CommandParser::Verb(std::stringstream &sentences) {
}
}
if (verbs.empty()) {
CommandUnrecognized();
}
return verbs;
}
@@ -68,7 +63,7 @@ int CommandParser::Parse() {
for (const auto &verb: verbs) {
if (!commands[verb]) {
printf("Sorry kid, can't understand ya! \n");
CommandUnrecognized();
break;
}
toRun = commands[verb];
+44 -21
View File
@@ -1,4 +1,6 @@
#include "game.h"
#include <cstdio>
#include <filesystem>
#include <fstream>
bool isActive;
@@ -20,6 +22,17 @@ bool Game::Init() {
rooms[i].resize(MAP_WIDTH);
}
filePath = fs::absolute(filePath);
if (!fs::exists(filePath)) {
printf("File Read error! Could not find Json at: \n");
printf("%s", filePath.c_str());
isActive = false;
return false;
}
// Read rooms into buffer
stream.open(absolute(filePath));
json = nlohmann::json::parse(stream);
@@ -32,9 +45,7 @@ bool Game::Init() {
parser.AddCommand("Move", Move);
parser.AddCommand("Look", Look);
currentRoom = rooms[curY][curX];
player.ChangeRoom(currentRoom.GetID());
currentRoom.Print();
ChangeRoom(rooms[0][0]);
return true;
}
@@ -52,6 +63,8 @@ void Game::ChangeRoom(Room r) {
if (currentRoom != r) {
currentRoom = r;
player.ChangeRoom(currentRoom.GetID());
currentRoom.Print();
printf("\n");
currentRoom.PrintAvalibleExits();
@@ -67,7 +80,7 @@ int Move(const std::string &c) {
std::string dirInput = c;
int tmpX = 0, tmpY = 0;
int tmpX = curX, tmpY = curY;
if (dirInput.empty()) {
printf("What Direction: ");
@@ -76,7 +89,7 @@ int Move(const std::string &c) {
}
const Direction dir = directions[dirInput.data()];
if (!Game::instance().GetCurrentRoom().ValidDirection(dir)) {
if (!Game::Instance().GetCurrentRoom().ValidDirection(dir)) {
printf("Sorry, can't move that direction kiddo \n");
return 0;
}
@@ -84,18 +97,42 @@ int Move(const std::string &c) {
switch (dir) {
case N:
tmpY -= 1;
if (tmpY > 0 && tmpY < MAP_HEIGHT) {
curY = tmpY;
} else {
printf("Ye hit a wall of some kind! \n");
return -1;
}
break;
case S:
tmpY += 1;
if (tmpY > 0 && tmpY < MAP_HEIGHT) {
curY = tmpY;
} else {
printf("Ye hit a wall of some kind! \n");
return -1;
}
break;
case E:
tmpX += 1;
if (tmpX > 0 && tmpX < MAP_WIDTH) {
curX = tmpX;
} else {
printf("Ye hit a wall of some kind! \n");
return -1;
}
break;
case W:
tmpX -= 1;
if (tmpX > 0 && tmpX < MAP_WIDTH) {
curX = tmpX;
} else {
printf("Ye hit a wall of some kind! \n");
return -1;
}
break;
default:
printf("Incorrect direction \n");
@@ -103,26 +140,12 @@ int Move(const std::string &c) {
break;
};
if (tmpX > 0 && tmpX < MAP_WIDTH) {
curX = tmpX;
} else {
printf("Ye hit a wall of some kind! \n");
return -1;
}
if (tmpY > 0 && tmpY < MAP_HEIGHT) {
curY = tmpY;
} else {
printf("Ye hit a wall of some kind! \n");
return -1;
}
Game::instance().ChangeRoom(rooms[curY][curX]);
Game::Instance().ChangeRoom(rooms[curY][curX]);
return 0;
}
int Look(const std::string &c) {
Room cur = Game::instance().GetCurrentRoom();
Room cur = Game::Instance().GetCurrentRoom();
cur.Print();
printf("\n");
cur.PrintAvalibleExits();
+3 -1
View File
@@ -1,4 +1,5 @@
#include "room.h"
#include <cstdio>
#include "game.h"
@@ -38,7 +39,8 @@ void Room::Print() const {
void Room::PrintAvalibleExits() {
printf("Available exits are: \n");
for (const auto &exit: exits) {
printf(directionChar[exit.dir].data(), "\n");
printf(directionChar[exit.dir].c_str());
printf("\n");
}
}
+1 -1
View File
@@ -18,7 +18,7 @@ void Scenery::Interact()
void Scenery::Examine()
{
printf("%s\n", GetDescription());
printf("%s\n", GetDescription().data());
}
Scenery::~Scenery()