-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
381 lines (342 loc) · 14 KB
/
mainwindow.cpp
File metadata and controls
381 lines (342 loc) · 14 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
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
#include "mainwindow.h"
#include <QGridLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QFont>
#include <QPushButton>
#include <QDebug>
#include <QMessageBox>
#include <QPropertyAnimation>
#include <QSequentialAnimationGroup>
#include <QParallelAnimationGroup>
#include <QRect>
#include <QTimer>
#include <QSize>
MainWindow::MainWindow(IGameManager& gameManager, IWordSource& wordSource, QWidget *parent)
: QMainWindow(parent)
, m_centralWidget(nullptr)
, m_mainVerticalLayout(nullptr)
, m_wordSource(wordSource)
, m_gameManager(gameManager)
, m_currentRow(0)
, m_currentCol(0)
, m_isGameActive(true)
, m_flipAnimationGroup(new QSequentialAnimationGroup(this))
, m_popAnimationGroup(new QParallelAnimationGroup(this))
{
m_centralWidget = new QWidget(this);
setCentralWidget(m_centralWidget);
m_gameManager.newGame();
setWindowTitle(QString("Wordle - [Gizli kelime: %1]").arg(m_gameManager.getSecretWord()));
m_mainVerticalLayout = new QVBoxLayout(m_centralWidget);
m_centralWidget->setStyleSheet("background-color: white;");
m_mainVerticalLayout->setContentsMargins(10, 10, 10, 10); // Sol, Üst, Sağ, Alt
QWidget* gridContainer = new QWidget(this);
const int boxSize = 60;
const int spacing = 5;
gridContainer->setFixedSize(
GameConfig::WORD_LENGTH * boxSize + (GameConfig::WORD_LENGTH - 1) * spacing,
GameConfig::MAX_GUESSES * boxSize + (GameConfig::MAX_GUESSES - 1) * spacing
);
QFont letterFont("Arial", 24, QFont::Bold);
for (int row = 0; row < GameConfig::MAX_GUESSES; ++row) {
for (int col = 0; col < GameConfig::WORD_LENGTH; ++col) {
m_gridLabels[row][col] = new QLabel(gridContainer);
QLabel* label = m_gridLabels[row][col];
int xPos = col * (boxSize + spacing);
int yPos = row * (boxSize + spacing);
label->setGeometry(xPos, yPos, boxSize, boxSize);
label->setFont(letterFont);
label->setAlignment(Qt::AlignCenter);
label->setStyleSheet(
"QLabel { background-color: #ffffff; color: black; border: 2px solid #000000; border-radius: 0px; }"
);
}
}
QHBoxLayout* gridContainerLayout = new QHBoxLayout();
gridContainerLayout->addStretch();
gridContainerLayout->addWidget(gridContainer);
gridContainerLayout->addStretch();
// Klavye satırlarını QHBoxLayout ile oluştur
QFont buttonFont("Arial", 12);
const QString keyboardRows[] = {"QWERTYUIOPĞÜ", "ASDFGHJKLŞİ", "ZXCVBNMÖÇ"};
// 1. satır: tam başlar (0), 12 harf
int row0Start = 0;
// Klavye satırlarını QHBoxLayout ile oluştur
// 1. satır
QHBoxLayout* row1Layout = new QHBoxLayout();
row1Layout->addStretch(1);
for (int i = 0; i < keyboardRows[0].length(); ++i) {
QString letter = keyboardRows[0][i];
QPushButton* button = new QPushButton(letter, this);
button->setFixedSize(40, 50);
button->setFont(buttonFont);
button->setStyleSheet(
"QPushButton {"
" background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #bfc1c6, stop:1 #818384);"
" color: #222;"
" border: none;"
" border-radius: 8px;"
" font-weight: bold;"
" font-size: 16px;"
" letter-spacing: 1px;"
" }"
"QPushButton:hover {"
" background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #e0e0e0, stop:1 #a0a2a6);"
" color: #000;"
" }"
);
connect(button, &QPushButton::clicked, this, [=]() {
onLetterPressed(letter);
});
m_keyboardButtons[letter.at(0)] = button;
row1Layout->addWidget(button);
}
row1Layout->addStretch(1);
// 2. satır (yarım buton kadar sağa kaydırmak için ek stretch)
QHBoxLayout* row2Layout = new QHBoxLayout();
row2Layout->addStretch(2);
for (int i = 0; i < keyboardRows[1].length(); ++i) {
QString letter = keyboardRows[1][i];
QPushButton* button = new QPushButton(letter, this);
button->setFixedSize(40, 50);
button->setFont(buttonFont);
button->setStyleSheet(
"QPushButton {"
" background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #bfc1c6, stop:1 #818384);"
" color: #222;"
" border: none;"
" border-radius: 8px;"
" font-weight: bold;"
" font-size: 16px;"
" letter-spacing: 1px;"
" }"
"QPushButton:hover {"
" background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #e0e0e0, stop:1 #a0a2a6);"
" color: #000;"
" }"
);
connect(button, &QPushButton::clicked, this, [=]() {
onLetterPressed(letter);
});
m_keyboardButtons[letter.at(0)] = button;
row2Layout->addWidget(button);
}
row2Layout->addStretch(2);
// 3. satır (1 tam buton kadar sağa kaydırmak için daha fazla stretch)
QHBoxLayout* row3Layout = new QHBoxLayout();
row3Layout->addStretch(3);
QPushButton* enterButton = new QPushButton("ENTER", this);
enterButton->setMinimumSize(60, 50);
enterButton->setFont(buttonFont);
enterButton->setStyleSheet(
"QPushButton {"
" background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #6dd47e, stop:1 #38a169);"
" color: #fff;"
" border: none;"
" border-radius: 8px;"
" font-weight: bold;"
" font-size: 16px;"
" letter-spacing: 1px;"
" }"
"QPushButton:hover {"
" background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #8ef5a6, stop:1 #4fd67b);"
" color: #222;"
" }"
);
connect(enterButton, &QPushButton::clicked, this, &MainWindow::onEnterPressed);
row3Layout->addWidget(enterButton);
for (int i = 0; i < keyboardRows[2].length(); ++i) {
QString letter = keyboardRows[2][i];
QPushButton* button = new QPushButton(letter, this);
button->setFixedSize(40, 50);
button->setFont(buttonFont);
button->setStyleSheet(
"QPushButton {"
" background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #bfc1c6, stop:1 #818384);"
" color: #222;"
" border: none;"
" border-radius: 8px;"
" font-weight: bold;"
" font-size: 16px;"
" letter-spacing: 1px;"
" }"
"QPushButton:hover {"
" background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #e0e0e0, stop:1 #a0a2a6);"
" color: #000;"
" }"
);
connect(button, &QPushButton::clicked, this, [=]() {
onLetterPressed(letter);
});
m_keyboardButtons[letter.at(0)] = button;
row3Layout->addWidget(button);
}
QPushButton* backspaceButton = new QPushButton("SİL", this);
backspaceButton->setMinimumSize(60, 50);
backspaceButton->setFont(buttonFont);
backspaceButton->setStyleSheet(
"QPushButton {"
" background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #f56565, stop:1 #c53030);"
" color: #fff;"
" border: none;"
" border-radius: 8px;"
" font-weight: bold;"
" font-size: 16px;"
" letter-spacing: 1px;"
" }"
"QPushButton:hover {"
" background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #ffb3b3, stop:1 #e53e3e);"
" color: #222;"
" }"
);
connect(backspaceButton, &QPushButton::clicked, this, &MainWindow::onBackspacePressed);
row3Layout->addWidget(backspaceButton);
row3Layout->addStretch(3);
m_mainVerticalLayout->addLayout(gridContainerLayout);
m_mainVerticalLayout->addLayout(row1Layout);
m_mainVerticalLayout->addLayout(row2Layout);
m_mainVerticalLayout->addLayout(row3Layout);
}
MainWindow::~MainWindow() {}
void MainWindow::onLetterPressed(const QString& letter)
{
if (!m_isGameActive || m_currentCol >= GameConfig::WORD_LENGTH) { return; }
m_gridLabels[m_currentRow][m_currentCol]->setText(letter);
triggerPopAnimation(m_currentRow, m_currentCol);
m_currentCol++;
}
void MainWindow::onBackspacePressed()
{
if (!m_isGameActive || m_currentCol <= 0) { return; }
m_currentCol--;
m_gridLabels[m_currentRow][m_currentCol]->setText("");
}
void MainWindow::onEnterPressed()
{
if (!m_isGameActive || m_flipAnimationGroup->state() == QAbstractAnimation::Running) { return; }
if (m_currentCol != GameConfig::WORD_LENGTH) { return; }
m_isGameActive = false;
QString guess;
for (int col = 0; col < GameConfig::WORD_LENGTH; ++col) {
guess += m_gridLabels[m_currentRow][col]->text();
}
// Girilen kelimenin sözlükte olup olmadığını kontrol et
if (!m_wordSource.contains(guess)) {
QMessageBox::warning(this, "Geçersiz kelime", "Bu kelime listede yok.");
m_isGameActive = true;
return;
}
GuessResult result = m_gameManager.submitGuess(guess);
for(int i = 0; i < GameConfig::WORD_LENGTH; ++i) {
QChar letter = guess.at(i).toUpper();
LetterResult currentStatus = m_letterStates.value(letter, LetterResult::NotInWord);
if (letterResultRank(result.letterResults[i]) > letterResultRank(currentStatus)) {
m_letterStates[letter] = result.letterResults[i];
}
}
updateKeyboardColors();
triggerFlipAnimation(result.letterResults);
connect(m_flipAnimationGroup, &QSequentialAnimationGroup::finished, this, [=]() {
if (result.gameState == GameState::Won) {
QMessageBox::information(this, "Tebrikler!", "Doğru kelimeyi buldunuz!");
} else if (result.gameState == GameState::Lost) {
QMessageBox::warning(this, "Kaybettiniz!", "6 tahmin hakkınız bitti.\nDoğru kelime: " + m_gameManager.getSecretWord());
} else {
m_currentRow++;
m_currentCol = 0;
m_isGameActive = true;
}
}, Qt::SingleShotConnection);
}
void MainWindow::triggerPopAnimation(int row, int col)
{
// Zaten çalışan bir pop animasyonu varsa, yenisini başlatma
if (m_popAnimationGroup->state() == QAbstractAnimation::Running) {
return;
}
m_popAnimationGroup->clear();
QLabel* label = m_gridLabels[row][col];
if (!label) return;
QRect startGeometry = label->geometry();
QRect endGeometry = startGeometry;
endGeometry.adjust(-2, -2, 2, 2);
// Büyüme ve küçülmeyi art arda oynatmak için SIRALI bir grup kullanıyoruz.
QSequentialAnimationGroup* sequence = new QSequentialAnimationGroup(this);
QPropertyAnimation* anim1 = new QPropertyAnimation(label, "geometry");
anim1->setDuration(100);
anim1->setStartValue(startGeometry);
anim1->setEndValue(endGeometry);
anim1->setEasingCurve(QEasingCurve::OutCubic);
QPropertyAnimation* anim2 = new QPropertyAnimation(label, "geometry");
anim2->setDuration(100);
anim2->setStartValue(endGeometry);
anim2->setEndValue(startGeometry);
anim2->setEasingCurve(QEasingCurve::InCubic);
sequence->addAnimation(anim1);
sequence->addAnimation(anim2);
// Ana pop grubuna bu sekansı ekle
m_popAnimationGroup->addAnimation(sequence);
m_popAnimationGroup->start();
}
void MainWindow::triggerFlipAnimation(const QVector<LetterResult>& result)
{
if (m_flipAnimationGroup->state() == QAbstractAnimation::Running) {
return;
}
m_flipAnimationGroup->clear();
int rowToUpdate = m_currentRow;
for (int col = 0; col < GameConfig::WORD_LENGTH; ++col) {
QLabel* label = m_gridLabels[rowToUpdate][col];
// Ortadan dönme efekti için geometriyi manuel hesaplıyoruz
QRect startGeometry = label->geometry();
QRect midGeometry = startGeometry;
midGeometry.setHeight(0); // Yüksekliği sıfırla
midGeometry.moveTop(startGeometry.top() + startGeometry.height() / 2); // Merkeze kaydır
QPropertyAnimation* anim1 = new QPropertyAnimation(label, "geometry", m_flipAnimationGroup);
anim1->setDuration(200);
anim1->setStartValue(startGeometry);
anim1->setEndValue(midGeometry);
anim1->setEasingCurve(QEasingCurve::InCubic);
QPropertyAnimation* anim2 = new QPropertyAnimation(label, "geometry", m_flipAnimationGroup);
anim2->setDuration(200);
anim2->setStartValue(midGeometry);
anim2->setEndValue(startGeometry);
anim2->setEasingCurve(QEasingCurve::OutCubic);
connect(anim1, &QPropertyAnimation::finished, this, [=](){
QString styleSheet = "color: white; border-radius: 0px; border: 2px solid transparent; font: bold 24pt 'Arial';";
if (result[col] == LetterResult::CorrectPosition) {
styleSheet += "background-color: #538d4e;";
} else if (result[col] == LetterResult::WrongPosition) {
styleSheet += "background-color: #b59f3b;";
} else {
styleSheet += "background-color: #3a3a3c;";
}
label->setStyleSheet(styleSheet);
});
m_flipAnimationGroup->addPause(150);
m_flipAnimationGroup->addAnimation(anim1);
m_flipAnimationGroup->addAnimation(anim2);
}
m_flipAnimationGroup->start();
}
void MainWindow::updateKeyboardColors()
{
for (auto it = m_letterStates.constBegin(); it != m_letterStates.constEnd(); ++it) {
QChar letter = it.key();
LetterResult state = it.value();
QPushButton* button = m_keyboardButtons.value(letter, nullptr);
if (button) {
QString styleSheet = "color: white; border: none; border-radius: 5px; font: bold 12pt 'Arial';";
if (state == LetterResult::CorrectPosition) {
styleSheet += "background-color: #538d4e;";
} else if (state == LetterResult::WrongPosition) {
styleSheet += "background-color: #b59f3b;";
} else {
styleSheet += "background-color: #3a3a3c;";
}
button->setStyleSheet(styleSheet);
}
}
}