-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMainWindow.cpp
More file actions
123 lines (113 loc) · 4.55 KB
/
Copy pathCMainWindow.cpp
File metadata and controls
123 lines (113 loc) · 4.55 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
//-----------------------------------------------------------------------------
#include <QKeyEvent>
#include <QtDebug>
#include <QMessageBox>
#include "CMainWindow.h"
//-----------------------------------------------------------------------------
CMainWindow::CMainWindow(QWidget *parent) : QMainWindow(parent), solveur(nullptr) {
qRegisterMetaType<CDeplacement::EDirection>();
srand(time(NULL));
setupUi(this);
installEventFilter(this);
lblScore->setText("Score : "+QString::number(wGame->getScore()));
}
//-----------------------------------------------------------------------------
void CMainWindow::on_actQuitter_triggered() {
QApplication::quit();
}
//-----------------------------------------------------------------------------
void CMainWindow::on_actNouveauJeu_triggered() {
if(QMessageBox::question(this, "Confirmation", "Etes vous sûre de vouloir commencer une nouvelle partie ?", QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
wGame->nouveau();
lblScore->setText("Score : "+QString::number(wGame->getScore()));
}
}
//-----------------------------------------------------------------------------
void CMainWindow::on_pbIA_clicked() {
if(solveur) {
solveur->stop();
} else {
solveur = new CSolveur(wGame->getGame());
connect(solveur, &CSolveur::coupChoisi, this, &CMainWindow::onCoupChoisi);
connect(solveur, &QThread::finished, this, &CMainWindow::onSolveurFini);
pbIA->setText("Stop");
solveur->start();
}
}
//-----------------------------------------------------------------------------
void CMainWindow::on_chkVideo_toggled(bool checked) {
wGame->setEnregistrement(checked);
}
//-----------------------------------------------------------------------------
void CMainWindow::onCoupChoisi(CDeplacement::EDirection direction) {
CGame::EResultat resultat;
switch(direction) {
case CDeplacement::edHaut: resultat = wGame->haut(false); break;
case CDeplacement::edDroite: resultat = wGame->droite(false); break;
case CDeplacement::edBas: resultat = wGame->bas(false); break;
case CDeplacement::edGauche: resultat = wGame->gauche(false); break;
default: return;
}
lblScore->setText("Score : "+QString::number(wGame->getScore()));
wGame->capturerFrame();
if(resultat == CGame::erPerdu || resultat == CGame::erFin || resultat == CGame::erGagne) {
solveur->stop();
return;
}
solveur->updateGame(wGame->getGame());
}
//-----------------------------------------------------------------------------
void CMainWindow::onSolveurFini() {
solveur->deleteLater();
solveur = nullptr;
pbIA->setText("IA");
}
//-----------------------------------------------------------------------------
bool CMainWindow::eventFilter(QObject *obj, QEvent *event) {
if(event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
bool bonneTouche = false;
CGame::EResultat resultat;
switch(keyEvent->key()) {
case Qt::Key_Up:
resultat = wGame->haut();
bonneTouche = true;
break;
case Qt::Key_Right:
resultat = wGame->droite();
bonneTouche = true;
break;
case Qt::Key_Down:
resultat = wGame->bas();
bonneTouche = true;
break;
case Qt::Key_Left:
resultat = wGame->gauche();
bonneTouche = true;
break;
default:
break;
}
if(bonneTouche) {
lblScore->setText("Score : "+QString::number(wGame->getScore()));
switch(resultat) {
case CGame::erGagne:
QMessageBox::information(this, "Gagné", "Félicitation, vous avez gangé !", QMessageBox::Ok);
break;
case CGame::erPerdu:
QMessageBox::information(this, "Perdu", "Vous avez perdu, re-tenter votre chance !", QMessageBox::Ok);
wGame->nouveau();
lblScore->setText("Score : "+QString::number(wGame->getScore()));
break;
case CGame::erFin:
QMessageBox::information(this, "Fin de partie", "Votre partie est terminé !", QMessageBox::Ok);
break;
default:
break;
}
return true;
}
}
return QObject::eventFilter(obj, event);
}
//-----------------------------------------------------------------------------