-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
79 lines (69 loc) · 1.85 KB
/
Source.cpp
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
#include <queue>
#include <vector>
#include <memory>
#include "Play.h"
#include "Player.h"
using namespace std;
//Enumeration for return type of main function
enum {
SUCCESS = 0,
FAIL_WRONG_ARGUMENTS = 1,
FAIL_FILE_OPEN = 2
};
int main(int argc, char* argv[]) {
if (argc < 2) {
//Check for the command line usage
cerr << "ERROR: usage: " << argv[0] << "<configuration_file_name>" << endl;
return FAIL_WRONG_ARGUMENTS;
}
string playName;
string currentLine;
queue<SingleLine> information;
ifstream config(argv[1]);
if (!config.is_open()) {
cerr << "ERROR: Open file fail: " << argv[1] << endl;
return FAIL_FILE_OPEN;
}
int characterCount = 0;
bool nameFound = false;
while (getline(config, currentLine)) {
if (currentLine.size()>0) {
if (!nameFound) {
playName = currentLine;
nameFound = true;
}
else {
//Transfer the every single line into istringstream
istringstream iss(currentLine);
string character, path;
iss >> character >> path;
SingleLine temp(character, path);
information.push(temp);
characterCount++;
}
}
}
Play play(playName);
//Use the unique_ptr to preserve the Player object
vector<unique_ptr<Player>> players;
for (int i = 1; i <= characterCount; i++) {
ifstream tempInFile(information.front().second);
if (!tempInFile.is_open()) {
cerr << "ERROR: Open file fail: " << information.front().second << endl;
return FAIL_FILE_OPEN;
}
//Use new to pass a right value's address to a unique_ptr
unique_ptr<Player> p(new Player(play, information.front().first, tempInFile));
//Here use the move to transfer the move to vector
players.push_back(move(p));
players.back()->enter();
information.pop();
}
auto iter = players.begin();
while (iter != players.end()) {
//players' iterator stores a pointer to Player object
(*iter)->exit();
iter++;
}
return SUCCESS;
}