Skip to content
Snippets Groups Projects
Select Git revision
  • 1128a83328c3a0a24e5f6079a7b3af29d56ad5c5
  • master default protected
2 results

ligne.cpp

Blame
  • ligne.cpp 1.78 KiB
    #include "ligne.h"
    #include "fullligneexception.h"
    #include "emptyligneexception.h"
    <<<<<<< HEAD
    #include "iostream"
    #include <cctype>
    using namespace std;
    =======
    #include <QString>
    >>>>>>> e6f8b0eb13207f90ae4ed37356359f928e941f2a
    
    Ligne::Ligne(string bonmot) : bonmot(bonmot), positionCurseur(0) {}
    
    void Ligne::initLigne() {
        for (int i = 0; i < bonmot.length(); i++) {
            contenu.push_back(new VraieCase());
        }
    }
    
    bool Ligne::isGameCleared() {
        int nbjustes = 0;
        for (int i=0;i<bonmot.length();i++) {
            int etat = (tolower(contenu[i]->getLetter()) == bonmot[i]) ? 0 : 1;
            etat += Ligne::dansMot(tolower(contenu[i]->getLetter()), bonmot);
            contenu[i]->setEtat(etat);
            cout << contenu[i]->getLetter() << bonmot[i] << etat << endl;
        }
        show();
        return (nbjustes == 2*bonmot.length());
    }
    
    void Ligne::show() {
        cout << "La ligne : |";
        for (int i=0;i<bonmot.length();i++) {
            if (contenu[i]->getEtat() == 1) {cout << "\033[33m";}
            if (contenu[i]->getEtat() == 2) {cout << "";}
            cout << contenu[i]->getLetter();
            if (contenu[i]->getEtat() >= 1) cout << "\033[31m";
        }
        cout << "|" << endl;
    }
    
    bool Ligne::dansMot(char lettre, string mot) {
        for (int i=0;i<mot.length();i++) {
            if (mot[i] == lettre) {
                return true;
            }
        }
        return false;
    }
    
    void Ligne::entreLettre(char lettre) {
        if (positionCurseur == bonmot.length()) {
            throw FullLigneException();
        }
        contenu[positionCurseur]->setLetter(QChar(lettre));  // Conversion ici
        positionCurseur++;
    }
    
    void Ligne::supprLettre() {
        if (positionCurseur == 0) {
            throw EmptyLigneException();
        }
        cout << "Suppression d'une lettre" << endl;
        contenu[positionCurseur - 1]->setLetter(QChar(' '));
        positionCurseur--;
    }