-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
106 lines (93 loc) · 2.44 KB
/
Copy pathmain.cpp
File metadata and controls
106 lines (93 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* main.cpp
*
* Created on: 29 août 2013
* Author: jgintrand
*/
#include <Character.h>
#include <LivingBeing.h>
#include <Main.h>
#include <Map.h>
#include <Room.h>
#include <RustedIronBlade.h>
#include <sys/types.h>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <map>
#include <new>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#define _MAX_PATH 100
using namespace std;
void StringExplode(string str, string separator, vector<string>* results);
static void Initialize();
typedef void(Main::*f_ptr)(std::vector<std::string>);
static std::map<std::string, f_ptr> mapString;
int main()
{
srand (time(NULL));
Character hero("Kebree");
hero.setWeapon(new RustedIronBlade());
Main m;
m.setHero(&hero);
Initialize();
while(!m.isEnded()) {
cout << "# ";
char action[_MAX_PATH];
cin.getline(action, _MAX_PATH);
string s(action);
vector<string> commandLine;
StringExplode(action, " ", &commandLine);
vector<string> args;
for(unsigned int i=1; i<commandLine.size(); i++){
args.push_back(commandLine[i]);
}
try {
if(mapString.count(commandLine.front()) > 0)
(m.*(mapString[commandLine.front()]))(args);
else
cout << "Command not found" << endl;;
} catch (const out_of_range& oor) {
cout << "Command not found" << endl;
} catch (const std::exception& e) {
cerr << e.what() <<endl;
}
if(m.turnEnd()){
hero.getMap()->getRoom()->cpuPlay(&hero);
if(hero.is_dead()) {
cout << "You are dead !" <<endl;
cout << hero.getStatus() <<endl;
return 0;
}
}
}
}
static void Initialize()
{
mapString["help"] = &Main::displayHelp;
mapString["quit"] = &Main::quit;
mapString["status"] = &Main::status;
mapString["go"] = &Main::changeRoom;
mapString["look"] = &Main::look;
mapString["hit"] = &Main::hit;
mapString["spend"] = &Main::spend;
mapString["use"] = &Main::useItem;
mapString["inventory"] = &Main::displayInventory;
}
void StringExplode(string str, string separator, vector<string>* results){
unsigned int found;
found = str.find_first_of(separator);
while(found != string::npos){
if(found > 0){
results->push_back(str.substr(0,found));
}
str = str.substr(found+1);
found = str.find_first_of(separator);
}
if(str.length() > 0){
results->push_back(str);
}
}