-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.cpp
144 lines (128 loc) · 3.01 KB
/
board.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
#include <board.h>
Board::Board() {
// new Board(10, 10, 25, 25 ,25, 25, 0);
}
Board::Board(int x,
int y,
int share1,
int share2,
int share3,
int share4,
int talkMode) {
if (share1 + share2 + share3 + share4 != 100) {
throw std::invalid_argument("Shares do not add up to 100 percent");
}
encounter = new Encounter();
xDim = x;
yDim = y;
talkingMode = talkMode;
citizens = new Citizen[x * y];
setParties(share1, share2, share3, x * y);
}
Board::~Board() {
delete encounter;
delete[] citizens;
}
void Board::setParties(int share1, int share2, int share3, int size) {
int p1 = share1 * (size / 100);
int p2 = p1 + share2 * (size / 100);
int p3 = p2 + share3 * (size / 100);
for (int i = 0; i < size; i++) {
int party;
if (i < p1) {
party = 0;
} else if (i < p2) {
party = 1;
} else if (i < p3) {
party = 2;
} else {
party = 3;
}
Citizen* c = new Citizen(party);
citizens[i] = *c;
}
}
void Board::prepareEncounter(int talkMode) {
int size = xDim * yDim;
int index = rand() % size;
Citizen* c1 = &citizens[index];
Citizen* c2;
switch (talkMode) {
case 0:
switch (rand() % 4) {
case 0:
if (index - xDim < 0) {
c2 = &citizens[size - 1 + index - xDim];
} else {
c2 = &citizens[index - xDim];
}
break;
case 1:
if (index + xDim >= size) {
c2 = &citizens[index % xDim];
} else {
c2 = &citizens[index + xDim];
}
break;
case 2:
if (index % xDim == 0) {
c2 = &citizens[index + xDim - 1];
} else {
c2 = &citizens[index - 1];
}
break;
case 3:
if (index % xDim == xDim - 1) {
c2 = &citizens[index - xDim + 1];
} else {
c2 = &citizens[index + 1];
}
break;
}
}
encounter->talk(c1, c2);
}
bool Board::isDictatorship() {
int party = citizens[0].getParty();
int size = xDim * yDim;
for (int i = 0; i < size; i++) {
if (citizens[i].getParty() != party) {
return false;
}
}
return true;
}
void Board::setXDim(int x) {
xDim = x;
}
void Board::setYDim(int y) {
yDim = y;
}
int Board::getXDim() {
return xDim;
}
int Board::getYDim() {
return yDim;
}
Citizen* Board::getCitizens() {
return citizens;
}
void Board::reset(int x,
int y,
int share1,
int share2,
int share3,
int share4,
int talkMode) {
if (share1 + share2 + share3 + share4 != 100) {
throw std::invalid_argument("Shares do not add up to 100 percent");
}
setXDim(x);
setYDim(y);
setParties(share1, share2, share3, xDim * yDim);
}
namespace talk_modes {
const unsigned int north_south_east_west = 0;
const unsigned int diagonal = 1;
const unsigned int all = 2;
} // namespace talk_modes