samedi 26 mai 2018

Java Mini Sudoku without backtracking

I've been working on a project on making a mini sudoku (6x6) solver without using backtracking. For this, I've made it so that the solver receives the clues from the answer sheet, make random numbers for all empty spaces, then check the conditions of the sudoku: repeat making numbers if any of them fails. Terribly inefficient, I know, but I wanted to do it this way.

Currently my codes are as follows:

public class Solver
{
    private int[][] sudokuA = new int[6][6];
    private int[][] sudokuR = new int[6][6];
    private int random;
    private int random2;

    public Solver(int[][] sudokuQ)
    {
        for (int i = 0; i<6; i++)
        {
            for (int j = 0; j<6; j++)
            {
                sudokuA[i][j] = sudokuQ[i][j];
                sudokuR[i][j] = sudokuQ[i][j];
            }
        }   
    }

    public boolean condition1(int[] array, int number)
    {
    for (int e = 0; e<6; e++)
    {
        if (array[e] == number)
        {
            return true;
        } 
    }
    return false;

    public void Loop1()
    {
        int refresh[] = new int[6];
        for (int i = 0; i<6; i++)
        {
            for (int r = 0; r<6; r++)
            {
                int input = sudokuR[i][r];
                refresh[r] = input;
            }
            for (int j = 0; j<6; j++)
            {
                if (refresh[j] == 0)
                {
                    boolean match = true;
                    while(match)
                    {
                        random = (int)(Math.random()*6+1);
                        match = condition1(refresh, random);
                    }
                    refresh[j] = random;
                    sudokuR[i][j] = random;
                }   
            }
        }
    }
}

And this code works to meet condition 1 (i.e. horizontal rows are 1 to 6) of the "guess grid". But then comes condition 2, which is to make vertical rows 1 to 6, which I can't seem to get it working for some reason....here is the code:

    public boolean condition2(int[] array, int number)
{
    int count = 0;
    for (int o = 0; o<6; o++)
    {
        if(array[o] == number)
        {
            count++;
        }
    }
    if (count > 1)
    {
        return false;
    }
    return true;
}

public void Loop2()
{
    for (int i = 0; i<6; i++)
    {
        int refresh2[] = new int[6];
        for (int r = 0; r<6; r++)
        {
            int input = sudokuR[r][i];
            refresh2[r] = input;
        }
        for (int j = 0; j<6; j++)
        {
            random2 = refresh2[j];
            if (condition2(refresh2, random2))
            {
                this.Loop1();
                i = -1;
                break;
            }
        }
    }
}

What I wanted to do was that the program checks for condition 2, and keep on making the grid that satisfies condition 1 (with Loop1) until condition 2 is also satisfied as well. However, the program just stops working as soon as I run it, and I don't know what to do;;;




Aucun commentaire:

Enregistrer un commentaire