This repository has been archived by the owner on Aug 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.h
438 lines (340 loc) · 16.4 KB
/
mainwindow.h
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "qframe.h"
#include "qlabel.h"
#include "QLineEdit"
#include "qpushbutton.h"
#include <QMainWindow>
#include <QDebug>
#include <QMessageBox>
#include <QGridLayout>
#include <QFile>
#include <QSettings>
#include <QCursor>
#include <QPixmap>
#include <QWidget>
#include <QKeyEvent>
#include <QHostInfo>
#include <QtNetwork/QNetworkInterface>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
#include <QTcpServer>
#include <QTcpSocket>
#include <QMovie>
#include <QTimer>
#include <QFileDialog>
#include <QDialogButtonBox>
#include <QSoundEffect>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
inline QString MESSAGE_START_PREP = "START_PREP";
inline QString MESSAGE_HOST_READY = "HOST_READY";
inline QString MESSAGE_CLIENT_READY = "CLIENT_READY";
inline QString MESSAGE_START_GAME = "START_GAME";
inline QString MESSAGE_CLIENT_WON = "CLIENT_WON";
inline QString MESSAGE_HOST_WON = "HOST_WON";
inline QString MESSAGE_HOST_DISCONNECTED = "HOST_DISCONNECTED";
inline QString MESSAGE_CLIENT_DISCONNECTED = "CLIENT_DISCONNECTED";
inline QString MESSAGE_HOST_HIT = "HIT_HOST";
inline QString MESSAGE_CLIENT_HIT = "CLIENT_HOST";
class MainWindow : public QMainWindow{
Q_OBJECT
public:
Ui::MainWindow* getUI() const{
return ui;
}
QSoundEffect Miss, Hit, MainMusic, BattleMusic;
int MusicLevel=100, FXLevel=100;
int UserGameMap[10][10] = {{0}}, EnemyGameMap[10][10] = {{0}};// UserGameMap - Map used for gameplay as user's map. EnemyGameMap - Map used for gameplay as enemy's map.
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void GridButtonPressed(int row, int col);
private slots:
bool isCheckConnectedToInternet() {// Checks for internet connection (not the most reliable way, best i could've implement quickly)
QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
for (const QNetworkInterface& interface : interfaces)
{
if (interface.isValid() && interface.flags().testFlag(QNetworkInterface::IsUp) &&
interface.flags().testFlag(QNetworkInterface::IsRunning) &&
!interface.flags().testFlag(QNetworkInterface::IsLoopBack))
{
QList<QNetworkAddressEntry> addresses = interface.addressEntries();
for (const QNetworkAddressEntry& address : addresses)
{
if (address.ip().protocol() == QAbstractSocket::IPv4Protocol &&
address.ip().toString() != "127.0.0.1")
{
// Found a connected Wi-Fi interface
return true;
}
}
}
}
// No connected Wi-Fi interface found
return false;
}
QString getWlanIpAddress() {
QString localhostname = QHostInfo::localHostName();
QString localhostIP;
QList<QHostAddress> hostList = QHostInfo::fromName(localhostname).addresses();
foreach (const QHostAddress& address, hostList) {
if (address.protocol() == QAbstractSocket::IPv4Protocol && address.isLoopback() == false) {
localhostIP = address.toString();
}
}
qDebug() << "Localhost name: " << localhostname;
qDebug() << "IP = " << localhostIP;
return localhostIP;
}
void on_pushButton_menuLanguage_clicked();// When clicked on language button (flag)
void readSettings();// Reads local settings (Name, Chosen Language, Sound level, etc.)
void translateAll();// Translates all buttons/labels that exist in translation file
bool checkShipPlacement(int startRow, int startCol, int shipLength, bool isVertical, int MapToCheck[10][10]);
QString translate(const QString& source, const QString& targetLanguage){// Looks up translation for given source string, in given language
static QMap<QString, QMap<QString, QString>> translations;
// If the translations map is empty, load translations from file.
if (translations["en"].empty()){
QFile file("D:\\DeltaMolfar\\Work\\PNU\\CPL_Warships\\res\\en_tr.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qWarning() << "Failed to open en_tr.txt file";
return source;
}
QTextStream in(&file);
while(!in.atEnd()){
QString line = in.readLine();
QStringList parts = line.split(":");
translations["en"][parts[0]] = parts[1];
}
if (translations["ua"].empty()){
QFile file("D:\\DeltaMolfar\\Work\\PNU\\CPL_Warships\\res\\ua_tr.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qWarning() << "Failed to open ua_tr.txt file";
return source;
}
QTextStream in(&file);
while(!in.atEnd()){
QString line = in.readLine();
QStringList parts = line.split(":");
translations["ua"][parts[0]] = parts[1];
}
}
}
auto it = translations.find(targetLanguage);
if (it != translations.end()){
auto submap = it.value();
if (submap.contains(source)){
return submap.value(source);
}
else{
qDebug() << "Translation not found for" << source << "in" << targetLanguage;
}
}
else{
qDebug() << "No translations found for language:" << targetLanguage;
}
// If no translation is found, return the source string.
return source;
}
void on_pushButton_menuSingleplayer_clicked();
void on_pushButton_menuMultiplayer_clicked();
void on_pushButton_SinglePlayer_Menu_Back_clicked();
void on_pushButton_SinglePlayer_EasyMode_clicked();// When chosen easy mode in single player
void on_pushButton_SinglePlayer_MediumMode_clicked();// When chosen medium mode in single player
void on_pushButton_SinglePlayer_HardMode_clicked();// When chosen hard mode in single player
void SinglePlayerButtonChange(int Num);
void AddShip(int startRow, int startCol, int shipLength, bool isVertical, int OriginalArray[10][10]);
void ResetMaps();
bool IsPreparationComplete(bool IsSinglePlayer);
void GetEnemyMap(bool Generate);// Gets enemy map from another user, or generates one if AI.
void StartGame(bool Versus_AI);// Starts game sequence
void FinishGame(bool Win);
void SimulateEnemyHit();
void Game_Tick(bool ChangeTurn, bool Versus_AI);
void DifficultyStyleSheetHandler();// Changes apearence of difficulty buttons on click
void Network_NewConnectionHandler();// Handles new connection to a server (if this client is a server)
void Network_ClientConnetedToHostHandler();
void Network_ClientConnectionError(QAbstractSocket::SocketError error);
void Network_Host_HandleClientData();
void Network_Client_HandleHostData();
void Network_Host_SendMessage(QString MessageType, int row=0, int col=0, int DoubleArray[10][10]=0);
void Network_Client_SendMessage(QString MessageType, int row=0, int col=0, int DoubleArray[10][10]=0);
void on_pushButton_SinglePlayer_Menu_Play_clicked();
void on_pushButton_SinglePlayerPrep_Start_clicked();// When clicked on start in singleplayer preparation
void on_pushButton_SinglePlayerGameplay_Surrender_clicked();// When clicked on surrender button in singleplayer gameplay
void on_pushButton_SinglePlayerPrep_Carrier_clicked();// When clicked on carrier in singleplayer preparation
void on_pushButton_SinglePlayerPrep_Cruiser_clicked();// When clicked on cruiser in singleplayer preparation
void on_pushButton_SinglePlayerPrep_Submarine_clicked();// When clicked on submarine in singleplayer preparation
void on_pushButton_SinglePlayerPrep_Boat_clicked();// When clicked on boat in singleplayer preparation
void SetCursorOnPrep();// Changes cursor depending on ship chosen
void on_pushButton_SinglePlayerPrep_Back_clicked();
void on_pushButton_MultiPlayerSettings_Back_clicked();
void on_pushButton_MultiPlayerSettings_QnA_clicked();
void on_pushButton_SinglePlayerPrep_Rules_clicked();
void on_pushButton_MultiPlayerSettings_Create_clicked();
void on_pushButton_MultiPlayerSettings_Connect_clicked();
void on_pushButton_MultiPlayerSettings_Ready_or_Start_clicked();
void on_pushButton_MultiPlayerPrep_Start_clicked();
void on_pushButton_MultiPlayer_Carrier_clicked();
void on_pushButton_MultiPlayer_Cruiser_clicked();
void on_pushButton_MultiPlayer_Submarine_clicked();
void on_pushButton_MultiPlayer_Boat_clicked();
void on_pushButton_MultiPlayerPrep_Disconnect_clicked();
void on_pushButton_MultiPlayer_Surrender_clicked();
void on_pushButton_MultiPlayerPrep_Rules_clicked();
void on_pushButton_menuSettings_clicked();
void on_pushButton_menuProfile_clicked();
void on_pushButton_menuCredits_clicked();
void on_pushButton_Profile_Save_clicked();
void on_pushButton_Profile_Back_clicked();
void on_pushButton_Profile_ChangeName_clicked();
void on_pushButton_Profile_Icon_clicked();
void on_pushButton_Settings_Back_clicked();
void on_horizontalSlider_valueChanged(int value);
void on_horizontalSlider_2_valueChanged(int value);
private:
QString UserImagePath, EnemyImagePath, UserName, EnemyName, languageSetting, HoldingType;// languageSettings - holds which language to use. HoldingType - holds which ship player is currently holding.
bool UnsavedProfile=false, IsClient=true, IsUserTurn=false, IsHolding = false, IsVertical = false;// IsHolding - is user holding any ship. IsVertical - If the ship user holding ship be vertical.
int UserHealth=20, EnemyHealth=20, DifficultyMode=2, lastHitPos[2] = {-1}, hitDirection = -1; // 0: up, 1: down, 2: left, 3: right; // DifficultyMode: 1 - Easy, 2 - Medium, 3 - Hard
QTimer AiHitDelay;
std::vector<std::vector<int>> scores = std::vector<std::vector<int>>(10, std::vector<int>(10, 0));
QTcpServer* server;
QTcpSocket *clientSocket;
QTcpSocket *hostSocket;
Ui::MainWindow *ui;
protected:
void keyPressEvent(QKeyEvent *event) override{// Called when any key pressed (used for turning ships in preparation on 'R')
if(event->key() == Qt::Key_R){
if(IsVertical){IsVertical=false;}
else{IsVertical=true;}
SetCursorOnPrep();
}
}
};
class GridWidget : public QFrame{// GridWidget - grid that is used in preparation and in gameplay.
Q_OBJECT
public:
GridWidget(QFrame* qFrame, QWidget* parent) : QFrame(qFrame->parentWidget()){
QGridLayout *gridLayout = new QGridLayout(this);
gridLayout->setSpacing(5);
// Creates labels for column markings
for (int col = 0; col < 10; ++col) {
QString colMark = QString::number(col + 1);
QLabel *label = new QLabel(colMark, this);
label->setAlignment(Qt::AlignCenter);
label->setStyleSheet("color: rgb(235, 235, 235);\nfont: 700 18pt \"Consolas\";");
gridLayout->addWidget(label, 0, col + 1);
}
// Creates labels for row markings and buttons for grid cells
for(int row = 0; row<10; ++row){
QString rowMark = QChar('A' + row);
QLabel *label = new QLabel(rowMark, this);
label->setAlignment(Qt::AlignCenter);
label->setStyleSheet("color: rgb(235, 235, 235);\nfont: 700 18pt \"Consolas\";");
gridLayout->addWidget(label, row + 1, 0);
for(int col = 0; col<10; ++col){
QPushButton *button = new QPushButton(this);
button->setFixedSize(50, 50);
button->setStyleSheet(buttonStyleFree);
gridLayout->addWidget(button, row + 1, col + 1);
connect(button, &QPushButton::pressed, this, [this, row, col]() {
buttonPressed(row, col);
});
button->setObjectName(QString("button_%1_%2").arg(row).arg(col));
}
}
setLayout(gridLayout);
}
void updateButtonStyles(const int gridArray[10][10]){
for(int row = 0; row < 10; ++row){
for(int col = 0; col < 10; ++col){
QPushButton* button = findChild<QPushButton*>(QString("button_%1_%2").arg(row).arg(col));
if(button){
int value = gridArray[row][col];
if(value == 0){
button->setStyleSheet(buttonStyleFree);
}
else if(value == 1){
button->setStyleSheet(buttonStyleOccupied);
}
}
}
}
}
void SetGridDisabled(bool disabled){
for(int row = 0; row<10; ++row){
for(int col = 0; col<10; ++col){
QPushButton* button = findChild<QPushButton*>(QString("button_%1_%2").arg(row).arg(col));
if(button){
button->setDisabled(disabled);
}
}
}
}
bool Hit(int row, int col, int MapToHit[10][10]){
QPushButton* button = findChild<QPushButton*>(QString("button_%1_%2").arg(row).arg(col));
static QRegularExpression rg("\\s*/\\*\\s*PutImageHere\\s*\\*/");
if(MapToHit[row][col]==0){
button->setStyleSheet( button->styleSheet().replace(rg, "/* PutImageHere */ image: url(:/new/prefix1/res/mark_miss.png);") );
return false;
}
else{
button->setStyleSheet( button->styleSheet().replace(rg, "/* PutImageHere */ image: url(:/new/prefix1/res/mark_hit.png);") );
return true;
}
}
bool IsHitLegal(int row, int col){
QPushButton* button = findChild<QPushButton*>(QString("button_%1_%2").arg(row).arg(col));
if(button->styleSheet().contains("image: url(:/new/prefix1/res/mark_hit.png);")||button->styleSheet().contains("image: url(:/new/prefix1/res/mark_miss.png);")){return false;}
return true;
}
private slots:
void buttonPressed(int row, int col){// Called when button on grid is pressed
// Traverse the widget hierarchy to access the MainWindow
QWidget* currentWidget = parentWidget(); // Get the immediate parent widget (QStackedWidget)
QWidget* mainWindowWidget = nullptr;
// Traverse up the widget hierarchy until MainWindow is found
while (currentWidget) {
if (MainWindow* mainWindow = qobject_cast<MainWindow*>(currentWidget)) {
mainWindowWidget = mainWindow;
break;
}
currentWidget = currentWidget->parentWidget();
}
// Check if the MainWindow instance was found
if (mainWindowWidget) {
MainWindow* mainWindow = qobject_cast<MainWindow*>(mainWindowWidget);
if (mainWindow) {
mainWindow->GridButtonPressed(row, col);
}
}
}
private:
QString buttonStyleFree = "\
QPushButton { /* PutImageHere */\
background-color: rgb(220,220,220); \
border: 2px solid black; \
} \
QPushButton:hover { \
border-color: rgb(0,210,210); \
} \
QPushButton:pressed { \
border-color: rgb(0,175,175); \
}";
QString buttonStyleOccupied = "\
QPushButton { /* PutImageHere */\
background-color: rgb(40,40,40); \
border: 2px solid black; \
} \
QPushButton:hover { \
border-color: rgb(0,210,210); \
} \
QPushButton:pressed { \
border-color: rgb(0,175,175); \
}";
};
#endif // MAINWINDOW_H
// https://www.flaticon.com/free-icons/letter-x -> Letter x icons created by Alfredo Hernandez - Flaticon
// https://www.flaticon.com/free-icons/close -> Close icons created by Pixel perfect - Flaticon