Help with Arrays and Random

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sheza7
    New Member
    • May 2007
    • 6

    Help with Arrays and Random

    Hi, I am a beginner in JAVA language...
    I am making a simple sudoku game (in notepad -I have to use only notepad).
    I have my sudoku tables stored in 2d arrays e.g sudoku1[9][9].
    Lets say I have 5 2d-arrays, sudoku1....sudo ku5, and I want a random one to be chosen... how do I do that.
    I know I have to use class Random, but how do I make it in this case (i.e 2d arrays)??

    also
    this is how u put values in a 2d array...
    int [][] example2d=
    {
    {0, 3, 2},
    {0, 5, 8},
    };
    how can u put values in a 3d array??
    int [][][] example3d=
    ???????
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Are the different sudoku values in several different files, or all in the same file?

    To input into a 3d array...
    Code:
    int my3DArray = { { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} },
                      { {10, 11, 12}, {13, 14, 15}, {16, 17, 18} },
                      { {19, 20, 21}, {22, 23, 24}, {25, 26, 27} } }

    Comment

    • Sheza7
      New Member
      • May 2007
      • 6

      #3
      I am a beginner, I am not sure about what you mean by filing them (I mean as a java programmer)... I have them stored like this...
      Code:
        int [][] sudok1= 
        {
         {3, 5, 9, 8, 7, 1, 6, 4, 2},
         {1, 2, 4, 3, 6, 9, 7, 5, 8},
         {8, 6, 7, 2, 5, 4, 9, 1, 3},
         {5, 8, 3, 1, 4, 7, 2, 9, 6},
         {2, 9, 1, 6, 3, 8, 5, 7, 4},
         {7, 4, 6, 5, 9, 2, 8, 3, 1},
         {6, 7, 5, 4, 8, 3, 1, 2, 9},
         {4, 1, 8, 9, 2, 5, 3, 6, 7},
         {9, 3, 2, 7, 1, 6, 4, 8, 5},
        };
        int [][] sudok2= 
        {
         {3, 5, 9, 8, 7, 1, 6, 4, 2},
         {1, 2, 4, 3, 6, 9, 7, 5, 8},
         {8, 6, 7, 2, 5, 4, 9, 1, 3},
         {5, 8, 3, 1, 4, 7, 2, 9, 6},
         {2, 9, 1, 6, 3, 8, 5, 7, 4},
         {7, 4, 6, 5, 9, 2, 8, 3, 1},
         {6, 7, 5, 4, 8, 3, 1, 2, 9},
         {4, 1, 8, 9, 2, 5, 3, 6, 7},
         {9, 3, 2, 7, 1, 6, 4, 8, 5},
        };
      Is there a way of putting them into a file and extracting them?

      I get this error for the 3d one you posted...
      Code:
      P1.java:17: illegal initializer for int
       int my3DArray = { { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} },
      For the randoming I came with the idea of putting all 2d arrays (sudoku1 & sudoku2 in this case) in a 3d one. Thus using
      Code:
        Random ran = new Random();
        sudok[ran.nextInt(2)];
      ...I will be able to extract a random 2d array.
      Is that good, or is there a better way to do it?

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Ahh...sorry, I had read your question wrong and thought you were storing the sudoku puzzles in a text file. Go ahead and ignore that first line :)

        Yes, creating a 3d array like that should work...I wonder if you can initialize it like this:

        Code:
        int[][] sudoku1 = { /* blah blah blah here... */ };
        int[][] sudoku2 = { /* blah blah blah here too... */ };
        // ...and so on...
        int[][][] sudokus = {sudoku1, sudoku2, /* blah blah blah... */ };

        Comment

        • Sheza7
          New Member
          • May 2007
          • 6

          #5
          P.S.:
          I got the 3d array working (...forgot about [][][])

          Comment

          • Sheza7
            New Member
            • May 2007
            • 6

            #6
            Code:
            int[][] sudoku1 = { /* blah blah blah here... */ };
            int[][] sudoku2 = { /* blah blah blah here too... */ };
            // ...and so on...
            int[][][] sudokus = {sudoku1, sudoku2, /* blah blah blah... */ };
            Can I do something like this...
            Code:
            int [][][] sudokus = { { {sudok1} },
                                          { {sudok2} } };
            ... I mean do I have to retype the whole 2d arrays again, isn't there a way to reference to them.

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              Well, if sudok1 and sudok2 are already 2d arrays, then you can't set them to the third 'array' of sudokus. You wouldn't have to retype the 2d arrays - you said you already had them declared in your program, yes? Then just initialize sudokus using those variables.

              Comment

              • Sheza7
                New Member
                • May 2007
                • 6

                #8
                ok I guess I can do that...
                Would it be possible (and also more efficient) to store&retrieve sudokus in a text file??

                P.S: THANKS for all the help!

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #9
                  It definitely would be possible to store the sudokus in a text file. It would also allow you to (relatively) easily add more puzzles to be used in your program. Whether it's more efficient, though, is something I'm not sure of. I know it would take longer to open the file and read the numbers into your arrays. Whether that extra flexibility in adding puzzles is worth the added loading time is up to you.

                  Comment

                  • Sheza7
                    New Member
                    • May 2007
                    • 6

                    #10
                    But if I put them in a text file will I be able to retrieve a random table (pull randomly a whole 2d array).
                    I mean now that I have put them in a 3d one I can choose a random one.
                    Code:
                    my3DArray[ran.nextInt(3)][][]
                    I guess I have to first learn how the text file retrieving works.

                    also
                    how can the "user" store (meaning to store while the program is runned)?
                    Code:
                     n=sc.nextInt()
                    How can that value stay permanently?

                    Comment

                    • Ganon11
                      Recognized Expert Specialist
                      • Oct 2006
                      • 3651

                      #11
                      Originally posted by Sheza7
                      But if I put them in a text file will I be able to retrieve a random table (pull randomly a whole 2d array).
                      I mean now that I have put them in a 3d one I can choose a random one.
                      Code:
                      my3DArray[ran.nextInt(3)][][]
                      Yes. At the start of your program, you can read in the sudoku puzzles into the 3d array, and can still use the above code to randomly select a puzzle.

                      Originally posted by Sheza7
                      how can the "user" store (meaning to store while the program is runned)?
                      Code:
                       n=sc.nextInt()
                      How can that value stay permanently?
                      I'm not exactly sure what you mean by this...could you elaborate further as to what you're trying to accomplish?

                      Comment

                      Working...