-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsession.cpp
More file actions
165 lines (147 loc) · 5.29 KB
/
session.cpp
File metadata and controls
165 lines (147 loc) · 5.29 KB
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
/*
doko is a C++ doppelkopf program with an integrated UCT player.
Copyright (c) 2011-2016 Silvan Sievers
For questions, please write to: silvan.sievers@unibas.ch
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "session.h"
#include "actual_game_state.h"
#include "game_type.h"
#include "human_player.h"
#include "options.h"
#include "random_player.h"
#include "rng.h"
#include "timer.h"
#include "uct_player.h"
#include <algorithm>
#include <cassert>
#include <iostream>
using namespace std;
Session::Session(Options &options_)
: options(options_),
rng(options.get_random_seed()),
first_player(0),
vorfuehrung(false) {
timer = new Timer();
Cards::setup_bit_count();
play();
cout << "time: " << *timer << endl;
}
Session::~Session() {
for (int i = 0; i < 4; ++i)
delete players[i];
delete timer;
}
void Session::play() {
cout << "starting doppelkopf session" << endl << endl;
options.dump();
const vector<player_t> &players_types = options.get_players_types();
for (size_t i = 0; i < 4; ++i) {
switch (players_types[i]) {
case UCT:
players[i] = new UctPlayer(i, options);
break;
case HUMAN:
players[i] = new HumanPlayer(i);
break;
case RANDOM:
players[i] = new RandomPlayer(i);
break;
}
players_points[i] = 0;
if (options.use_compulsory_solo())
played_compulsory_solo[i] = false;
else // no compulsory solo to play is the same as saying players already played their compulsory solo
played_compulsory_solo[i] = true;
}
for (int i = 0; i < options.get_number_of_games(); ++i) {
cout << "starting game number " << i << " [" << *timer << "]" << endl;
if (options.use_random_cards())
shuffle_cards();
else {
if (!options.specify_cards_manually(cards)) {
cout << "using random cards" << endl;
shuffle_cards();
}
}
set_cards();
if (!vorfuehrung)
check_vorfuehrung(options.get_number_of_games() - i);
ActualGameState actual_game_state(options, players, first_player, cards, played_compulsory_solo, vorfuehrung);
int new_points[4] = { 0, 0, 0, 0 };
actual_game_state.get_score_points(new_points);
for (int i = 0; i < 4; ++i) {
players[i]->inform_about_game_end(new_points);
players_points[i] += new_points[i];
}
if (actual_game_state.is_compulsory_solo()) {
int solo_player = actual_game_state.get_compulsory_solo_player();
played_compulsory_solo[solo_player] = true;
}
statistics();
// update next player for all normal games and vorfuehrung compulsory solos. all "regular" compulsory solos are going to be repeated with the same dealer and player positions (although for vorfuehrung it does not really matter, because anyway the solo player starts playing)
if (!actual_game_state.is_compulsory_solo() || vorfuehrung)
first_player = next_player(first_player);
}
cout << "doppelkopf session finished" << endl;
}
void Session::shuffle_cards() {
cards[0] = Cards();
cards[1] = Cards();
cards[2] = Cards();
cards[3] = Cards();
Card deck[48];
for (int i = 0; i < 48; ++i) {
deck[i] = Card(i);
}
random_shuffle(deck, deck + 48, rng);
for (int i = 0; i < 12; ++i) {
cards[0].add_card(deck[i]);
cards[1].add_card(deck[i + 12]);
cards[2].add_card(deck[i + 24]);
cards[3].add_card(deck[i + 36]);
}
assert(cards[0].size() == 12);
assert(cards[1].size() == 12);
assert(cards[2].size() == 12);
assert(cards[3].size() == 12);
}
void Session::set_cards() const {
//if (options.use_verbose())
//cout << "dealing cards:" << endl;
for (int i = 0; i < 4; ++i) {
//if (options.use_verbose())
//cout << i << " " << cards[i] << endl;
players[i]->set_cards(cards[i]);
}
}
void Session::check_vorfuehrung(int number_of_remaining_games) {
int remaining_compulsory_solos = 0;
for (int i = 0; i < 4; ++i) {
if (!played_compulsory_solo[i])
++remaining_compulsory_solos;
}
assert(remaining_compulsory_solos <= number_of_remaining_games);
if (remaining_compulsory_solos == number_of_remaining_games) {
vorfuehrung = true;
}
}
void Session::statistics() const {
cout << "standings:" << endl;
int sum = 0;
for (int i = 0; i < 4; ++i) {
cout << "player " << i << ": " << players_points[i] << endl;
sum += players_points[i];
}
cout << endl;
assert(sum == 0);
}