Skip to content
Snippets Groups Projects
Select Git revision
  • 2bb341134986ab8ebfae90870ce247ef5c54095c
  • main default protected
2 results

README.md

Blame
  • Main.qml 24.81 KiB
    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Controls 2.12
    // Optionnel : utilisez un style non natif pour autoriser la personnalisation
    // import QtQuick.Controls.Material 2.12
    
    Window {
        id: mainWindow
        width: Screen.width
        height: Screen.height
        visible: true
        visibility: Window.Maximized
        title: qsTr("Motus")
    
        // Propriétés de gestion
        property int indice_case: 0
        property int current_essai: 0
        property var caseArray: []   // Rempli lors de la création de la grille
        property var case_focus: null
    
        property int nb_lettres: 5
        property var arrayTry: [5,6,6,7,7,8,8,8,8]
        property int nb_essais: arrayTry[nb_lettres - 4]
        property string mot: ""
        property var mot_split: []
    
        property var keysArray: []
    
        function changeKeyColor(letter, newColor) {
            for (var i = 0; i < keysArray.length; i++) {
                if (keysArray[i].keyLetter === letter) {
                    keysArray[i].keyColor = newColor;
                }
            }
        }
    
        // Rectangle principal qui couvre la fenêtre
        Rectangle {
            id: mainRect
            anchors.fill: parent
            color: "#323232"
            focus: true
    
            // Gestion des touches du clavier physique
            Keys.onPressed: function(event) {
                if (mot !== "" && !motusGame.loosetry && !motusGame.win && !motusGame.loosetime) {
                    // Saisie d'une lettre (de A à Z)
                    if (/^[a-zA-Z]$/.test(event.text)) {
                        if (indice_case < nb_lettres) {
                            var cellIndex = current_essai * nb_lettres + indice_case;
                            caseArray[cellIndex]._textText = event.text.toUpperCase();
                            indice_case++;
                            event.accepted = true;
                        }
                    }
                    // Gestion de la touche Backspace
                    else if (event.key === Qt.Key_Backspace) {
                        if (indice_case > 0) {
                            indice_case--;
                            var cellIndex = current_essai * nb_lettres + indice_case;
                            caseArray[cellIndex]._textText = "";
                            event.accepted = true;
                        }
                    }
                    // Passage à la ligne suivante avec la touche Entrée
                    else if (event.key === Qt.Key_Return) {
                        if (indice_case === nb_lettres) {
                            // Construire le mot entré en majuscules
                            var candidate = [];
                            for (var i = 0; i < nb_lettres; i++){
                                candidate[i] = caseArray[current_essai * nb_lettres + i]._textText.toUpperCase();
                            }
                            var mot_entre = candidate.join("");
                            console.log("Mot entré :", mot_entre);
    
                            if (motusGame.existWord(mot_entre)) {
                                // Le mot existe, on procède à l'évaluation avec gestion des doublons
                                var target = mot_split.map(function(letter) {
                                    return letter.toUpperCase();
                                });
    
                                // Construire un tableau de fréquence des lettres du mot cible
                                var targetCounts = {};
                                for (var i = 0; i < nb_lettres; i++) {
                                    var letter = target[i];
                                    targetCounts[letter] = (targetCounts[letter] || 0) + 1;
                                }
    
                                // Tableau pour stocker la couleur à appliquer pour chaque case.
                                // Par défaut, on ne change pas le style (chaîne vide)
                                var resultColors = new Array(nb_lettres);
    
                                // Première passe : marquer en vert les lettres bien placées
                                for (var i = 0; i < nb_lettres; i++) {
                                    if (candidate[i] === target[i]) {
                                        resultColors[i] = "#42cc3d"; // vert
                                        targetCounts[candidate[i]]--;
                                    } else {
                                        resultColors[i] = "";
                                    }
                                }
    
                                // Vérifier si le mot entré correspond exactement au mot à deviner
                                if (mot_entre.toUpperCase() === mot.toUpperCase()) {
                                    motusGame.setWinbool(true);
                                    console.log("✅ Victoire !");
                                }
    
                                // Deuxième passe : marquer en jaune les lettres présentes mais mal placées
                                for (var i = 0; i < nb_lettres; i++) {
                                    if (resultColors[i] === "") {
                                        var letter = candidate[i];
                                        if (targetCounts[letter] && targetCounts[letter] > 0) {
                                            resultColors[i] = "#f0d437"; // jaune
                                            targetCounts[letter]--;
                                        }
                                    }
                                }
    
                                // Appliquer les couleurs aux cases et aux touches si une couleur a été définie
                                for (var i = 0; i < nb_lettres; i++) {
                                    var case_verif = caseArray[current_essai * nb_lettres + i];
                                    if (resultColors[i] !== "") {
                                        case_verif.rectangleColor = resultColors[i];
                                        case_verif.rectangleBorderColor = resultColors[i];
                                        changeKeyColor(candidate[i], resultColors[i]);
                                    }
                                }
    
                                // Passage à la ligne suivante et mise à jour du focus
                                if (current_essai < nb_essais - 1) {
                                    current_essai++;
                                    indice_case = 0;
                                    case_focus = caseArray[current_essai * nb_lettres];
                                } else {
                                    // Fin de partie : désactiver l'interface ou afficher un message
                                    if (mot_entre.toUpperCase() !== mot.toUpperCase()) {
                                        motusGame.setLoosetrybool(true);
                                        console.log("loosetry=true");
                                    }
    
                                }
                            } else {
                                // Le mot n'existe pas dans le dictionnaire : colorer toute la ligne en rouge
                                for (var i = 0; i < nb_lettres; i++) {
                                    var case_verif = caseArray[current_essai * nb_lettres + i];
                                    case_verif.rectangleColor = "#cf1b2a";
                                    case_verif.rectangleBorderColor = "#cf1b2a";
                                }
                                revertTimer.start();
                            }
                            event.accepted = true;
                        }
                    }
                }
            }
    
            // Timer pour réinitialiser le style de la ligne en cas de mot inexistant
            Timer {
                id: revertTimer
                interval: 1000  // 1 seconde
                repeat: false
                onTriggered: {
                    console.log("Réinitialisation du style de la ligne");
                    for (var i = 0; i < nb_lettres; i++) {
                        var case_verif = caseArray[current_essai * nb_lettres + i];
                        case_verif.rectangleColor = "#323232";
                        case_verif.rectangleBorderColor = "#ffffff";
                    }
                }
            }
    
            Row {
                id: main_row
                anchors.fill: parent
                spacing: 150
    
                Rectangle {
                    id: rectangle_buttons
                    width: 400
                    height: 850
                    color: "#323232"
                    border.width: 0
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.left: parent.left
                    anchors.leftMargin: 0.05 * Window.width
    
                    Column {
                        id: column_buttons
                        width: 400
                        height: 850
                        spacing: 20
    
                        Choosebutton {
                            id: choosebutton
                            anchors.horizontalCenter: parent.horizontalCenter
                            _textText: "Generate a word"
                            Case {
                                id: case3
                                x: 131
                                y: 8
                                _textText: "mot test"
                            }
                            MouseArea {
                                anchors.fill: parent
                                cursorShape: Qt.PointingHandCursor
                                enabled: motusGame && (motusGame.win || motusGame.loosetry || motusGame.loosetime || !motusGame.debut)
                                onClicked: {
    
                                    // 1. Réinitialiser les indices AVANT de tout
                                    current_essai = 0;
                                    indice_case = 0;
    
                                    // 2. Vider toutes les cases
                                    for (var i = 0; i < caseArray.length; i++) {
                                        caseArray[i]._textText = "";
                                        caseArray[i].rectangleColor = "#323232";
                                        caseArray[i].rectangleBorderColor = "#ffffff";
                                    }
    
                                        // 3. Vider les touches clavier
                                    for (var j = 0; j < keysArray.length; j++) {
                                        keysArray[j].keyColor = "#808080";
                                    }
    
                                    // Début d'une partie, désactivation des boutons de régénération, etc.
                                    motusGame.debut=true;
                                    mot = motusGame.getRandomWord(nb_lettres);
                                    motusGame.setLoosetrybool(false);
                                    motusGame.setLoosetimebool(false);
                                    motusGame.setWinbool(false);
                                    motusGame.startTimer();
                                    case3._textText = mot;
                                    mot_split = mot.split("");
                                    console.log(mot_split);
    
    
                                }
                            }
                        }
    
                        Rectangle {
                            id: big_rectangle_langue
                            width: 250
                            height: 300
                            color: "#323232"
                            radius: 10
                            border.color: "#ffffff"
                            border.width: 3
                            anchors.horizontalCenter: parent.horizontalCenter
    
                            Column {
                                id: column
                                anchors.fill: parent
                                anchors.margins: 10
                                spacing: 15
    
                                Text {
                                    id:text_langue
                                    y: 100
                                    text: "Language"
                                    font.pixelSize: 25
                                    color: "white"
                                    horizontalAlignment: Text.AlignHCenter
                                    anchors.horizontalCenter: parent.horizontalCenter
                                }
    
                                LangageButton {
                                    id: langageButton
                                    anchors.horizontalCenter: parent.horizontalCenter
                                    _textText: "Français"
                                    rectangleColor: "#7a7a7a"
                                    MouseArea {
                                        enabled: motusGame && (motusGame.win || motusGame.loosetry || motusGame.loosetime || !motusGame.debut)
                                        anchors.left: parent.left
                                        anchors.top: parent.top
                                        anchors.bottom: parent.bottom
                                        anchors.horizontalCenter: parent.horizontalCenter
                                        cursorShape: Qt.PointingHandCursor
                                        onClicked: {
    
                                            motusGame.dictionnaryChoosed = "Motus\\mots_francais_bis.txt";
                                            langageButton2.rectangleColor= "#7a7a7a";
                                            langageButton.rectangleColor= "green";
                                            choosebutton._textText="Générer un mot";
                                            text_langue.text="Langage"
                                            text_choix_nb_lettres.text="Nombre de lettres"
                                            numberLetterBtn.firstItemText="Aléatoire"
    
                                        }
                                    }
                                }
    
                                LangageButton {
                                    id: langageButton2
                                    anchors.horizontalCenter: parent.horizontalCenter
                                    _textText: "English"
                                    rectangleColor: "green"
                                    MouseArea {
                                        enabled: motusGame && (motusGame.win || motusGame.loosetry || motusGame.loosetime || !motusGame.debut)
                                        anchors.fill: parent
                                        cursorShape: Qt.PointingHandCursor
                                        onClicked: {
                                            motusGame.dictionnaryChoosed = "Motus\\words_alpha.txt";
                                            langageButton2.rectangleColor= "green";
                                            langageButton.rectangleColor= "#7a7a7a";
                                            choosebutton._textText="Generate a word;"
                                            text_langue.text="Language"
                                            text_choix_nb_lettres.text="Number of letters"
                                            numberLetterBtn.firstItemText="Random"
    
                                        }
                                    }
                                }
                            }
                        }
    
                        Rectangle {
                            id: big_rectangle_nb_lettres
                            width: 250
                            height: 300
                            color: "#323232"
                            radius: 10
                            border.color: "#ffffff"
                            border.width: 3
                            anchors.horizontalCenter: parent.horizontalCenter
    
                            Column {
                                id: colonne_nb_lettres
                                anchors.fill: parent
                                spacing: 50
    
                                Text {
                                    id: text_choix_nb_lettres
                                    text: "Number of letters"
                                    font.pixelSize: 22
                                    anchors.horizontalCenter: parent.horizontalCenter
                                    color: "white"
                                }
    
                                NumberLetterButton {
                                    id: numberLetterBtn
                                    firstItemText: "Random"
                                    width: parent.width
                                    height: 60
    
                                }
                            }
                        }
                    }
                }
    
                Rectangle {
                    id: rectangle_center
                    width: 700
                    height: 900
                    color: "#323232"
                    border.width: 0
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.horizontalCenter: parent.horizontalCenter
    
                    Column {
                        id: center_column
                        x: 0
                        y: 36
                        width: 716
                        height: 864
                        spacing: 30
    
                        Rectangle {
                            id: rectangle_titre
                            width: 200
                            height: 70
                            color: "#323232"
                            border.width: 0
                            anchors.horizontalCenter: parent.horizontalCenter
    
                            Text {
                                id: _text2
                                text: qsTr("MOTUS")
                                anchors.verticalCenter: parent.verticalCenter
                                font.pixelSize: 50
                                color: "white"
                                font.family: "Tahoma"
                                font.bold: true
                                horizontalAlignment: Text.AlignHCenter
                                anchors.horizontalCenter: parent.horizontalCenter
                                anchors.top: parent.top
                            }
                        }
    
                        Rectangle {
                            id: rectangle_grid
                            width: 450
                            height: 500
                            color: "#323232"
                            border.width: 0
                            anchors.verticalCenter: parent.verticalCenter
                            anchors.horizontalCenter: parent.horizontalCenter
    
                            Grid {
                                id: grid
                                x: -553
                                y: -430
                                anchors.left: parent.left
                                anchors.right: parent.right
                                anchors.top: parent.top
                                anchors.bottom: parent.bottom
                                anchors.leftMargin: 39
                                anchors.rightMargin: -39
                                anchors.topMargin: -80
                                anchors.bottomMargin: 80
                                spacing: 5
                                rows: 6
                                columns: 5
    
                                Repeater {
                                    id: caseRepeater
                                    model: 30
                                    delegate: Case {
                                        objectName: "caseDelegate"
                                    }
                                }
                            }
                        }
    
                        Rectangle {
                            id: rectangle1
                            width: 600
                            height: 230
                            anchors.horizontalCenter: parent.horizontalCenter
                            color: "#323232"
                            radius: 10
                            border.color: "#ffffff"
                            border.width: 5
                            anchors.bottom: parent.bottom
                            anchors.bottomMargin: 50
    
                            Column {
                                anchors.fill: parent
                                anchors.margins: 10
                                spacing: 6
                                topPadding: rectangle1.height * 0.05
                                bottomPadding: rectangle1.height * 0.05
    
                                Repeater {
                                    model: [
                                        ["A", "Z", "E", "R", "T", "Y", "U", "I", "O", "P"],
                                        ["Q", "S", "D", "F", "G", "H", "J", "K", "L"],
                                        ["↑", "W", "X", "C", "V", "B", "N", "M", "<"]
                                    ]
                                    delegate: Row {
                                        spacing: 5
                                        anchors.horizontalCenter: parent.horizontalCenter
    
                                        Repeater {
                                            model: modelData
                                            delegate: Rectangle {
                                                id: keyRect
                                                property string keyLetter: modelData  // Stocke la lettre associée
                                                property color keyColor: "#808080"     // Couleur par défaut
    
                                                width: (modelData === "Entrée" || modelData === "<") ? 80 : 50
                                                height: 60
                                                color: keyColor
                                                radius: 5
                                                border.color: keyColor
    
                                                Text {
                                                    anchors.centerIn: parent
                                                    text: modelData
                                                    font.pixelSize: 30
                                                    color: "white"
                                                    font.family: "Tahoma"
                                                    font.bold: true
                                                }
                                                MouseArea {
                                                    anchors.fill: parent
                                                    cursorShape: Qt.PointingHandCursor
                                                    onClicked: {
                                                        if (modelData === "<") {
                                                            if (indice_case > 0) {
                                                                indice_case--;
                                                                caseArray[indice_case]._textText = "";
                                                                case_focus = caseArray[indice_case];
                                                            }
                                                        } else if (modelData === "↑") {
                                                            // Action pour la touche "↑" si nécessaire
                                                        } else {
                                                            if (indice_case < caseArray.length) {
                                                                caseArray[indice_case]._textText = modelData.toUpperCase();
                                                                indice_case++;
                                                                if (indice_case < caseArray.length)
                                                                    case_focus = caseArray[indice_case];
                                                            }
                                                        }
                                                    }
                                                }
                                                Component.onCompleted: {
                                                    mainWindow.keysArray.push(keyRect);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
    
                Rectangle {
                    id: rectangle_right
                    width: 400
                    height: 850
                    color: "#323232"
                    border.width: 0
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.right: parent.right
                    anchors.rightMargin: 0.05 * Window.width
    
                    Row {
                        y: 50
                        anchors.horizontalCenter: parent.horizontalCenter
                        spacing: 20
    
                        MotusTimer {
                            id: motusTimer
                            width: 100
                            height: 100
                        }
                    }
                }
            }
        }
    
        Component.onCompleted: {
            caseArray = [];
            for (var i = 0; i < caseRepeater.count; i++) {
                caseArray.push(caseRepeater.itemAt(i));
            }
            if (caseArray.length > 0) {
                case_focus = caseArray[0];
            }
        }
    
        Text {
            y: 32
            text: "Temps écoulé ! Mot : "+ mot
            visible: motusGame && motusGame.loosetime
            color: "red"
            font.pixelSize: 30
            anchors.horizontalCenter: parent.horizontalCenter
        }
    
        Text {
            y: 32
            text: "Nombre d'essais maximum atteint ! Mot : "+ mot
            visible: motusGame && motusGame.loosetry
            color: "red"
            font.pixelSize: 30
            anchors.horizontalCenter: parent.horizontalCenter
        }
    
        Text {
            y: 32
            text: "Gagné !"
            visible: motusGame && motusGame.win
            color: "green"
            font.pixelSize: 30
            anchors.horizontalCenter: parent.horizontalCenter
        }
    }