From 75bfd6d23f3d66ebe449895120bde768ad3b6703 Mon Sep 17 00:00:00 2001 From: Yanis Dziki <yanis.dziki@ens-lyon.fr> Date: Mon, 31 Mar 2025 11:23:27 +0200 Subject: [PATCH] =?UTF-8?q?Modification=20main.cpp=20et=20cr=C3=A9ation=20?= =?UTF-8?q?classe=20Q=5FObject=20Jeu=20pour=20r=C3=A9cup=C3=A9rer=20les=20?= =?UTF-8?q?infos=20du=20brain=20et=20pouvoir=20afficher=20le=20mot=20chois?= =?UTF-8?q?i?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- motus/CMakeLists.txt | 1 + motus/GameWindow.qml | 32 +++++++++++++++++++++++++++++++- motus/jeu.cpp | 23 +++++++++++++++++++++++ motus/jeu.h | 23 +++++++++++++++++++++++ motus/main.cpp | 28 +++++++--------------------- 5 files changed, 85 insertions(+), 22 deletions(-) create mode 100644 motus/jeu.cpp create mode 100644 motus/jeu.h diff --git a/motus/CMakeLists.txt b/motus/CMakeLists.txt index 108da78..a1e6262 100644 --- a/motus/CMakeLists.txt +++ b/motus/CMakeLists.txt @@ -34,6 +34,7 @@ qt_add_qml_module(appmotus QML_FILES Bouton.qml RESOURCES QML_FILES GameWindow.qml + SOURCES jeu.h jeu.cpp ) # Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1. diff --git a/motus/GameWindow.qml b/motus/GameWindow.qml index abdd6f3..6f5bf26 100644 --- a/motus/GameWindow.qml +++ b/motus/GameWindow.qml @@ -1,3 +1,6 @@ + + + import QtQuick Item { @@ -9,11 +12,38 @@ Item { color: "#ffffff" anchors.fill: parent + // Zone où le mot est affiché + Row { + id: motDisplay + spacing: 10 + anchors.top: parent.top + anchors.horizontalCenter: parent.horizontalCenter + anchors.topMargin: 50 + + Repeater { + model: jeu.motAffiche.split(" ") // Divise la chaîne en lettres et "_" + + Rectangle { + width: 50 + height: 50 + border.color: "#000000" + border.width: 2 + radius: 5 + color: "#eeeeee" + + Text { + anchors.centerIn: parent + text: modelData + font.pixelSize: 20 + } + } + } + } + Clavier { id: clavier x: 225 y: 594 } } - } diff --git a/motus/jeu.cpp b/motus/jeu.cpp new file mode 100644 index 0000000..47e8423 --- /dev/null +++ b/motus/jeu.cpp @@ -0,0 +1,23 @@ +#include "jeu.h" +#include <iostream> + +Jeu::Jeu(QObject *parent) : QObject(parent), brain("./") { + brain.setFichierDico("words_alpha.txt"); + brain.setNombreEssais(5); + brain.setTailleMots(5); + brain.initGame(); + + mot = QString::fromStdString(brain.getMot()); // ✅ Conversion std::string → QString + + std::cout << "Mot choisi : " << mot.toStdString() << std::endl; +} + +QString Jeu::getMotAffiche() { + if (mot.isEmpty()) return ""; + + QString affichage = QString(mot.at(0)); + for (int i = 1; i < mot.length(); ++i) { + affichage += " _"; + } + return affichage; +} diff --git a/motus/jeu.h b/motus/jeu.h new file mode 100644 index 0000000..6a765dc --- /dev/null +++ b/motus/jeu.h @@ -0,0 +1,23 @@ +#ifndef JEU_H +#define JEU_H + +#include <QObject> +#include "brain.h" + +class Jeu : public QObject { + Q_OBJECT + Q_PROPERTY(QString motAffiche READ getMotAffiche NOTIFY motChanged) + +public: + explicit Jeu(QObject *parent = nullptr); + QString getMotAffiche(); + +signals: + void motChanged(); + +private: + Brain brain; + QString mot; +}; + +#endif // JEU_H diff --git a/motus/main.cpp b/motus/main.cpp index d353e6d..fb39116 100644 --- a/motus/main.cpp +++ b/motus/main.cpp @@ -1,30 +1,16 @@ #include <QGuiApplication> #include <QQmlApplicationEngine> -#include <iostream> +#include <QQmlContext> +#include "jeu.h" -#include "brain.h" - -using namespace std; - -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); - QQmlApplicationEngine engine; - QObject::connect( - &engine, - &QQmlApplicationEngine::objectCreationFailed, - &app, - []() { QCoreApplication::exit(-1); }, - Qt::QueuedConnection); - engine.loadFromModule("motus", "Main"); - Brain brain("./"); - brain.setFichierDico("words_alpha.txt"); - brain.setNombreEssais(5); - brain.setTailleMots(5); - brain.initGame(); - cout << brain.getMot() << endl; + Jeu jeu; + engine.rootContext()->setContextProperty("jeu", &jeu); + + engine.loadFromModule("motus", "Main"); return app.exec(); } -- GitLab