-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
233 lines (224 loc) · 4.86 KB
/
main.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include "player.h"
#include "textdisplay.h"
using namespace std;
int main(int argc, char *argv[]) {
ifstream init;
string p1deck = "default.deck";
string p2deck = "default.deck";
bool test_mode = false;
// Handle command line arguments
for (int i = 1; i < argc; ++i) {
string arg = argv[i];
if (arg == "-deck1") {
// load deck from file for player 1
++i;
p1deck = argv[i];
}
else if (arg == "-deck2") {
// load deck from file for play 2
++i;
p2deck = argv[i];
}
else if (arg == "-init") {
// load game from file
++i;
init.open(argv[i]);
}
else if (arg == "-testing") {
// enable testing mode
test_mode = true;
}
else {}
}
// initialize game
string p1name, p2name;
cout << "What is Player 1's name?" << endl;
if (init >> p1name) {}
else {
cin >> p1name;
}
cout << "What is Player 2's name?" << endl;
if (init >> p2name) {}
else {
cin >> p2name;
}
Player p1;
Player p2;
/* do other proper initialization here
e.g. initialize deck, hand of the players */
p1.init(p1name, p1deck, test_mode, &p2);
p2.init(p2name, p2deck, test_mode, &p1);
// Command interpreter
string cmdline;
string sepcmd;
Player *curr_player = &p1;
Player *other_player = &p2;
while (true) {
istringstream iss;
if (getline(init, cmdline)) {
iss.str(cmdline);
}
else {
getline(cin, cmdline);
iss.str(cmdline);
}
if (cin.eof()) {
break;
}
try {
iss >> sepcmd;
if (sepcmd == "help") {
//display help message
ifstream helpfile{"help.txt"};
string s;
while(getline(helpfile, s)) {
cout << s << endl;
}
}
else if (sepcmd == "end") {
// end the current player's turn
curr_player->end_turn();
// the next player's turn starts
Player *tmp = curr_player;
curr_player = other_player;
other_player = tmp;
curr_player->start_turn();
}
else if (sepcmd == "quit") {
// terminate the game
break;
}
else if (sepcmd == "attack") {
// attack player or enemy's minion
// cin minion(s)
int minion;
int other_minion;
// call attack method
if (iss >> minion) {
if (iss >> other_minion) {
curr_player->attack_minion(minion-1,other_minion-1);
}
else {
curr_player->attack_player(minion-1);
}
}
else {
continue;
}
}
else if (sepcmd == "play") { //play cards on the hand
// do proper cin stuff here
int card;
int player;
char other_card;
if (iss >> card) {
if (iss >> player) {
Player *other;
if (player == 1) {
other = &p1;
}
else if (player == 2) {
other = &p2;
}
else {
throw "Invalid Input";
}
if (iss >> other_card) {
if (other_card == 'r') {
curr_player->play_on_ritual(card-1, *other);
}
else {
int other_card_int = other_card - '0';
curr_player->play_on_minion(card-1, *other, other_card_int-1);
}
}
}
else {
curr_player->play(card-1);
}
}
}
else if (sepcmd == "use") { // use ability of a minion currently ON THE FIELD
// do proper cin stuff here
int card;
int player;
char other_card;
if (iss >> card) {
if (iss >> player) {
Player *other;
if (player == 1) {
other = &p1;
}
else if (player == 2) {
other = &p2;
}
else {
throw "Invalid Input";
}
if (iss >> other_card) {
if (other_card == 'r') {
curr_player->use_on_ritual(card-1, *other);
}
else {
int other_card_int = other_card - '0';
curr_player->use_on_minion(card-1, *other, other_card_int-1);
}
}
}
else {
curr_player->use(card-1);
}
}
}
else if (sepcmd == "inspect") { // print information of the card
// do proper cin stuff here
int i;
iss >> i;
// calls inspect method
inspect(*curr_player, i-1);
}
else if (sepcmd == "hand") {
// calls displayhand method from textdisplay
displayhand(*curr_player);
}
else if (sepcmd == "board") {
// calls displayboard method from textdisplay
displayboard(p1, p2);
}
// TESTING FEATURES
else if (sepcmd == "draw") {
// draw a card from the deck
curr_player->draw();
}
else if (sepcmd == "discard") {
// do proper cin stuff here
int card;
iss >> card;
// remove a card from the player's hand
curr_player->discard(card-1);
}
else {}
// check win condition
if (p1.isDead()) {
cout << p2.get_name() << " wins!" << endl;
break;
}
if (p2.isDead()) {
cout << p1.get_name() << " wins!" << endl;
break;
}
}
catch (char const* &ErrorMsg) {
cout << ErrorMsg << endl;
continue;
}
catch (string &ErrorMsg) {
cout << ErrorMsg << endl;
continue;
}
}
}