-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
203 lines (148 loc) · 6.68 KB
/
main.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
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
// Buğra Ekuklu, 150120016
#include <iostream>
#include <fstream>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <stdexcept>
#include "DarkRed.hpp"
#include "Blue.hpp"
#include "Green.hpp"
#include "List.hpp"
int main(int argc, const char * argv[]) {
try {
std::ifstream file("deck.txt");
std::string inputString;
file.seekg(0, std::ios::end);
inputString.reserve(file.tellg());
file.seekg(0, std::ios::beg);
inputString.assign((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
std::cout << "PLAYERS: " << std::endl << inputString << std::endl;
List<char> firstPlayer;
List<char> secondPlayer;
for (unsigned int i = 0; i < inputString.length() / 2; ++i) {
if (i % 2 == 0) {
firstPlayer.append(inputString[i]);
} else if (inputString[i] != ' ') {
throw std::runtime_error("Expected blankspace after card identifier.");
}
}
for (unsigned long i = ((inputString.length() / 2) + 1); i < inputString.length(); ++i) {
if (i % 2 == 0) {
secondPlayer.append(inputString[i]);
} else if (inputString[i] != ' ') {
throw std::runtime_error("Expected blankspace after card identifier.");
}
}
std::cout << "Score 1: ";
List<GameResult> firstPlayerResult;
for (unsigned int i = 0; i < firstPlayer.count(); ++i) {
Base *challengersCard = nullptr;
switch (firstPlayer[i]) {
case 'R':
challengersCard = new Red();
break;
case 'D':
challengersCard = new DarkRed();
break;
case 'B':
challengersCard = new Blue();
break;
case 'G':
challengersCard = new Green();
break;
default:
throw std::runtime_error("Unknown card identifier.");
}
Base *opponentsCard = nullptr;
switch (secondPlayer[i]) {
case 'R':
opponentsCard = new Red();
break;
case 'D':
opponentsCard = new DarkRed();
break;
case 'B':
opponentsCard = new Blue();
break;
case 'G':
opponentsCard = new Green();
break;
default:
throw std::runtime_error("Unknown card identifier.");
}
firstPlayerResult.append(challengersCard->compare(*opponentsCard));
std::cout << static_cast<unsigned int>(firstPlayerResult[firstPlayerResult.count() - 1]) << " ";
delete challengersCard;
delete opponentsCard;
}
std::cout << std::endl << "Score 2: ";
List<GameResult> secondPlayerResult;
for (unsigned int i = 0; i < firstPlayer.count(); ++i) {
Base *challengersCard = nullptr;
switch (secondPlayer[i]) {
case 'R':
challengersCard = new Red();
break;
case 'D':
challengersCard = new DarkRed();
break;
case 'B':
challengersCard = new Blue();
break;
case 'G':
challengersCard = new Green();
break;
default:
throw std::runtime_error("Unknown card identifier.");
}
Base *opponentsCard = nullptr;
switch (firstPlayer[i]) {
case 'R':
opponentsCard = new Red();
break;
case 'D':
opponentsCard = new DarkRed();
break;
case 'B':
opponentsCard = new Blue();
break;
case 'G':
opponentsCard = new Green();
break;
default:
throw std::runtime_error("Unknown card identifier.");
}
secondPlayerResult.append(challengersCard->compare(*opponentsCard));
std::cout << static_cast<unsigned int>(secondPlayerResult[secondPlayerResult.count() - 1]) << " ";
delete challengersCard;
delete opponentsCard;
}
std::cout << std::endl;
unsigned int firstPlayerPoint = 0;
unsigned int firstPlayerCounters[3] = {0};
for (unsigned int i = 0; i < firstPlayerResult.count(); ++i) {
firstPlayerPoint += static_cast<unsigned int>(firstPlayerResult[i]);
firstPlayerCounters[static_cast<unsigned int>(firstPlayerResult[i])] += 1;
}
unsigned int secondPlayerPoint = 0;
unsigned int secondPlayerCounters[3] = {0};
for (unsigned int i = 0; i < secondPlayerResult.count(); ++i) {
secondPlayerPoint += static_cast<unsigned int>(secondPlayerResult[i]);
secondPlayerCounters[static_cast<unsigned int>(secondPlayerResult[i])] += 1;
}
std::cout << "Total score 1: Lose: " << firstPlayerCounters[0] << " Tie: " << firstPlayerCounters[1] << " Win: " << firstPlayerCounters[2] << std::endl
<< "Total score 2: Lose: " << secondPlayerCounters[0] << " Tie: " << secondPlayerCounters[1] << " Win: " << secondPlayerCounters[2] << std::endl;
if (firstPlayerPoint > secondPlayerPoint) {
std::cout << "Winner: Player1" << std::endl;
} else if (firstPlayerPoint < secondPlayerPoint) {
std::cout << "Winner: Player2" << std::endl;
} else {
std::cout << "Winner: Tie" << std::endl;
}
file.close();
} catch (std::exception &err) {
std::cout << "Error occurred: " << err.what() << std::endl;
}
return 0;
}