Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 sudoku puzzle generator logical error
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
xtreemboarder




PostPosted: Fri Apr 21, 2006 11:07 am   Post subject: sudoku puzzle generator logical error

I decided to try and create a SuDoKu Puzzle generator... thought it might be a challenge. Basically theres a 2D int array that holds all the numbers (obviously)... theres also a method which creates the random number and it also calls another method to check if its valid... the second method checks if theres that number in the row or column... and if there is it returns -1 and makes method 1 generate another random number (until it can be valid)... both checks work but for some reason the program ends before finishing generating all the numbers...

i know i dont have the 3 by 3 square check... its ok just ignore that ill add it in later... when this starts to work.

code:

// The "Sudoku_Generator" class.
import java.awt.*;
import hsa.*;

public class Sudoku_Generator
{
    static Console c;
    private static int createNumber (int[] [] board, int i, int g)
    {
        int randNum = -1;
        while (checkNumber (randNum, board, i, g) == -1)
        {
            randNum = ((int) (Math.random () * 9) + 1);
        }
        return randNum;
    }


    private static int checkNumber (int randNum, int[] [] board, int i, int g)
    {
        for (int k = 0 ; k < board.length ; k++) //check vertical
        {
            if (board [i] [k] == randNum)
                return -1;
        }
        for (int m = 0 ; m < board [i].length ; m++) // check horizontal
        {
            if (board [m] [g] == randNum)
                return -1;
        }
        return randNum;
    }


    public static void main (String[] args)
    {
        c = new Console ();

        int[] [] board = new int [9] [9];
        for (int i = 0 ; i < board.length ; i++)
        {
            for (int g = 0 ; g < board [i].length ; g++)
            {
                board [i] [g] = createNumber (board, i, g);
                c.print (board [i] [g]);
            }
            c.println ("");
        }

    } // main method
} // Sudoku_Generator class
Sponsor
Sponsor
Sponsor
sponsor
xtreemboarder




PostPosted: Mon Apr 24, 2006 10:49 am   Post subject: (No subject)

FOLLOW UP: how bout some help... i still dont understand why it doesnt work.
Ultrahex




PostPosted: Sun May 14, 2006 5:52 pm   Post subject: (No subject)

Ok, sure you can do it the way you are...

but why not make it make the full finished one then remove the numbers that you are suppose to guess... wouldn't that make it easier to program... just a suggestion...

ill get back to you on it in a bit maybe.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: