-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_main.cpp
225 lines (194 loc) · 6.11 KB
/
simple_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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include "game_spec.h"
#include "game_tree.h"
using namespace std;
vector< set<int> > k_subsets_set(int k, int num){
vector< set<int> > subsets;
if (k == 1 && num > 0){
for (int i = 0; i < num; i++){
set<int> tuple;
tuple.insert(i);
subsets.push_back(tuple);
}
} else if (num >= k){
subsets = k_subsets_set(k, num - 1);
vector< set<int> > temp_subsets = k_subsets_set(k - 1, num - 1);
for (int i = 0; i < temp_subsets.size(); i++){
temp_subsets[i].insert(num - 1);
subsets.push_back(temp_subsets[i]);
}
}
return subsets;
}
class SimpleRGame {
private:
GameSpec* spec;
double cconf, pconf;
vector< set<int> > subsets;
map<set<int>, double> prob_map;
public:
int mission, rpoints, spoints;
SimpleRGame(GameSpec* spec, double cconf, double pconf){
this->spec = spec;
this->cconf = cconf;
this->pconf = pconf;
mission = 0;
rpoints = 0;
spoints = 0;
subsets = k_subsets_set(spec->num_spies, spec->num_players);
set<int> spy_set;
for (int i = 0; i < subsets.size(); i++){
spy_set = subsets[i];
prob_map[spy_set] = 1.0 / subsets.size();
}
}
void update(vector<int> team, int num_fails){
set<int> spy_set;
double prob_no_fail, prob_fail;
double normalization = 0.0;
for (int i = 0; i < subsets.size(); i++){
spy_set = subsets[i];
if (find_num_spies(spy_set, team) < num_fails){
prob_map[spy_set] = 0;
} else if (num_fails > 0 && find_num_spies(spy_set, team) != num_fails){
prob_map[spy_set] *= (1 - cconf);
} else {
prob_map[spy_set] *= cconf;
}
if (num_fails == 0 && find_num_spies(spy_set, team) > 0){
prob_map[spy_set] *= (1 - pconf);
} else {
prob_map[spy_set] *= pconf;
}
normalization += prob_map[spy_set];
}
for (int i = 0; i < subsets.size(); i++){
spy_set = subsets[i];
prob_map[spy_set] /= normalization;
}
if (num_fails >= spec->wins[mission]){
spoints++;
} else {
rpoints++;
}
mission++;
}
vector<double> player_stats(){
int num_players = spec->num_players;
vector<double> player_probs;
for (int j = 0; j < num_players; j++){
player_probs.push_back(0);
}
set<int> spy_set;
for (int i = 0; i < subsets.size(); i++){
spy_set = subsets[i];
for (int j = 0; j < num_players; j++){
if (spy_set.count(j) > 0){
player_probs[j] += prob_map[spy_set];
}
}
}
return player_probs;
}
vector<double> player_spec_stats(int player){
int num_players = spec->num_players;
vector<double> player_probs;
for (int j = 0; j < num_players; j++){
player_probs.push_back(0);
}
set<int> spy_set;
double normalization = 0.0;
for (int i = 0; i < subsets.size(); i++){
spy_set = subsets[i];
for (int j = 0; j < num_players; j++){
if (spy_set.count(j) > 0 && spy_set.count(player) == 0){
player_probs[j] += prob_map[spy_set];
normalization += prob_map[spy_set];
}
}
}
for (int j = 0; j < num_players; j++){
player_probs[j] *= spec->num_spies / normalization;
}
return player_probs;
}
~SimpleRGame(){
if (!prob_map.empty()){
prob_map.clear();
}
}
};
void print_statistics(SimpleRGame* game, vector<string> player_names){
cout << "****************STATISTICS****************" << endl;
vector<double> stats = game->player_stats();
for (int i = 0; i < player_names.size(); i++){
cout << player_names[i] << ": " << stats[i] << endl;
}
cout << "******************************************" << endl;
}
void print_player_statistics(SimpleRGame* game, vector<string> player_names, string player_name, int player_num){
cout << "************STATISTICS FOR " << player_name << "************" << endl;
vector<double> stats = game->player_spec_stats(player_num);
for (int i = 0; i < player_names.size(); i++){
cout << player_names[i] << ": " << stats[i] << endl;
}
}
int main(){
double coord_confidence = 0.8;
double points_confidence = 0.6;
int num_players = 0;
cout << "Welcome to the Simple Resistance Game Calculator developed by Matthew Brennan and Vincent Kee." << endl;
while (num_players < 5 || num_players > 10){
cout << "Resistance can be played by 5 - 10 people." << endl;
cout << "Please enter the number of players playing: ";
cin >> num_players;
}
GameSpec* game_spec = new GameSpec(num_players);
SimpleRGame* game = new SimpleRGame(game_spec, coord_confidence, points_confidence);
map<string, int> name_map;
vector<string> player_names;
string curr_player_name;
cout << "Please enter the names (space-separated) of the players: ";
for (int i = 0; i < num_players; i++){
cin >> curr_player_name;
name_map[curr_player_name] = i;
player_names.push_back(curr_player_name);
}
int mission = 0, num_fails, num_spec_stats;
vector<int> team;
while (game->rpoints < 3 && game->spoints < 3){
cout << "Please enter the names (space-separated) of the players on the team (" << game_spec->missions[mission] << " players this round): ";
for (int i = 0; i < game_spec->missions[mission]; i++){
cin >> curr_player_name;
team.push_back(name_map[curr_player_name]);
}
sort(team.begin(), team.end());
cout << "Please enter the number of cards failing the mission (at least " << game_spec->wins[mission] << " out of " << game_spec->missions[mission] << " cards must be fails for the mission to fail): ";
cin >> num_fails;
game->update(team, num_fails);
print_statistics(game, player_names);
cout << "Please enter the number of players who would like to know statistics from their perspectives: ";
cin >> num_spec_stats;
if (num_spec_stats != 0){
cout << "Please enter the names of the players who would like to know statistics from their perspectives: ";
for (int i = 0; i < num_spec_stats; i++){
cin >> curr_player_name;
print_player_statistics(game, player_names, curr_player_name, name_map[curr_player_name]);
}
}
team.clear();
mission++;
}
// Case where spies win
if (game->rpoints < 3){
cout << "Spies win!" << endl;
} else { // Case where resistance wins
cout << "Resistance wins!" << endl;
}
delete game_spec;
delete game;
return 0;
}