#Sudoku c++ logic problem

5 messages · Page 1 of 1 (latest)

urban perch
#

I have the following code for a Sudoku game but it is not solving the board correctly. Also the output files doesn't have the solution or the initial matrix stored in them. What can I change in the code to make it solve the board correctly without adding the same number on the same row and column and to store the initial board and the solution in the txt files?
An instance of this class is used in a QAbstratTableModel class that helps me to generate the grid and has a solveSudoku method that calls methods from SudokuGenerator.cpp, but the method is not solving the sudoku board correctly.

Here is the main class

#include "SudokuGenerator.h"
#include "QVector"

int main() {
    SudokuGenerator sudokuGenerator(1);
    sudokuGenerator.umplu();
    sudokuGenerator.scrie_initial();
    sudokuGenerator.scrie_solutie();
    sudokuGenerator.scrie_curent();
    
    int** gridData;
    
    QVector<QVector<int>> solution(9, QVector<int>(9));
    
    for (int i = 0; i < 9; ++i) {
        for (int j = 0; j < 9; ++j) {
            if (gridData[i][j] != 0) {
                solution[i][j] = gridData[i][j];
            } else {
                solution[i][j] = sudokuGenerator.get_matrice()[i][j];
            }
        }
    }
}
woven thicketBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

tiny plover
#

I'm having a bit of a hard time reading the code, because it isn't fully in English.

What can I change in the code to make it solve the board correctly without adding the same number on the same row and column and to store the initial board and the solution in the txt files?
So are you saying you are able to store an initial grid to an output file, but are just not able to let the grid be solved?

tall bay
#

here's a sodoku grid generator implementation if you're still stuck on the problem, i don't have the time to go through your code to spot potential issues with it (I agree that the issue you're having is also unclear).

the python script validates what it creates by solving each potential puzzle, then discards anything that can't be solved. i don't remember which algorithms/strategies I used for the creation of the grids and for solving them, but i remember implementing some simple optimizations for both creating the unsolved puzzle grids and how it solved them during the validatation step. it shouldn't be too hard to compare algorithms or just port it directly to C++. I licensed it as MIT so you can copy it, port it, etc.

https://github.com/vorlac/sudoku/blob/main/sudoku_generator.py

it'll just run until it creates 100 valid puzzles. it'll print the grid unsolved and solved to console and write them all to a file like below:

=================================
== Sudoku Grid [001] generated ==
=================================

[pre-solved grid]

        9 . . | 8 3 . | . . .
        6 . . | . . . | . . .
        . . . | . . 1 | . . .
        ----- + ----- + -----
        . . . | . . . | . 5 .
        . . . | . . . | . . .
        . . . | . . . | . . .
        ----- + ----- + -----
        . . . | . . . | 7 . .
        . . . | . . . | . . .
        2 . . | . . 4 | . . .

[solution]

        9 1 2 | 8 3 5 | 4 6 7
        6 3 4 | 2 7 9 | 1 8 5
        5 7 8 | 4 6 1 | 2 3 9
        ----- + ----- + -----
        1 2 3 | 6 4 7 | 9 5 8
        4 5 6 | 1 9 8 | 3 7 2
        7 8 9 | 3 5 2 | 6 1 4
        ----- + ----- + -----
        3 4 5 | 9 8 6 | 7 2 1
        8 9 1 | 7 2 3 | 5 4 6
        2 6 7 | 5 1 4 | 8 9 3
#

the only issue you might have with it is that it generates puzzles that are pretty difficult by default. you can probably make a few easy tweaks to sprinkle a few additional values into the pre-solved grid by copying them over from the solution grid