Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Mini Sudoku game 4x4 ( four 2x2 grids) program

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    10
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Mini Sudoku game 4x4 ( four 2x2 grids) program

    Hello guys and girls,
    I'm doing this assignment and it involves making a mini sudoku game 4x4 ( four 2x2 grids) and the user enters 5 numbers into the board and starts solving it. Also we are not allowed to use arrays for this task.

    I've been trying to write the code up and this is what I have come up with.



    import java.util.Scanner;
    public class Main
    {
        public static final Scanner keyboard = new Scanner(System.in);
        public static void main(String[] args)
        {        
            int n = 1;
     
            Sudoku board = new Sudoku();
            System.out.println("Configure puzzle");
            System.out.print("Region: "); 
            int region = keyboard.nextInt();
            System.out.print("Cell: "); 
            int cell = keyboard.nextInt();
            System.out.print("Number: "); 
            int number = keyboard.nextInt();
            board.enterNumber(region, cell, number, true);
            board.print();
     
            while (n < 5){
                System.out.print("Region: "); 
                region = keyboard.nextInt();
                System.out.print("Cell: "); 
                cell = keyboard.nextInt();
                System.out.print("Number: "); 
                number = keyboard.nextInt();
                board.enterNumber(region, cell, number,true);
                board.print();
                n += 1;
            }
     
            System.out.println("Puzzle entered. Time to solve!");         
     
            int p = 1;       
            while (p < 13){
                System.out.print("Region: "); 
                region = keyboard.nextInt();
                System.out.print("Cell: "); 
                cell = keyboard.nextInt();
                System.out.print("Number: "); 
                number = keyboard.nextInt();
                board.enterNumber(region, cell, number,false);
                board.print();
                p += 1;
     
            }
        }
    }


    and

    public class Sudoku
    {
        private int a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4,d1,d2,d3,d4;
     
        public Sudoku()
        {
     
        }
     
        public void enterNumber(int region, int cell, int number, boolean lock)
            {
                {
                switch (region){
                    case 1:
                        if (cell == 1){
                            a1 = number;
                        }else if (cell == 2){
                            a2 = number;
                        }else if (cell == 3){
                            a3 = number;
                        }else if (cell == 4){
                            a4 = number;
                        }
                        break;
     
                    case 2:
                        if (cell == 1){
                            b1 = number;
                        }else if (cell == 2){
                            b2 = number;
                        }else if (cell == 3){
                            b3 = number;
                        }else if (cell == 4){
                            b4 = number;
                        }
                        break;
     
                    case 3:
                        if (cell == 1){
                            c1 = number;
                        }else if (cell == 2){
                            c2 = number;
                        }else if (cell == 3){
                            c3 = number;
                        }else if (cell == 4){
                            c4 = number;
                        }
                        break;
     
                    case 4:
                        if (cell == 1){
                            d1 = number;
                        }else if (cell == 2){
                            d2 = number;
                        }else if (cell == 3){
                            d3 = number;
                        }else if (cell == 4){
                            d4 = number;
                        }
                        break;
     
            }
        }
    }
     
     
     
     
        public void print()
        {
            System.out.println();
            System.out.println(+ a1 + " " + a2 + " " + b1 + " " + b2);
            System.out.println(+ a3 + " " + a4 + " " + b3 + " " + b4);
            System.out.println(+ c1 + " " + c2 + " " + d1 + " " + d2);
            System.out.println(+ c3 + " " + c4 + " " + d3 + " " + d4);
            System.out.println();
        }
    }


    Basically it's working to an extent but I am unsure about a few things.

    1. The task requires me to print out blank spaces " " instead of 0's for spots that don't have a value yet. ( The teacher gave us a hint of making all of them -1 but I'm not sure how that helps me)

    2. I used boolean lock to try lock in the first 5 entries the user inputs when they configure the board. However, when I input a new number to one of the initial spots, the original entry gets overwritten.

    Sorry for the wall of text. Any help/advice would be appreicated.
    Thanks
    Last edited by Freaky Chris; September 16th, 2009 at 05:43 AM.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Homework Help - Mini Sudoku

    public class Sudoku
    {
        private int a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4,d1,d2,d3,d4;
     
        public Sudoku()
        {
     
        }
     
        public void enterNumber(int region, int cell, int number, boolean lock)
            {
                {
                switch (region){
                    case 1:
                        if (cell == 1){
                            a1 = number;
                        }else if (cell == 2){
                            a2 = number;
                        }else if (cell == 3){
                            a3 = number;
                        }else if (cell == 4){
                            a4 = number;
                        }
                        break;
     
                    case 2:
                        if (cell == 1){
                            b1 = number;
                        }else if (cell == 2){
                            b2 = number;
                        }else if (cell == 3){
                            b3 = number;
                        }else if (cell == 4){
                            b4 = number;
                        }
                        break;
     
                    case 3:
                        if (cell == 1){
                            c1 = number;
                        }else if (cell == 2){
                            c2 = number;
                        }else if (cell == 3){
                            c3 = number;
                        }else if (cell == 4){
                            c4 = number;
                        }
                        break;
     
                    case 4:
                        if (cell == 1){
                            d1 = number;
                        }else if (cell == 2){
                            d2 = number;
                        }else if (cell == 3){
                            d3 = number;
                        }else if (cell == 4){
                            d4 = number;
                        }
                        break;
     
            }
        }
    }
     
     
     
     
        public void print()
        {
            System.out.println();
            System.out.println(Space(a1) + " " + Space(a2) + " " + Space(b1) + " " + Space(b2));
            System.out.println(Space(a3) + " " + Space(a4) + " " + Space(b3) + " " + Space(b4));
            System.out.println(Space(c1) + " " + Space(c2) + " " + Space(d1) + " " + Space(d2));
            System.out.println(Space(c3) + " " + Space(c4) + " " + Space(d3) + " " + Space(d4));
            System.out.println();
        }
     
        public String Space(int n){
            return n == 0 ? " " : new Integer(n).toString();
        }
    }

    Something like that might do it

    Chris

  3. The Following User Says Thank You to Freaky Chris For This Useful Post:

    derky (September 16th, 2009)

  4. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    10
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Homework Help - Mini Sudoku

    Thanks for your quick help, Chris! (for cleaning up my post as well)


    Am I doing something wrong with the boolean lock in my enterNumber method?
    The first 5 entries is entered as true and the ones following that is false. I've never used it before so I'm kinda lost if thats the right logic/syntax.

  5. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Homework Help - Mini Sudoku

    Sorry, I'm a bit confused by your second problem. Do you think you could explain again, perhaps provide some examples

    Thanks,
    Chris

  6. #5
    Junior Member
    Join Date
    Sep 2009
    Posts
    10
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Homework Help - Mini Sudoku

    Oh sure. Basically my program prompts the user to input the region, then the cell number of that region and then the number you want to put into that cell. The program requires the user to put in 5 numbers in different cells to create a starting point for the game. From there, the user continues to input numbers to different cells to try and fill in the blanks. Once the board is solved, a message will pop up saying Solved! or something like that.

    For example:
    Region 1
    Cell 2
    Number 3

    Imagine there are 4 boxes and each of them is a 2x2 grid
    the cells go in the order of
    12
    34

    so this would put a number 3 in cell 2 of the first top left grid.

    Now the problem is, I cannot make the first 5 inputs stay.

    So a problem would be,

    I put in
    Region 1
    Cell 2
    Number 4

    This would replace the previous 3 with the 4. But I want the 3 to be unchangable since it was one of the first 5 inputs.

    I hope this is easier to understand :S

  7. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Homework Help - Mini Sudoku

    Ah yes that makes perfect sense now thank you.

    public class Sudoku
    {
        private int a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4,d1,d2,d3,d4;
        private String originalCell = "";
        private String originalRegion = "";
        public Sudoku()
        {
     
        }
     
        public void enterNumber(int region, int cell, int number, boolean lock)
            {
                if(lock){
                     originalCell += cell;
                     originalRegion += region;
                }
               if(originalCell.indexOf(new Integer(cell).toString()) == originalRegion.indexOf(new Integer(region).toString()) &&  originalCell.indexOf(new Integer(cell).toString()) != -1){
                switch (region){
                    case 1:
                        if (cell == 1){
                            a1 = number;
                        }else if (cell == 2){
                            a2 = number;
                        }else if (cell == 3){
                            a3 = number;
                        }else if (cell == 4){
                            a4 = number;
                        }
                        break;
     
                    case 2:
                        if (cell == 1){
                            b1 = number;
                        }else if (cell == 2){
                            b2 = number;
                        }else if (cell == 3){
                            b3 = number;
                        }else if (cell == 4){
                            b4 = number;
                        }
                        break;
     
                    case 3:
                        if (cell == 1){
                            c1 = number;
                        }else if (cell == 2){
                            c2 = number;
                        }else if (cell == 3){
                            c3 = number;
                        }else if (cell == 4){
                            c4 = number;
                        }
                        break;
     
                    case 4:
                        if (cell == 1){
                            d1 = number;
                        }else if (cell == 2){
                            d2 = number;
                        }else if (cell == 3){
                            d3 = number;
                        }else if (cell == 4){
                            d4 = number;
                        }
                        break;
     
            }
        }
    }
     
     
     
     
        public void print()
        {
            System.out.println();
            System.out.println(Space(a1) + " " + Space(a2) + " " + Space(b1) + " " + Space(b2));
            System.out.println(Space(a3) + " " + Space(a4) + " " + Space(b3) + " " + Space(b4));
            System.out.println(Space(c1) + " " + Space(c2) + " " + Space(d1) + " " + Space(d2));
            System.out.println(Space(c3) + " " + Space(c4) + " " + Space(d3) + " " + Space(d4));
            System.out.println();
        }
     
        public String Space(int n){
            return n == 0 ? " " : new Integer(n).toString();
        }
    }

    Something like that?

    Chris

  8. #7
    Junior Member
    Join Date
    Sep 2009
    Posts
    10
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Homework Help - Mini Sudoku

    I just tried running with what you had there but when I try to input a number to a region but different cell it does not show up on the board.

    For example:

    I'm going to use 0's here because spaces dont show up :S

    Region 1
    Cell 1
    Number 1

    Region 2
    Cell 2
    Number 2

    Region 3
    Cell 3
    Number 3

    Region 4
    Cell 4
    Number 4

    Region 1
    Cell 4
    Number 2

    would give me
    1002
    0000
    0000
    3004 for my board. (the 5th input doesnt show up)

    And then the "Puzzle entered. Time to solve!" comes up
    and anything I input does not show up. ><

Similar Threads

  1. [SOLVED] What is cast operator and how to use it?
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 27th, 2009, 06:11 AM
  2. [SOLVED] Java program to prompt and display the user input number as odd or even
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 22nd, 2009, 01:19 AM