From 47b210cf1e72faddc27138ff6c48c4e2df0914d0 Mon Sep 17 00:00:00 2001 From: Yanis Dziki <yanis.dziki@ens-lyon.fr> Date: Fri, 11 Apr 2025 22:08:12 +0200 Subject: [PATCH] Omg les couleurs --- motus/GameWindow.qml | 17 ++++++++++++++--- motus/brain.cpp | 11 +++++++++++ motus/brain.h | 1 + motus/jeu.cpp | 9 +++++++++ motus/lettermodel.cpp | 14 +++++++++++++- motus/lettermodel.h | 5 ++++- motus/ligne.cpp | 9 +++++++++ motus/ligne.h | 2 ++ 8 files changed, 63 insertions(+), 5 deletions(-) diff --git a/motus/GameWindow.qml b/motus/GameWindow.qml index 79a1368..8fd720e 100644 --- a/motus/GameWindow.qml +++ b/motus/GameWindow.qml @@ -20,21 +20,32 @@ Item { height: 400 Repeater { - model: letterModel // <-- C++ ListModel exposé ici + model: letterModel + delegate: Rectangle { width: 60 height: 60 - color: "#dddddd" border.color: "#888" radius: 5 + color: { + switch (model.state) { + case 1: return "yellow"; + case 2: return "red"; + default: return "#dddddd"; + } + } + Text { anchors.centerIn: parent text: model.letter font.pixelSize: 24 } + + } } } + } Clavier { @@ -43,5 +54,5 @@ Item { y: 594 } } -} + diff --git a/motus/brain.cpp b/motus/brain.cpp index 3f09751..4c363d6 100644 --- a/motus/brain.cpp +++ b/motus/brain.cpp @@ -26,6 +26,17 @@ string Brain::getGrid() { return res; } + +string Brain::getGridStates() { + std::string res = ""; + for (int i = 0; i < mNbEssaisMax; ++i) { + res += lignes[i]->getEtats(); // chaque ligne donne une string d'états + } + return res; +} + + + vector<string> Brain::getTxtFiles() { vector<string> txtFiles; diff --git a/motus/brain.h b/motus/brain.h index 9f0ffe3..a597890 100644 --- a/motus/brain.h +++ b/motus/brain.h @@ -27,6 +27,7 @@ public: void supprLettre(); string getGrid(); vector<Ligne*> lignes; + string getGridStates(); private: string dicodir; diff --git a/motus/jeu.cpp b/motus/jeu.cpp index 56be84c..51395aa 100644 --- a/motus/jeu.cpp +++ b/motus/jeu.cpp @@ -27,6 +27,7 @@ void Jeu::onClavierClick(QString lettre) { if (currentIndex < 40) { // on évite de dépasser la grille m_letterModel->setLetter(currentIndex, lettre); + currentIndex++; } @@ -34,7 +35,15 @@ void Jeu::onClavierClick(QString lettre) { brain->entreLettre(lettre.toStdString()[0]); for (int index = 0; index < 40; index ++) { m_letterModel->setLetter(index, QString::fromLatin1(&brain->getGrid()[index], 1)); + + } + std::string etats = brain->getGridStates(); + for (int index = 0; index < 40; ++index) { + int etat = etats[index] - '0'; // convertit '0', '1', '2' → 0, 1, 2 + m_letterModel->setStates(index, etat); + } + // string lagrille = brain->getGrid(); // for (int index = 0; index < 40; index ++) { diff --git a/motus/lettermodel.cpp b/motus/lettermodel.cpp index bc63370..1b6d874 100644 --- a/motus/lettermodel.cpp +++ b/motus/lettermodel.cpp @@ -5,6 +5,7 @@ LetterModel::LetterModel(QObject *parent) { // Initialiser 40 lettres avec "_" m_letters.fill("_", 40); + m_states.fill(0,40); } int LetterModel::rowCount(const QModelIndex &parent) const { @@ -18,19 +19,30 @@ QVariant LetterModel::data(const QModelIndex &index, int role) const { if (role == LetterRole) return m_letters.at(index.row()); + else if (role == StateRole) + return m_states.at(index.row()); return QVariant(); } QHash<int, QByteArray> LetterModel::roleNames() const { return { - { LetterRole, "letter" } + { LetterRole, "letter" }, + { StateRole, "state" } }; } + void LetterModel::setLetter(int index, const QString &value) { if (index >= 0 && index < m_letters.size()) { m_letters[index] = value; emit dataChanged(this->index(index), this->index(index), { LetterRole }); } } + +void LetterModel::setStates(int index, int value) { + if (index >= 0 && index < m_states.size()) { + m_states[index] = value; + emit dataChanged(this->index(index), this->index(index), { StateRole }); + } +} diff --git a/motus/lettermodel.h b/motus/lettermodel.h index fe90feb..918451d 100644 --- a/motus/lettermodel.h +++ b/motus/lettermodel.h @@ -9,7 +9,8 @@ class LetterModel : public QAbstractListModel { public: enum Roles { - LetterRole = Qt::UserRole + 1 + LetterRole = Qt::UserRole + 1, + StateRole }; explicit LetterModel(QObject *parent = nullptr); @@ -19,7 +20,9 @@ public: QHash<int, QByteArray> roleNames() const override; Q_INVOKABLE void setLetter(int index, const QString &value); + Q_INVOKABLE void setStates(int index, int value); private: QVector<QString> m_letters; + QVector<int> m_states; }; diff --git a/motus/ligne.cpp b/motus/ligne.cpp index 71a17a0..edc9650 100644 --- a/motus/ligne.cpp +++ b/motus/ligne.cpp @@ -40,6 +40,15 @@ string Ligne::getMot() { return res; } +string Ligne::getEtats() { + string res =""; + for (int i=0; i<bonmot.length();i++) { + res = res + std::to_string(contenu[i]->getEtat()); + + } + return res; +} + void Ligne::show() { cout << "La ligne : |"; for (int i=0;i<bonmot.length();i++) { diff --git a/motus/ligne.h b/motus/ligne.h index 8c92162..57da94d 100644 --- a/motus/ligne.h +++ b/motus/ligne.h @@ -25,6 +25,8 @@ public: vector<VraieCase*> contenu; string getMot(); bool dansMot(char lettre, string mot); + string getEtats(); + private: string bonmot; -- GitLab