-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpttournament.cpp
368 lines (338 loc) · 10.7 KB
/
rpttournament.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "rpttournament.h"
#include "ui_rpttournament.h"
rpttournament::rpttournament(QWidget *parent) ://构造函数
QMainWindow(parent),
ui(new Ui::rpttournament)
{
ui->setupUi(this);
ui->startgame->hide();
ui->tolastpage->hide();
ui->elim->hide();
ui->repro->hide();
ui->role->hide();
ui->groupBox->hide();
ui->stackedWidget->hide();
ui->nextpage->hide();
ui->showrole->hide();
ui->setround->setMinimum(1); // 设置滑动条的最小值
ui->setround->setMaximum(50); // 设置滑动条的最大值
ui->setround->setValue(10); // 设置滑动条初始值
ui->setelim->setMinimum(1);
ui->setelim->setMaximum(10);
ui->setelim->setValue(5);
ui->setmistake->setMinimum(0);
ui->setmistake->setMaximum(50);
ui->setmistake->setValue(5);
ui->setround->setPageStep(1);
ui->setelim->setPageStep(1);
ui->setmistake->setPageStep(1);
ui->doublewin->setRange(-10,10);
ui->doublewin->setValue(0);
ui->doublewin->stepBy(1);
ui->doublewin->setWrapping(true);
ui->doublelose->setRange(-10,10);
ui->doublelose->setValue(0);
ui->doublelose->stepBy(1);
ui->doublelose->setWrapping(true);
ui->win->setRange(-10,10);
ui->win->setValue(0);
ui->win->stepBy(1);
ui->win->setWrapping(true);
ui->lose->setRange(-10,10);
ui->lose->setValue(0);
ui->lose->stepBy(1);
ui->lose->setWrapping(true);
}
rpttournament::~rpttournament()
{
delete ui;
}
bool rpttournament::comp( player* a, player*b){//自定义比较函数
return a->my_score<b->my_score;
}
void rpttournament::refresh(player*a){//刷新对象初始参数
if(a->getname()=="copycat"){
a->myact = 1;
}
else if(a->getname()=="grudger"){
a->evercheated = false;
a->myact = 1;
}
else if(a->getname()=="simple"){
a->mylastchoie = 1;
a->myact = 1;
}
else if(a->getname()=="duck"){
a->myact = 1;
a->twocheat = 0;
}
}
void rpttournament::paintEvent(QPaintEvent*event){//画背景
QPixmap pixmap = QPixmap(":/image/white.png").scaled(this->size());
QPalette palette;
palette.setBrush(backgroundRole(), QBrush(pixmap));
setPalette(palette);
}
void rpttournament::on_pushButton_clicked()
{
ui->pushButton->hide();
ui->textBrowser->hide();
ui->stackedWidget->show();
ui->nextpage->show();
ui->showrole->show();
ui->startgame->show();
ui->tolastpage->show();
}
void rpttournament::on_showrole_clicked()//展示游戏规则
{
ui->role->show();
}
void rpttournament::on_nextpage_clicked()//stackedwidget翻页
{
int nIndex = ui->stackedWidget->currentIndex();
nIndex++;
nIndex=nIndex%3;
ui->stackedWidget->setCurrentIndex(nIndex);
}
void rpttournament::on_pushButton_2_clicked()//设置玩家组成
{
int a = ui->cat->text().toInt();
int b = ui->cheater->text().toInt();
int c = ui->pink->text().toInt();
int d = ui->duck->text().toInt();
int f = ui->grudger->text().toInt();
int g = ui->simple->text().toInt();
int h = ui->rotten->text().toInt();
int sum = a+b+c+d+f+g+h;
if(sum!=25){
QMessageBox::information(NULL,"Wrong input!","玩家组成总数错误,请更正输入。");
}else{
n_copycat = a;
n_cheat = b;
n_pink = c;
n_duck = d;
n_grugder = f;
n_simple = g;
n_rotten = h;
QMessageBox::information(NULL,"Congratulations!","设置成功。");
}
qDebug()<<a<<","<<b<<","<<c<<","<<d<<","<<f<<","<<g<<","<<h;
}
void rpttournament::on_rewarddone_clicked()//设置奖励值
{
doublewin_reward = ui->doublewin->value();
doublelose_reward =ui->doublelose->value();
lose_reward = ui->lose->value();
win_reward = ui->win->value();
QMessageBox::information(NULL,"Congratulations!","设置成功。");
qDebug()<<doublewin_reward<<","<<doublelose_reward<<","<<lose_reward<<","<<win_reward;
}
void rpttournament::on_done3_clicked()//设置犯错率,消减人数,对战轮数
{
mis_rate = ui->setmistake->value()*1.0/100.0;
n_eliminate = ui->setelim->value();
round = ui->setround->value();
qDebug()<<round<<","<<n_eliminate<<","<<mis_rate;
ui->showset->setText(QString::number(round)+","+QString::number(n_eliminate)+","+QString::number(mis_rate));
ui->showset->repaint();
QMessageBox::information(NULL,"Congratulations!","设置成功。");
}
void rpttournament::on_startgame_clicked()//按下开始游戏按钮,groupbox显示,
{
pink *pk = new pink[n_pink];
cheater *cht = new cheater[n_cheat];
copycat *cat = new copycat[n_copycat];
grudger *grd = new grudger[n_grugder];
duck *dk = new duck[n_duck];
simple *sp = new simple[n_simple];
rotten *rt = new rotten[n_rotten];
player*pl[]={pk,cht,cat,grd,dk,sp,rt};
int num[]={n_pink,n_cheat,n_copycat,n_grugder,n_duck,n_simple,n_rotten};
for(int i = 0;i < 7;i ++){
for(int j = 0;j < num[i];j ++){
list.append(&pl[i][j]);
}
}
ui->groupBox->show();
ui->startgame->hide();
ui->nextpage->hide();
ui->stackedWidget->hide();
showresult();
}
void rpttournament::game(){//一局比赛,所有人两两对决round轮
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
int k ,j;
//qDebug()<<"开始对局,列表长度:"<<list.length();
for(k = 0;k < 24;k ++){//前24个(0~23)玩家都可能是玩家a
for(j = k + 1;j < 25;j ++){//玩家b
player *a = list[k];
player *b = list[j];
qDebug()<<"获取双方姓名:"<<a->getname()<<" vs "<<b->getname();
bool ahistory(false) ,bhistory(false);
for(int i = 0;i < round;i ++){//两名玩家进行round轮对决
if(i >= 1){
a->act(ahistory);
b->act(bhistory);
}
int x = qrand()%100;
bool a_real_act,b_real_act;
if(x<mis_rate*100){//犯错
a_real_act = !a->getact();
}else{//正常决策
a_real_act = a->getact();
}
int y = qrand()%100;
if(y<mis_rate*100){//犯错
b_real_act = !b->getact();
}else{//正常决策
b_real_act = b->getact();
}
ahistory = b_real_act;
bhistory = a_real_act;
if(a_real_act){
if(b_real_act){
a->my_score+=doublewin_reward;
b->my_score+=doublewin_reward;
qDebug()<<"win/win";
}else{
a->my_score+=lose_reward;
b->my_score+=win_reward;
qDebug()<<"lose/win";
}
}else{
if(b_real_act){
a->my_score+=win_reward;
b->my_score+=lose_reward;
qDebug()<<"win/lose";
}else{
a->my_score+=doublelose_reward;
b->my_score+=doublelose_reward;
qDebug()<<"lose/lose";
}
}
}
//两名玩家进行round轮对决后,要将其进行参数复原。
qDebug()<<"refresh "<<k<<","<<j;
refresh(a);
refresh(b);
}
}
qSort(list.begin(), list.end(), comp);
//所有玩家对局完后,显示结果
qDebug()<<"进行显示";
showresult();
qDebug()<<"显示完成";
}
void rpttournament::showresult(){//展示结果
QFont font;
font.setPointSize(12);//字体大小
ui->showall->clear();
ui->showall->repaint();
ui->showscore->clear();
ui->showscore->repaint();
ui->showall->setFont(font);
ui->showscore->setFont(font);
for(int i = 0;i < list.length();i ++){
ui->showall->insertPlainText(list[i]->getchinesename()+",");
ui->showscore->insertPlainText(QString::number(list[i]->my_score)+",");
}
ui->showall->repaint();
ui->showscore->repaint();
qDebug()<<"显示完成,列表长度:"<<list.length();
}
void rpttournament::eliminate(){//淘汰函数
for(int i = 0;i < n_eliminate;i ++){
list.removeAt(0);
}
for(int i = 0;i < list.length();i ++){
list[i]->my_score = 0;
}
showresult();
}
void rpttournament::reproduce(){//繁殖函数
QString winners[n_eliminate];
QHash<QString,int> hashtable;
for(int i =0;i < n_eliminate;i ++){
winners[i] = list[25-2*n_eliminate+i]->getname();
if(hashtable.find(winners[i])==hashtable.end()){
hashtable.insert(winners[i],1);
}else hashtable.find(winners[i]).value()++;
}
QHash<QString, int>::const_iterator it;
for(it=hashtable.constBegin();it!=hashtable.constEnd(); ++it){
if(it.key()=="copycat"){
copycat *obj=new copycat[it.value()];
for(int i = 0;i < it.value();i ++){
list.append(&obj[i]);
}
}
else if(it.key()=="pink"){
pink *obj=new pink[it.value()];
for(int i = 0;i < it.value();i ++){
list.append(&obj[i]);
}
}
else if(it.key()=="duck"){
duck *obj=new duck[it.value()];
for(int i = 0;i < it.value();i ++){
list.append(&obj[i]);
}
}
else if(it.key()=="grudger"){
grudger *obj=new grudger[it.value()];
for(int i = 0;i < it.value();i ++){
list.append(&obj[i]);
}
}
else if(it.key()=="cheater"){
cheater *obj=new cheater[it.value()];
for(int i = 0;i < it.value();i ++){
list.append(&obj[i]);
}
}
else if(it.key()=="simple"){
simple *obj=new simple[it.value()];
for(int i = 0;i < it.value();i ++){
list.append(&obj[i]);
}
}
else if(it.key()=="rotten"){
rotten *obj=new rotten[it.value()];
for(int i = 0;i < it.value();i ++){
list.append(&obj[i]);
}
}
}
showresult();
}
void rpttournament::on_end_clicked()
{
list.clear();
ui->groupBox->hide();
ui->startgame->show();
ui->nextpage->show();
ui->stackedWidget->show();
}
void rpttournament::on_game_clicked()
{
game();
ui->game->hide();
ui->elim->show();
}
void rpttournament::on_elim_clicked()
{
eliminate();
ui->elim->hide();
ui->repro->show();
}
void rpttournament::on_repro_clicked()
{
reproduce();
ui->repro->hide();
ui->game->show();
}
void rpttournament::on_tolastpage_clicked()
{
lp->show();
this->hide();
}