Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Application 2048
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Delplanque Sara
Application 2048
Commits
ac866a43
Commit
ac866a43
authored
2 months ago
by
Breitwiller Josephine
Browse files
Options
Downloads
Plain Diff
merge
parents
4357e504
e22daca7
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
applicationQT2048/GamePage.qml
+1
-1
1 addition, 1 deletion
applicationQT2048/GamePage.qml
applicationQT2048/gamemanager.cpp
+65
-3
65 additions, 3 deletions
applicationQT2048/gamemanager.cpp
applicationQT2048/gamemanager.h
+8
-0
8 additions, 0 deletions
applicationQT2048/gamemanager.h
with
74 additions
and
4 deletions
applicationQT2048/GamePage.qml
+
1
−
1
View file @
ac866a43
...
@@ -118,7 +118,7 @@ FocusScope { // Permet de capter le focus pour les raccourcis clavier
...
@@ -118,7 +118,7 @@ FocusScope { // Permet de capter le focus pour les raccourcis clavier
Text
{
Text
{
id
:
score
id
:
score
text
:
"
0
"
text
:
gameManager
.
score
color
:
"
white
"
color
:
"
white
"
font.bold
:
true
font.bold
:
true
font.pixelSize
:
25
font.pixelSize
:
25
...
...
This diff is collapsed.
Click to expand it.
applicationQT2048/gamemanager.cpp
+
65
−
3
View file @
ac866a43
...
@@ -3,11 +3,14 @@
...
@@ -3,11 +3,14 @@
#include
<QFileInfo>
#include
<QFileInfo>
#include
<QDebug>
#include
<QDebug>
GameManager
::
GameManager
(
QObject
*
parent
)
:
QObject
(
parent
),
grid
(
4
,
std
::
vector
<
int
>
(
4
,
0
))
{
GameManager
::
GameManager
(
QObject
*
parent
)
:
QObject
(
parent
),
grid
(
4
,
std
::
vector
<
int
>
(
4
,
0
))
,
m_score
(
0
),
m_gameOver
(
false
)
{
}
}
void
GameManager
::
moveLeft
()
{
void
GameManager
::
moveLeft
()
{
if
(
m_gameOver
)
{
// Si la partie est terminée, ne fais rien
return
;
}
for
(
int
i
=
0
;
i
<
4
;
i
++
)
{
for
(
int
i
=
0
;
i
<
4
;
i
++
)
{
std
::
vector
<
int
>
newRow
(
4
,
0
);
// Créer une nouvelle ligne vide
std
::
vector
<
int
>
newRow
(
4
,
0
);
// Créer une nouvelle ligne vide
int
newPos
=
0
;
// Position pour insérer un élément non nul (commence par le début)
int
newPos
=
0
;
// Position pour insérer un élément non nul (commence par le début)
...
@@ -42,13 +45,17 @@ void GameManager::moveLeft() {
...
@@ -42,13 +45,17 @@ void GameManager::moveLeft() {
grid
[
i
]
=
newRow
;
grid
[
i
]
=
newRow
;
}
}
emit
addRandomElement
();
emit
addRandomElement
();
m_gameOver
=
isGameOver
();
//Mettre a jour l'état et l'enregistrer
//Mettre a jour l'état et l'enregistrer
historyArray
.
append
(
gridToJsonArray
());
historyArray
.
append
(
gridToJsonArray
());
emit
gridChanged
();
emit
gridChanged
();
emit
calculscore
();
}
}
void
GameManager
::
moveRight
()
{
void
GameManager
::
moveRight
()
{
if
(
m_gameOver
)
{
// Si la partie est terminée, ne fais rien
return
;
}
for
(
int
i
=
0
;
i
<
4
;
i
++
)
{
for
(
int
i
=
0
;
i
<
4
;
i
++
)
{
std
::
vector
<
int
>
newRow
(
4
,
0
);
// Créer une nouvelle ligne vide
std
::
vector
<
int
>
newRow
(
4
,
0
);
// Créer une nouvelle ligne vide
int
newPos
=
3
;
// Position pour insérer un élément non nul (commence par la fin)
int
newPos
=
3
;
// Position pour insérer un élément non nul (commence par la fin)
...
@@ -83,16 +90,21 @@ void GameManager::moveRight() {
...
@@ -83,16 +90,21 @@ void GameManager::moveRight() {
grid
[
i
]
=
newRow
;
grid
[
i
]
=
newRow
;
}
}
emit
addRandomElement
();
emit
addRandomElement
();
m_gameOver
=
isGameOver
();
//Mettre a jour l'état et l'enregistrer
//Mettre a jour l'état et l'enregistrer
historyArray
.
append
(
gridToJsonArray
());
historyArray
.
append
(
gridToJsonArray
());
emit
gridChanged
();
emit
gridChanged
();
emit
calculscore
();
}
}
void
GameManager
::
moveUp
()
{
void
GameManager
::
moveUp
()
{
if
(
m_gameOver
)
{
// Si la partie est terminée, ne fais rien
return
;
}
// Implémentation du mouvement vers le haut
// Implémentation du mouvement vers le haut
for
(
int
j
=
0
;
j
<
4
;
j
++
)
{
for
(
int
j
=
0
;
j
<
4
;
j
++
)
{
std
::
vector
<
int
>
newCol
(
4
,
0
);
// Créer une nouvelle ligne vide
std
::
vector
<
int
>
newCol
(
4
,
0
);
// Créer une nouvelle ligne vide
...
@@ -131,13 +143,18 @@ void GameManager::moveUp() {
...
@@ -131,13 +143,18 @@ void GameManager::moveUp() {
}
}
emit
addRandomElement
();
emit
addRandomElement
();
m_gameOver
=
isGameOver
();
//Mettre a jour l'état et l'enregistrer
//Mettre a jour l'état et l'enregistrer
historyArray
.
append
(
gridToJsonArray
());
historyArray
.
append
(
gridToJsonArray
());
emit
gridChanged
();
emit
gridChanged
();
emit
calculscore
();
}
}
void
GameManager
::
moveDown
()
{
void
GameManager
::
moveDown
()
{
if
(
m_gameOver
)
{
// Si la partie est terminée, ne fais rien
return
;
}
// Implémentation du mouvement vers le bas
// Implémentation du mouvement vers le bas
for
(
int
j
=
0
;
j
<
4
;
j
++
)
{
for
(
int
j
=
0
;
j
<
4
;
j
++
)
{
std
::
vector
<
int
>
newCol
(
4
,
0
);
// Créer une nouvelle ligne vide
std
::
vector
<
int
>
newCol
(
4
,
0
);
// Créer une nouvelle ligne vide
...
@@ -176,10 +193,12 @@ void GameManager::moveDown() {
...
@@ -176,10 +193,12 @@ void GameManager::moveDown() {
}
}
emit
addRandomElement
();
emit
addRandomElement
();
m_gameOver
=
isGameOver
();
//Mettre a jour l'état et l'enregistrer
//Mettre a jour l'état et l'enregistrer
historyArray
.
append
(
gridToJsonArray
());
historyArray
.
append
(
gridToJsonArray
());
emit
gridChanged
();
emit
gridChanged
();
emit
calculscore
();
}
}
...
@@ -187,10 +206,15 @@ void GameManager::restartGame(QString partieName) {
...
@@ -187,10 +206,15 @@ void GameManager::restartGame(QString partieName) {
//demander pour sauvergarder la partie précédente
//demander pour sauvergarder la partie précédente
<<<<<<<
HEAD
if
(
partieName
==
"false"
){
if
(
partieName
==
"false"
){
}
else
{
}
else
{
enregistrerPartie
(
partieName
);
enregistrerPartie
(
partieName
);
};
};
=======
enregistrerPartie
(
"partie1"
);
m_gameOver
=
false
;
>>>>>>>
e22daca7a3c09bd0a515d12093b0cbf7e3e4e152
// Réinitialisation des variables
// Réinitialisation des variables
...
@@ -221,6 +245,7 @@ void GameManager::restartGame(QString partieName) {
...
@@ -221,6 +245,7 @@ void GameManager::restartGame(QString partieName) {
//Mettre a jour l'état et l'enregistrer
//Mettre a jour l'état et l'enregistrer
historyArray
.
append
(
gridToJsonArray
());
historyArray
.
append
(
gridToJsonArray
());
emit
gridChanged
();
emit
gridChanged
();
emit
calculscore
();
}
}
...
@@ -244,7 +269,7 @@ void GameManager::addRandomElement() {
...
@@ -244,7 +269,7 @@ void GameManager::addRandomElement() {
int
row
=
emptyCells
[
randomIndex
].
first
;
int
row
=
emptyCells
[
randomIndex
].
first
;
int
col
=
emptyCells
[
randomIndex
].
second
;
int
col
=
emptyCells
[
randomIndex
].
second
;
// Placer un '2' dans cette case
// Placer un '2'
ou un '4'
dans cette case
int
randValue
=
std
::
rand
()
%
100
;
int
randValue
=
std
::
rand
()
%
100
;
if
(
randValue
<
75
)
{
if
(
randValue
<
75
)
{
grid
[
row
][
col
]
=
2
;
grid
[
row
][
col
]
=
2
;
...
@@ -316,6 +341,7 @@ void GameManager::undo() {
...
@@ -316,6 +341,7 @@ void GameManager::undo() {
}
}
emit
gridChanged
();
// Notifiez que la grille a changé
emit
gridChanged
();
// Notifiez que la grille a changé
emit
calculscore
();
}
}
}
}
...
@@ -384,6 +410,42 @@ void GameManager::chargerPartie(QString partieName){
...
@@ -384,6 +410,42 @@ void GameManager::chargerPartie(QString partieName){
file
.
close
();
file
.
close
();
}
}
int
GameManager
::
score
()
const
{
return
m_score
;
// Retourne le score actuel
}
<<<<<<<
HEAD
=======
void
GameManager
::
calculscore
(){
if
(
m_gameOver
)
{
// Si la partie est terminée, ne calcule pas le score
return
;
}
m_score
=
0
;
// Parcourir la grille pour additionner toute les cases
for
(
int
i
=
0
;
i
<
4
;
i
++
)
{
for
(
int
j
=
0
;
j
<
4
;
j
++
)
{
m_score
+=
grid
[
i
][
j
];
}
}
emit
scoreChanged
();
}
bool
GameManager
::
isGameOver
()
{
for
(
int
i
=
0
;
i
<
4
;
i
++
)
{
for
(
int
j
=
0
;
j
<
4
;
j
++
)
{
if
(
grid
[
i
][
j
]
==
0
)
{
// Une case vide est trouvée, donc le jeu n'est pas fini
return
false
;
}
// Vérifier les possibilités de fusion (horizontale et verticale)
if
((
i
<
3
&&
grid
[
i
][
j
]
==
grid
[
i
+
1
][
j
])
||
(
j
<
3
&&
grid
[
i
][
j
]
==
grid
[
i
][
j
+
1
]))
{
return
false
;
}
}
}
return
true
;
// Si la grille est pleine et aucune fusion n'est possible, la partie est terminée
}
>>>>>>>
e22daca7a3c09bd0a515d12093b0cbf7e3e4e152
This diff is collapsed.
Click to expand it.
applicationQT2048/gamemanager.h
+
8
−
0
View file @
ac866a43
...
@@ -16,6 +16,7 @@ class GameManager : public QObject
...
@@ -16,6 +16,7 @@ class GameManager : public QObject
Q_PROPERTY
(
QVector
<
int
>
gridValues
READ
getGridValues
NOTIFY
gridChanged
)
Q_PROPERTY
(
QVector
<
int
>
gridValues
READ
getGridValues
NOTIFY
gridChanged
)
Q_PROPERTY
(
QStringList
partieHistorique
READ
getPartieHistorique
NOTIFY
historiqueChanged
)
Q_PROPERTY
(
QStringList
partieHistorique
READ
getPartieHistorique
NOTIFY
historiqueChanged
)
Q_PROPERTY
(
int
score
READ
score
NOTIFY
scoreChanged
)
public:
public:
explicit
GameManager
(
QObject
*
parent
=
nullptr
);
explicit
GameManager
(
QObject
*
parent
=
nullptr
);
...
@@ -26,24 +27,31 @@ public:
...
@@ -26,24 +27,31 @@ public:
Q_INVOKABLE
void
restartGame
(
QString
partieName
);
Q_INVOKABLE
void
restartGame
(
QString
partieName
);
Q_INVOKABLE
void
undo
();
Q_INVOKABLE
void
undo
();
Q_INVOKABLE
void
chargerPartie
(
QString
partieName
);
Q_INVOKABLE
void
chargerPartie
(
QString
partieName
);
bool
isGameOver
();
QVector
<
int
>
getGridValues
()
const
;
QVector
<
int
>
getGridValues
()
const
;
QStringList
getPartieHistorique
();
QStringList
getPartieHistorique
();
int
score
()
const
;
private:
private:
std
::
vector
<
std
::
vector
<
int
>>
grid
;
std
::
vector
<
std
::
vector
<
int
>>
grid
;
QJsonArray
historyArray
;
QJsonArray
historyArray
;
int
m_score
;
Q_INVOKABLE
void
addRandomElement
();
Q_INVOKABLE
void
addRandomElement
();
Q_INVOKABLE
void
enregistrerPartie
(
QString
partieName
);
Q_INVOKABLE
void
enregistrerPartie
(
QString
partieName
);
Q_INVOKABLE
void
calculscore
();
QJsonArray
gridToJsonArray
();
QJsonArray
gridToJsonArray
();
bool
m_gameOver
;
signals:
signals:
void
gridChanged
();
void
gridChanged
();
void
historiqueChanged
();
void
historiqueChanged
();
void
scoreChanged
();
};
};
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment