DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3

Thread: Sudoku

  1. #1
    Join Date
    Dec 2005
    Posts
    4

    ...

    ...
    Last edited by Akole; 02-13-2006 at 06:38 PM.

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    If you want to return the answer of a specified square, then you're going to need to solve it first. I would recommend having a class Square, which would hold all the possible values of that square.
    The board would be a 9 by 9 array of these squares.
    Code:
    class Square {
        ArrayList<Integer> possibilities = new ArrayList<Integer>();
        
        public Square() {
            for (int i = 1; i <= 9; i++) {
                possibilities.add(i);
            }
        }
        
        /** this will be called only for the squares that are given to us */
        public Square(int num) {
            if (num => 1 && num <= 9) {
                possibilities.add(num);
            } 
        }
        
        public void removePossibility(int num) {
            possibilities.remove(num);
        }
    }
    etc.

    About the algorithm, you're going to have to come up with that yourself. For example, if you have a number 4 given to you at index (1, 0), then you'll have to do something like:
    Code:
    for (int i = 0; i < 9; i++) {
        if (i != 0) {
            square[1][i].remove(4);
        }
        if (i != 1) {
            square[i][0].remove(4);
        }
    }
    
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (i != 1 and j != 0) {
                square[i][j].remove(4);
            }
        }
    }
    Good luck!

  3. #3
    Join Date
    Mar 2005
    Location
    UK, London
    Posts
    150
    ArrayList<Integer> possibilities = new ArrayList<Integer>();
    Implemented as of java 1.5, from what i have read (I think)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links