lundi 7 mai 2018

Checking a sudoku square validity in C through boolean

So I made this code with a friend to try and create a sudoku, he suggested creating the grid recursively, and to check the validity thanks to boolean rules. However, I'm a little unsure and hitting the vector subscript out of range

bool Valide() const {
    //Pour un puzzle sudoku, on doit faire une fonction checkSquare
    for (int i = 0; i < tailleligne_; i++) {
        if (!LigneValide(i) || !ColonneValide(i)) {
            return false;
        }
    }

    return true;
}

and

    bool LigneValide(int ligne) const {
    vector<bool> numPresent(tailleligne_ + 1, false);

    for (int i = 0; i < tailleligne_; i++) {
        int element = grilleElements_[ligne][i];

        if (element != 0) {
            if (numPresent[element]) {
                return false;
            }
            else {
                numPresent[element] = true;
            }
        }
        // ne fait rien si element == 0
    }

    return true;
}
// vérifie que les nombres sont utilisés seulement une fois dans la colonne
// assume que les valeurs sont en grilleElements_ en [1, tailleligne_]
// retour faux si valeur répétées dans la colonne, vrai sinon.




bool ColonneValide(int col) const {
    vector<bool> numPresent(tailleligne_ + 1, false);

    for (int i = 0; i < tailleligne_; i++) {
        int element = grilleElements_[i][col];

        if (element != 0) {
            if (numPresent[element]) {
                return false;
            }
            else {
                numPresent[element] = true;
            }
        }
        else {
            // si l'element ==0, il n'y a plus rien a vérifier de toute façon donc on peut laisser la boucle
            break;
        }
    }

    return true;
}

tailleligne_ is basically the size of lines and columns. Anyways I tried to do the checksquare the same way as the other 2, with two if loops, with i+3 and j+3, then use a element2 and return false if numPresent[element] && numPresent[element2] but in that case the code breaks and it says I hit the vector subscript out of range. Any help is appreciated, thanks a lot! :)

Aucun commentaire:

Enregistrer un commentaire