-
Notifications
You must be signed in to change notification settings - Fork 0
/
wtconnectthreegamedialog.cpp
241 lines (213 loc) · 5.48 KB
/
wtconnectthreegamedialog.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
#include "wtconnectthreegamedialog.h"
#include <cassert>
#include <Wt/WBreak>
#include <Wt/WImage>
#include <Wt/WPushButton>
#include <Wt/WText>
#include <Wt/WTimer>
#include <Wt/WResource>
#include "connectthreegame.h"
#include "connectthreeresources.h"
#include "wtconnectthreewidget.h"
ribi::con3::WtGameDialog::WtGameDialog(
const Resources& resources,
const std::bitset<3>& is_player_human)
: m_board{},
m_is_player_human(is_player_human),
m_players{},
m_resources(resources),
m_state(state_playing),
m_timer(new Wt::WTimer(this))
{
this->setContentAlignment(Wt::AlignCenter);
m_timer->timeout().connect(
this,
&ribi::con3::WtGameDialog::DoComputerTurn);
m_timer->setInterval(100);
ShowGame();
//OnValidMove(); //Draw screen
}
ribi::con3::WtGameDialog::~WtGameDialog()
{
m_timer->stop();
}
void ribi::con3::WtGameDialog::DoComputerTurn()
{
assert(m_board);
if (m_board->IsComputerTurn()
&& m_board->GetWinner() == Winner::no_winner)
{
m_board->DoComputerTurn();
OnValidMove();
}
}
///OnValidMove is called after a valid move. The game
///is either terminated, or the next player can do
///his/her move.
void ribi::con3::WtGameDialog::OnValidMove()
{
if (m_board->GetWinner() == Winner::no_winner)
{
UpdatePlayersPanel();
return;
}
m_state = state_winner;
this->PauseTimer();
ShowWinner();
}
void ribi::con3::WtGameDialog::RestartGame()
{
m_state = state_playing;
ShowGame();
assert(m_board);
m_board->Restart();
UpdatePlayersPanel();
StartTimer();
}
void ribi::con3::WtGameDialog::SetIsPlayerHuman(const std::bitset<3>& is_player_human)
{
if (m_is_player_human != is_player_human)
{
m_is_player_human = is_player_human;
assert(m_board);
m_board->SetIsPlayerHuman(m_is_player_human);
m_board->update();
UpdatePlayersPanel();
}
}
void ribi::con3::WtGameDialog::ShowGame()
{
assert(m_state == state_playing);
this->clear();
m_board = new WtWidget(m_resources,m_is_player_human,16,8);
m_board->m_signal_valid_move.connect(
boost::bind(
&ribi::con3::WtGameDialog::OnValidMove,
this));
assert(m_board);
assert(m_board->GetWinner() == Winner::no_winner);
this->addWidget(m_board);
this->addWidget(new Wt::WBreak);
this->addWidget(new Wt::WBreak);
m_players = {
new Wt::WImage(
m_resources.GetPlayersFilenames()[0],
"Player 1"),
new Wt::WImage(
m_resources.GetPlayersFilenames()[1],
"Player 2"),
new Wt::WImage(
m_resources.GetPlayersFilenames()[2],
"Player 3")
};
assert(m_players.size() == 3);
assert(m_players[0]);
assert(m_players[1]);
assert(m_players[2]);
this->addWidget(m_players[0]);
this->addWidget(m_players[1]);
this->addWidget(m_players[2]);
UpdatePlayersPanel();
}
void ribi::con3::WtGameDialog::ShowWinner()
{
assert(m_state = state_winner);
assert(m_board->GetWinner() != Winner::no_winner);
assert(!m_timer->isActive());
//Request all info from m_board, before deleting it
const Winner winner_index = m_board->GetWinner();
const std::string text
= (winner_index == Winner::draw
? "draw" : "winner");
//There is a winner
this->clear();
Wt::WImage * winner = new Wt::WImage;
switch(winner_index)
{
case Winner::player1:
winner->setImageRef(
m_resources.GetPlayersFilenames()[0]);
break;
case Winner::player2:
winner->setImageRef(
m_resources.GetPlayersFilenames()[1]);
break;
case Winner::player3:
winner->setImageRef(
m_resources.GetPlayersFilenames()[2]);
break;
case Winner::draw:
winner->setImageRef(
m_resources.GetEmptyFilename());
break;
default:
assert(!"Should not get here");
throw std::logic_error("Known value of GetCurrentPlayer in ribi::con3::WtConnectThreeGameDialog::OnValidMove");
}
this->addWidget(winner);
this->addWidget(new Wt::WBreak);
{
Wt::WText * const title = new Wt::WText(text.c_str());
title->setStyleClass("title");
this->addWidget(title);
}
this->addWidget(new Wt::WBreak);
{
Wt::WPushButton * const button(new Wt::WPushButton("Restart",this));
button->clicked().connect(
this,
&ribi::con3::WtGameDialog::RestartGame
);
}
}
void ribi::con3::WtGameDialog::UpdatePlayersPanel()
{
assert(m_board);
assert(m_players.size() == 3);
assert(m_players[0]);
assert(m_players[1]);
assert(m_players[2]);
const std::bitset<3>& is_player_human = m_board->GetIsPlayerHuman();
const int n_players = 3; //as the name is 'ConnectThree'
for (int i=0; i!=n_players; ++i)
{
Player i_p;
switch (i)
{
case 0: i_p = Player::player1; break;
case 1: i_p = Player::player2; break;
case 2: i_p = Player::player3; break;
default: assert(!"Should not get here");
}
if (is_player_human[i])
{
if (m_board->GetActivePlayer() == i_p)
{
m_players[i]->setImageRef(m_resources.GetPlayersFilenames()[i]);
}
else
{
m_players[i]->setImageRef(m_resources.GetPlayersGreyFilenames()[i]);
}
}
else
{
if (m_board->GetActivePlayer() == i_p)
{
m_players[i]->setImageRef(m_resources.GetComputersFilenames()[i]);
}
else
{
m_players[i]->setImageRef(m_resources.GetComputerGreyFilename());
}
}
}
}
void ribi::con3::WtGameDialog::PauseTimer()
{
m_timer->stop();
}
void ribi::con3::WtGameDialog::StartTimer()
{
m_timer->start();
}