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
+32
View File
@@ -0,0 +1,32 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/Text-Adventure",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
+2 -2
View File
@@ -34,8 +34,8 @@ class Creature
bool IsAlive() { return GetHealth() > 0; }
void PrintDescription() { printf("%s\n", description); }
void PrintName() { printf("%s\n", name); }
void PrintDescription() { printf("%s\n", description.data()); }
void PrintName() { printf("%s\n", name.data()); }
virtual ~Creature() = default;
};
+3 -3
View File
@@ -15,8 +15,8 @@ void PlayerEditor::Edit()
{
printf("Options: %s\n");
for (auto &command : commands.begin())
/*for (const auto &command : commands.begin())
{
printf("%s\n", command.c_str());
}
printf("%s\n", command);
}*/
}
+5 -2
View File
@@ -26,6 +26,8 @@ private:
nlohmann::json json;
fs::path filePath = "Resources/Rooms.json";
std::ifstream stream;
inline static Game *game_;
// Likely need to make sure Player is ADDED to room
@@ -44,13 +46,14 @@ public:
static void SetIsActive(const bool active) { isActive = active; }
static Game &instance() {
static Game *game_;
static Game &Instance() {
if (game_ == nullptr) {
game_ = new Game();
}
return *game_;
}
~Game() = default;
};
// Commands
-7
View File
@@ -1,7 +0,0 @@
#pragma once
#include <iostream>
#include <iomanip>
#include "object.h"
class Scenery : Object
+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()