-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.cc
126 lines (106 loc) · 2.53 KB
/
controller.cc
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "controller.h"
#include <fstream>
using namespace std;
//constructor when user has a map input
Controller::Controller(string filename){
fileName = filename;
game = NULL;
view = NULL;
ifs = NULL;
quit = false;
skip = false;
}
//default constructor used
Controller::Controller(){
fileName = "";
game = NULL;
view = NULL;
ifs = NULL;
quit = false;
skip = false;
}
//calls view function to update cell in view
void Controller::updateCell(int row, int col, char ch){
view->updateCell(row, col, ch);
}
void Controller::init(){
if (fileName != "") cout << "Loaded map " << fileName << endl;
cout << "Select your race: ";
char c;
cin >> c;
view = new View(ROWS, COLS, c);
if (fileName != "") { //initalizes game either with or without a file
this->ifs = new ifstream(fileName.c_str());
game = new Game(ifs, c, this);
}
else game = new Game(c, this);
}
//displays different messages based on status of the game;
//offers player chance to play again
void Controller::gameOver(bool won, int gold){
//tells you whether you won or lost and whether you
//would like to play the game again
while (1){
if (won) cout << "You won!";
else cout << "You Lose!";
cout << endl << "Your score was " << gold << "." << endl;
cout << "Play Again?(y/n) ";
string output;
cin >> output;
if (output == "y"){
delete game;
delete view;
delete ifs;
init();
return;
} else if (output == "n") {
quit = true;
return;
}
}
}
//starts the game and responds based on user input
void Controller::play(){
init();
while(1){ //main command loop
string s;
if (!quit){
view->printout();
cin >> s;
}
//if user wants to quit the game
if (s == "q" || quit || cin.eof()){
break;
} else if (s == "r"){
delete game;
delete view;
delete ifs;
init();
} else if (s == "p"){
view->printout();
}
//input for player turn
else {
game->playerTurn(s);
if (skip) skip = false;
else game->enemyTurn();
}
}
if (game != NULL) delete game;
if (view != NULL) delete view;
if (ifs != NULL) delete ifs;
}
//updates view's status
void Controller::updateStatus(int hp, int gold, int atk, int def, int floor){
view->updateStatus(hp, gold, atk, def, floor);
}
//updates view's action output
void Controller::updateAction(std::string s){
view->updateAction(s);
}
//set skip to true
void Controller::skipEnemyTurn(){
skip = true;
}
//destructor
Controller::~Controller(){}