-
Notifications
You must be signed in to change notification settings - Fork 0
/
specker.cpp
269 lines (250 loc) · 6.12 KB
/
specker.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <iostream>
#include <stdexcept>
using namespace std;
class Move{
public:
Move(int sh, int sc, int th, int tc){
soheap=sh;
socoins=sc;
tarheap=th;
tarcoins=tc;
};
int getSource() const{
return soheap;
};
int getSourceCoins() const{
return socoins;
};
int getTarget() const{
return tarheap;
};
int getTargetCoins() const{
return tarcoins;
};
friend ostream & operator << (ostream &out, const Move &move){
out<<"takes "<<move.getSourceCoins()<<" coins from heap "<<move.getSource()<<" and puts ";
if(move.getTargetCoins()!=0) out<<move.getTargetCoins()<<" coins to heap "<<move.getTarget();
else out<<"nothing";
return out;
};
private:
int soheap,socoins,tarheap,tarcoins;
};
class State{
public:
State(int h, const int c[]){
a= new int[heaps];
heaps=h;
for(int i=0;i<h;i++) a[i]=c[i];
};
~State(){
delete [] a;
};
void next(const Move &move) throw(logic_error){
//if kai error
int arxthesi=move.getSource();
int telthesi=move.getTarget();
if(arxthesi<0||arxthesi>=heaps||telthesi<0||telthesi>=heaps)
{
throw logic_error("invalid heap");
}
else if(a[arxthesi]<(move.getSourceCoins())||(move.getTargetCoins())>=(move.getSourceCoins())){
throw logic_error("invalid heap");
}
else{
a[arxthesi]=a[arxthesi]-(move.getSourceCoins());
a[telthesi]=a[telthesi]+(move.getTargetCoins());
}
};
bool winning() const{
for(int j=0;j<heaps;j++){
if(a[j]!=0) return false;
}
return true;
};
int getHeaps() const{
return heaps;
};
int getCoins(int h) const throw(logic_error){
//if kai error
if(h>=heaps||h<0) throw logic_error("Invalid heap");
else
return a[h];
};
friend ostream & operator << (ostream &out,const State &state){
for(int j=0;j<state.heaps-1;j++){
out<<state.a[j]<<", ";
}
out<<state.a[state.heaps-1];
return out;
};
private:
int heaps;
int *a;
};
class Player {
public:
Player(const string &n){
name=n;
};
virtual ~Player(){
name.clear();
};
virtual const string & getType() const = 0;
virtual Move play(const State &s) = 0;
friend ostream & operator << (ostream &out, const Player &player){
out<<player.getType()<<" player "<<player.name;
return out;
};
protected:
string name;
};
class GreedyPlayer: public Player{
public:
GreedyPlayer(const string &n): Player(n){
type="Greedy";
};
virtual const string & getType() const override{
return type;
}
virtual Move play(const State &s) override{
// tarc=0;
// th=0;
int c,max;
c=0;
max=s.getCoins(0);
for(int i=0;i<s.getHeaps()-1;i++){
if(max<s.getCoins(i+1)){
max=s.getCoins(i+1);
c = i+1;
}
}
// sh=c;
// sc=max;
return Move(c,max,0,0);
};
protected:
string type;
};
class SpartanPlayer: public Player{
public:
SpartanPlayer(const string &n): Player(n){
type="Spartan";
};
virtual const string & getType() const override{
return type;
};
virtual Move play(const State &s) override{
int maxIndex = -1;
int maxValue = -1;
for (int i=0; i <s.getHeaps(); i++) {
if (s.getCoins(i) > maxValue) {
maxIndex = i;
maxValue = s.getCoins(i);
}
}
return Move(maxIndex,1,0,0);
};
protected:
string type;
};
class SneakyPlayer: public Player{
public:
SneakyPlayer(const string &n): Player(n){
type="Sneaky";
};
virtual const string & getType() const override{
return type;
};
virtual Move play(const State &s) override{
int c,min;
c=-1;
min=9999999999999999; // s.getCoins(0);
for(int i=0;i<s.getHeaps();i++){
if(s.getCoins(i)<min && s.getCoins(i)>0){
c=i;
min=s.getCoins(i);
}
}
return Move(c,min,0,0);
};
protected:
string type;
};
class RighteousPlayer: public Player{
public:
RighteousPlayer(const string &n): Player(n){
type="Righteous";
};
virtual const string & getType() const override{
return type;
};
virtual Move play(const State &s) override{
int c1,max,min,c2;
c1=-1;
c2=0;
max=-1;
min=s.getCoins(0);
for(int i=0;i<s.getHeaps();i++){
if(max<s.getCoins(i)){
c1=i;
max=s.getCoins(i);
}
if(s.getCoins(i)<min){
c2=i;
min=s.getCoins(i);
}
}
if(max%2!=0) max=(max)+1;
if(min%2!=0) min=(min)+1;
return Move(c1,max/2,c2,(max/2)-1);
};
protected:
string type;
};
class Game{
public:
Game(int heaps,int players){
p = players;
h = heaps;
a = new int [h];
seira = new Player* [p];//gia to play kyklika
HeapCount = 0;
PlayerCount = 0;
};
~Game(){
delete[] a;
delete[] seira;
}
void addHeap(int coins) throw(logic_error){
if( HeapCount> h-1 || coins<0)
throw logic_error("invalid");
else a[HeapCount++]=coins;
};
void addPlayer(Player *player) throw(logic_error){
//elegxos gia error
if(PlayerCount>p-1) throw logic_error("invalid");
else seira[PlayerCount++]=player;
};
void play(ostream &out) throw(logic_error){
//playercount==p error
if(PlayerCount!=p && HeapCount!=h){
throw logic_error("invalid");
return ;
}
int i = 0;
State current(h,a);
while(!current.winning()){
out<<"State: "<<current<<endl;
out<<*seira[i % p]<<" "<<seira[i % p]->play(current)<<endl;
current.next(seira[i % p]->play(current));
i++;
}
out<<"State: "<<current<<endl;
i--;
out<<*seira[i % p]<<" wins"<<endl;
};
private:
int *a,p,h,PlayerCount,HeapCount;
Player **seira;
};