-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandProcessingDriver.cpp
More file actions
70 lines (52 loc) · 2.41 KB
/
CommandProcessingDriver.cpp
File metadata and controls
70 lines (52 loc) · 2.41 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
#include "CommandProcessing.h"
#include "GameEngine.h"
#include <iostream>
using std:: cout;
using std:: endl;
using std:: cin;
#include <string>
using namespace std;
#include <fstream>
using std:: ifstream;
int main(){
GameEngine:: instance()->reset();
string answer;
cout<<"\nIf you want to enter commands to the console, type \"-console\". Otherwise, type \"-file <filename>\" to read commands from the file."<<endl;
getline(cin, answer);
// Letting the user use commands by console
if (answer.substr(0, answer.find(" ")) == "-console"){
bool answer = true;
cout<<"\nUser has chosen to use commands through the console."<<endl;
// The object "console" accesses the getCommand function
CommandProcessor* console = new CommandProcessor();
// This while loop lets the getCommand function loop till the game is over
while(!GameEngine:: instance()->getIsGameOver()){
// The getCommand function asks the user for the commands
Command* command = console->getCommand();
// Making sure command object is not null S
if (command != NULL){
// cout<<"\n COMMAND EFFECT: "<<command->getCommandEffect()<<endl;
GameEngine::instance()->transition(command->getCommandEffect());
}
}
// The user chooses for the commands to be read from the commands.txt file
} else {
// Here we are taking only the file name from the command string that the user inputs
// by extracting only what's inside the < > brackets
string fileName = answer.substr(answer.find_first_of("<")+1, answer.find_last_of(">") - answer.find_first_of("<")-1);
cout<<"\nUser has chosen to read commands from file \""<<fileName<<"\""<<endl;
ifstream input;
input.open(fileName);
// The fileCommand object calls the getCommand function
FileCommandProcessorAdapter* fileCommand = new FileCommandProcessorAdapter(fileName);
cout<<"Reading from file:\n"<<endl;
// Reading from the commands.txt file
while (!fileCommand->isEntireFileRead()) {
Command* commandFromFile = fileCommand->getCommand();
if (commandFromFile != NULL) {
GameEngine::instance()->transition(commandFromFile->getCommandEffect());
}
}
cout<<"\nEnd of file reached."<<endl;
}
}