-
Notifications
You must be signed in to change notification settings - Fork 0
/
lobbygame.cpp
321 lines (268 loc) · 11.3 KB
/
lobbygame.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
#include "lobbygame.h"
LobbyGame::LobbyGame(QWidget *parent, QString *pseudonyme, string nameLobby, Client * client, Lobby * lobby, bool isHost) :
QWidget(parent), pseudonyme(pseudonyme), client(client), lobby(lobby), isHost(isHost)
{
tchatGame = new QPlainTextEdit(this);
tchatGame->setStyleSheet("background-color: rgb(134, 135, 127);");
tchatGame->setGeometry(1000, 100, 700, 450);
tchatGame->setReadOnly(true);
inputMessage = new QLineEdit(this);
inputMessage->setStyleSheet("background-color: rgb(134, 135, 127);");
inputMessage->setGeometry(1000, 600, 700, 100);
inputMessage->setPlaceholderText("Ecrivez votre message");
connect(inputMessage, SIGNAL(returnPressed()), this, SLOT(writeMessage()));
gamemodeBox = new QComboBox(this);
gamemodeBox->setStyleSheet("background-color: rgb(84, 84, 84);");
gamemodeBox->setGeometry(1000, 750, 300, 50);
gamemodeBox->addItem("Normal");
startGame = new QPushButton(this);
startGame->setText("Démarrer la partie");
startGame->setStyleSheet("background-color: rgb(84, 84, 84);");
startGame->setGeometry(1000, 850, 300, 50);
connect(startGame, SIGNAL(clicked(bool)), this, SLOT(startGamePressed()));
startGame->setEnabled(isHost);
rulesButton = new QPushButton(this);
rulesButton->setText("Règles du jeu: ");
rulesButton->setStyleSheet("background-color: rgb(84, 84, 84);");
rulesButton->setGeometry(1000, 900, 700, 50);
rulesWindow = new QMainWindow(this);
connect(rulesButton, SIGNAL(clicked(bool)), this, SLOT(displayRules()));
for (int i=0; i<9; i++){
for (int j=0; j<9; j++){
QString player;
//vérifie si le pion est sur tel case pour envoyer la couleur du pion
if (i == 0 && j == 4)
player = "1";
else if ( i == 8 && j == 4)
player = "2";
else
player = "None";
ButtonBox *button = new ButtonBox(this, i, j, player);
connect(button, SIGNAL(clicked(bool)), button, SLOT(pushButton_clicked()));
connect(button, SIGNAL(buttonPressed(string)), this, SLOT(sendMove(string)));
buttonBoxList.append(button);
}
}
for (int i=0; i<9; i++){
for (int j=0; j<9; j++){
ButtonWall *button = new ButtonWall(this, i, j, "H");
connect(button, SIGNAL(clicked(bool)), button, SLOT(pushButton_clicked()));
connect(button, SIGNAL(wallClicked(string, string)), this, SLOT(sendWall(string, string)));
buttonWallH.append(button);
}
}
for (int i=0; i<9; i++){
for (int j=0; j<9; j++){
ButtonWall *button = new ButtonWall(this, i, j, "V");
connect(button, SIGNAL(clicked(bool)), button, SLOT(pushButton_clicked()));
connect(button, SIGNAL(wallClicked(string, string)), this, SLOT(sendWall(string, string)));
buttonWallV.append(button);
}
}
setStateButton(false);
border1 = new QPushButton(this);
border1->setStyleSheet("background-color: rgb(0, 0, 0);");
border1->setGeometry( 0,0,900, 20);
border1->show();
border2 = new QPushButton(this);
border2->setStyleSheet("background-color: rgb(0, 0, 0);");
border2->setGeometry( 0,900,920, 20);
border2->show();
QPushButton *border3 = new QPushButton(this);
border3->setStyleSheet("background-color: rgb(0, 0, 0);");
border3->setGeometry( 0,0,20, 900);
border3->show();
QPushButton *border4 = new QPushButton(this);
border4->setStyleSheet("background-color: rgb(0, 0, 0);");
border4->setGeometry( 900,0,20, 900);
border4->show();
saveGame = new QPushButton(this);
saveGame->setText("Interrompre et sauvegarder la partie");
saveGame->setStyleSheet("background-color: rgb(84, 84, 84);");
saveGame->setGeometry(1000, 850, 300, 50);
//connect(saveGame, SIGNAL(clicked(bool)), this, SLOT(on_createAccountButton_clicked()));
saveGame->hide();
surrendGame = new QPushButton(this);
surrendGame->setText("Abandonner la partie");
surrendGame->setStyleSheet("background-color: rgb(84, 84, 84);");
surrendGame->setGeometry(1400, 850, 300, 50);
connect(surrendGame, SIGNAL(clicked(bool)), this, SLOT(exitLobby()));
surrendGame->hide();
validMove = new QPushButton(this);
validMove->setText("Valider le déplacement");
validMove->setStyleSheet("background-color: rgb(84, 84, 84);");
validMove->setGeometry(1000, 750, 300, 50);
//connect(validMove, SIGNAL(clicked(bool)), this, SLOT(on_createAccountButton_clicked()));
validMove->hide();
validWall = new QPushButton(this);
validWall->setText("Valider le mur");
validWall->setStyleSheet("background-color: rgb(84, 84, 84);");
validWall->setGeometry(1400, 750, 300, 50);
//connect(validWall, SIGNAL(clicked(bool)), this, SLOT(on_createAccountButton_clicked()));
validWall->hide();
}
LobbyGame::~LobbyGame(){
}
void LobbyGame::exitLobby(){
if(!rulesWindow->isHidden()) rulesWindow->hide();
emit returnMainClient();
}
void LobbyGame::startGamePressed(){
string msg = "/launchGame";
client->TrySendMessage(msg);
qDebug()<<"post launchGame";
int connectedPlayer = lobby->getNumberConnectedPlayer();
qDebug()<<"start Game";
if(connectedPlayer == 2 || connectedPlayer == 4){
qDebug()<<"start Game";
string start = "/startGame standard";
client->TrySendMessage(start);
qDebug()<<"start Game";
}else{
return;
}
}
QPushButton* LobbyGame::getBorderHost(){
return border2;
}
QPushButton* LobbyGame::getBorderGuest(){
return border1;
}
void LobbyGame::writeMessage(){
QString message, name;
message = inputMessage->text();
string msg = "/allChat \"" + message.toStdString()+ '"';
client->TrySendMessage(msg);
inputMessage->clear();
}
void LobbyGame::displayMessage(string message){
tchatGame->appendPlainText(QString::fromStdString(message));
tchatGame->update();
}
void LobbyGame::sendMove(string move){
string moveToSend = "/move ";
string jumpToSend = "/jump ";
moveToSend += move;
jumpToSend += move;
qDebug()<<"move sent: " + QString::fromStdString(moveToSend);
client->TrySendMessage(moveToSend);
client->TrySendMessage(jumpToSend);
}
void LobbyGame::resetWallClicked(){
for (auto h : buttonWallH){
if(h->isPressed()){
h->setChecked(false);
h->setStyleSheet("background-color: rgb(193, 187, 19);");
h->update();
h->setUnpressed();
}
}
for (auto v : buttonWallV){
if(v->isPressed()){
v->setChecked(false);
v->setStyleSheet("background-color: rgb(193, 187, 19);");
v->update();
v->setUnpressed();
}
}
}
void LobbyGame::sendWall(string move, string dir){
wallCount++;
if(wallCount == 1){
wallToSend += '"';
wallToSend += move;
wallToSend += " ";
dirWall = dir;
}
if(wallCount == 2){
resetWallClicked();
string finalWall = "/move ";
wallToSend += move;
wallToSend += " +";
wallToSend += '"';
finalWall += wallToSend;
qDebug()<< "move wall: " +QString::fromStdString(finalWall);
if(dirWall == dir){
client->TrySendMessage(finalWall);
}
wallCount = 0;
wallToSend = "";
}
}
void LobbyGame::displayBoard(string brd, string wall) {
Board board;
board.deserializeBoard(brd);
vector<Wall> temp_wall;
if(wall != "noWalls"){
temp_wall= deserialiseWallString(wall);
board.setNewWallList(temp_wall);
}
int x = 0;
for(int i = 0; i < buttonBoxList.size(); i++){
int y = i % 9;
if(i != 0 && i % 9 == 0) x++;
int idPlayer = board.getBoard()[x][y];
if(idPlayer == 1){
buttonBoxList[i]->setStyleSheet("background-color: rgb(255, 0, 0);");
}else if(idPlayer == 2){
buttonBoxList[i]->setStyleSheet("background-color: rgb(0, 255, 0);");
}else if(idPlayer == 3){
buttonBoxList[i]->setStyleSheet("background-color: rgb(0, 0, 255);");
}else if(idPlayer == 4){
buttonBoxList[i]->setStyleSheet("background-color: rgb(140, 0, 255);");
}else{
buttonBoxList[i]->setStyleSheet("background-color: rgb(132, 97, 8);");
}
if ( i < buttonBoxList.size()-1 && ( i == 0 || i % 9 != 0)){
if(board.isWallBetween(buttonBoxList[i]->getCoord(), buttonBoxList[i+1]->getCoord())){
buttonWallV[i]->setStateWall(true);
}
}
if(i < 72){
if(board.isWallBetween(buttonBoxList[i]->getCoord(), buttonBoxList[i+9]->getCoord())){
buttonWallH[i]->setStateWall(true);
}
}
buttonBoxList[i]->update();
}
}
void LobbyGame::displayRules(){
QPlainTextEdit *a = new QPlainTextEdit(this);
a->setReadOnly(true);
a->appendHtml("<html><head/><body><p align=center><span style= font-size:14pt;>Règles de base:</span></p></body></html>");
//a->appendHtml("<html><head/><body><p><a href=https://www.gigamic.com/files/catalog/products/rules/quoridor-classic-fr.pdf%3E<span style= text-decoration: underline; color:#007af4;>Lien vers les règles de base</span></a></p></body></html>");
a->appendHtml("https://www.gigamic.com/files/catalog/products/rules/quoridor-classic-fr.pdf%22");
a->appendPlainText(" ");
a->appendHtml("<html><head/><body><p align=center><span style= font-size:14pt;>Mode Contre-la-montre:</span></p></body></html>");
a->appendPlainText("Disponible uniquement en mode deux joueurs. Les joueurs disposent chacun d'un timer de 10 minutes et la partie se termine soit si un gagnant est élu avant la fin des timers soit si un timer arrive à bout et dans ce cas le joueur qui n'a plus de temps perd la partie.");
a->appendPlainText(" ");
a->appendHtml("<html><head/><body><p align=center><span style= font-size:14pt;>Mode Attaque:</span></p></body></html>");
a->appendPlainText("Disponible uniquement en mode deux joueurs. Lorsqu'un joueur passe par dessus l'autre joueur, il envoie ce dernier à sa case de départ ou à une autre case de départ si sa case est bloqué par les murs.");
a->appendPlainText(" ");
a->appendHtml("<html><head/><body><p align=center><span style= font-size:14pt;>Mode Pouvoirs:</span></p></body></html>");
a->appendPlainText("Disponible uniquement en mode deux joueurs.");
rulesWindow->setCentralWidget(a);
rulesWindow->setWindowTitle("Règles du jeu");
rulesWindow->setGeometry(500, 500, 700, 300);
rulesWindow->show();
}
void LobbyGame::setStateButton(bool b){
for(auto btn : buttonBoxList){
btn->setDisabled(!b);
}
for(auto btn : buttonWallH){
btn->setDisabled(!b);
}
for(auto btn : buttonWallV){
btn->setDisabled(!b);
}
}
void LobbyGame::initGame(){
delete gamemodeBox;
delete startGame;
setStateButton(true);
saveGame->show();
surrendGame->show();
validMove->show();
validWall->show();
}