Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
// Handhouse.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <string>
#include <vector>
#include "Player.h"
//#include "Looping.h"
using namespace std;
#include <iostream>
#include <string>
#include <vector> // For the command handling function.
#include <cctype> // Will be used to eliminate case sensitivity problems.
using namespace std;
enum allDirections { north, south, west, east };
enum allRooms { kitchen, bedroom, bathroom, hall };
enum allVerbs { take, drop, use, examine, inventory, look };
const int wrong = -1;
const int directions = 4;
const int ROOMS = 4;
const int VERBS = 8;
struct word
{
string word;
int code;
};
struct room
{
string description;
int doors[directions];
};
void set_rooms(room* rms)
{
rms[bathroom].description.assign("bathroom");
rms[bathroom].doors[north] = wrong;
rms[bathroom].doors[east] = wrong;
rms[bathroom].doors[south] = kitchen;
rms[bathroom].doors[west] = wrong;
rms[bedroom].description.assign("bedroom");
rms[bedroom].doors[north] = wrong;
rms[bedroom].doors[east] = wrong;
rms[bedroom].doors[south] = hall;
rms[bedroom].doors[west] = wrong;
rms[kitchen].description.assign("kitchen");
rms[kitchen].doors[north] = bathroom;
rms[kitchen].doors[east] = hall;
rms[kitchen].doors[south] = wrong;
rms[kitchen].doors[west] = wrong;
rms[hall].description.assign("hall");
rms[hall].doors[north] = bedroom;
rms[hall].doors[east] = wrong;
rms[hall].doors[south] = wrong;
rms[hall].doors[west] = kitchen;
}
void set_directions(word* dir)
{
dir[north].code = north;
dir[north].word = "north";
dir[east].code = east;
dir[east].word = "east";
dir[south].code = south;
dir[south].word = "south";
dir[west].code = west;
dir[west].word = "west";
}
void set_verbs(word* vbs)
{
// enum allVerbs {take, drop, use, OPEN, CLOSE, examine, inventory, look};
vbs[take].code = take;
vbs[take].word = "take";
vbs[drop].code = drop;
vbs[drop].word = "drop";
vbs[use].code = use;
vbs[use].word = "use";
vbs[examine].code = examine;
vbs[examine].word = "examine";
vbs[inventory].code = inventory;
vbs[inventory].word = "inventory";
vbs[look].code = look;
vbs[look].word = "look";
}
void section_command(string Cmd, string& wd1, string& wd2)
{
string sub_str;
vector<string> words;
char search = ' ';
size_t i, j;
for (i = 0; i < Cmd.size(); i++)
{
if (Cmd.at(i) != search)
{
sub_str.insert(sub_str.end(), Cmd.at(i));
}
if (i == Cmd.size() - 1)
{
words.push_back(sub_str);
sub_str.clear();
}
if (Cmd.at(i) == search)
{
words.push_back(sub_str);
sub_str.clear();
}
}
// Very simple. For the moment I only want the first to words at most (verb / noun).
if (words.size() == 0)
{
cout << "No command given" << endl;
}
if (words.size() == 1)
{
wd1 = words.at(0);
}
if (words.size() == 2)
{
wd1 = words.at(0);
wd2 = words.at(1);
}
if (words.size() > 2)
{
cout << "Command too long. Only type one or two words (direction or verb and noun)" << endl;
}
}
//all available doors
void look_around(int loc, room* rms, word* dir)
{
int i;
cout << "You are in a " << rms[loc].description << "." << endl;
for (i = 0; i < directions; i++)
{
if (rms[loc].doors[i] != wrong)
{
cout << "There is an exit " << dir[i].word << " to a " << rms[rms[loc].doors[i]].description << "." << endl;
}
}
}
bool parser(int& loc, string wd1, string wd2, word* dir, word* vbs, room* rms)
{
int i;
for (i = 0; i < directions; i++)
{
if (wd1 == dir[i].word)
{
if (rms[loc].doors[dir[i].code] != wrong)
{
loc = rms[loc].doors[dir[i].code];
cout << "I am now in a " << rms[loc].description << "." << endl;
return true;
}
else
{
cout << "No exit that way." << endl;
return true;
}
}
}
// verb section
int VERB_ACTION = wrong;
for (i = 0; i < VERBS; i++)
{
if (wd1 == vbs[i].word)
{
VERB_ACTION = vbs[i].code;
break;
}
}
if (VERB_ACTION == look)
{
look_around(loc, rms, dir);
return true;
}
if (VERB_ACTION == wrong)
{
cout << "No valid command entered." << endl;
return true;
}
return false;
}
//This is the main function from where the other functions are called.
int main()
{
Game game;
//game.RunFunctions();
string command;
string word_1;
string word_2;
room rooms[ROOMS];
set_rooms(rooms);
word directions[directions];
set_directions(directions);
word verbs[VERBS];
set_verbs(verbs);
int location = kitchen; // enumrator 0.
while (word_1 != "quit")
{
string command;
cout << "What is your next move? ";
cin >> command;
cout << command << endl;
word_1.clear();
word_2.clear();
// Call the function that handles the command line format.
section_command(command, word_1, word_2);
// Call the parser.
if (word_1 != "quit")
{
parser(location, word_1, word_2, directions, verbs, rooms);
}
}
return 0;
}