简体   繁体   中英

Help With Java Sudoku Permuter Program using Two Dimensional Array?

I have to create a program that displays the 9 rows of a sudoku as 9 9-digit numbers and then prompt the user to do one of 6 operations on the sudoku. Then we have to output the sudoku each time the user performs an operation. This is sort of a sample run of how it should go:

Welcome to Sudoku Permuter.

   C C C C C C C C C
   1 2 3 4 5 6 7 8 9
R1 0 8 0 4 0 2 0 6 0
R2 0 3 4 0 0 0 9 1 0
R3 9 6 0 0 0 0 0 8 4
R4 0 0 0 2 1 6 0 0 0
R5 2 0 0 0 0 9 6 0 0
R6 0 1 0 3 5 7 0 0 8
R7 8 4 0 0 0 0 0 7 5
R8 0 2 6 0 0 0 1 3 0
R9 0 9 0 7 0 1 0 4 0

(0 denotes a blank)

Enter 1 to swap two rows in a panel
Enter 2 to swap two columns in a panel
Enter 3 to swap two row panels
Enter 4 to swap two column panels
Enter 5 to swap two numbers
Enter 0 to end:

Let's say the user enters 3 (to swap two row panels). This would come up:

Enter row panels (1-3) to swap: 3 1

It would swap row panels 1 and 3, and this would be the output:

   C C C C C C C C C
   1 2 3 4 5 6 7 8 9
R1 8 4 0 0 0 0 0 7 5
R2 0 2 6 0 0 0 1 3 0
R3 0 9 0 7 0 1 0 4 0
R4 0 0 0 2 1 6 0 0 0
R5 2 0 0 0 0 9 6 0 0
R6 0 1 0 3 5 7 0 0 8
R7 0 8 0 4 0 2 0 6 0
R8 0 3 4 0 0 0 9 1 0
R9 9 6 0 0 0 0 0 8 4

Rows 1-3 have been switched with rows 7-9.

Let's say the user inputs 5. This comes up:

Enter two numbers: 2 8

The original sudoku is outputted again, except 2's and 8's are switched throughout.

   C C C C C C C C C
   1 2 3 4 5 6 7 8 9
R1 0 2 0 4 0 8 0 6 0
R2 0 3 4 0 0 0 9 1 0
R3 9 6 0 0 0 0 0 2 4
R4 0 0 0 8 1 6 0 0 0
R5 8 0 0 0 0 9 6 0 0
R6 0 1 0 3 5 7 0 0 2
R7 2 4 0 0 0 0 0 7 5
R8 0 8 6 0 0 0 1 3 0
R9 0 9 0 7 0 1 0 4 0

If the user entered 1, something would come up saying

Enter two rows (1-9) to switch:

And whichever rows the user enters, those two individual rows would be swapped and the sudoku would once again be outputted. It'd be similar if the user entered 2, except 2 columns would be switched. Similarly, if the user entered 4, two column panels would be switched.

We're supposed to use a two dimensional array like this:

int [] [] sudoku = new int[10] [10]

I have no idea how to do this. I've been struggling all semester, this is my first programming class. I just don't understand arrays at all, and I don't understand how we're even supposed to display the sudoku in the first place. This problem isn't in our book, so I have nothing to look back on, either. I really need to pass this class. If anyone could help me, I really appreciate it. Try to make it easy to understand, there's a lot of stuff I haven't learned how to do yet (ex: for the record, idk what parseInt is). I've tried reading the book (several times). It helps some, but this program is going to be impossible. Thank you SO much for the help.

  1. Do you know how to read input?
  2. Do you know how to work with 1-dimensional arrays (lists), like {1, 2, 3, 4, 5}?
  3. Do you understand for loops?
  4. What part of the concept of arrays seems difficult to you?

For instance, here's a block of code that just prints out the raw contents of the array. Does this code make sense?

int [] [] sudoku = new int[10] [10];
// loop through all of the rows
for (int row = 0; row < 10; row++) {
    // loop through all columns for each row
    for (int column = 0; column < 10; column++) {
        // print out the sudoku value at that row and column
        System.out.print(sudoku[row] [column] + " ");
    }
    // at the end of the row, print a blank line to start the next row
    System.out.println();
}

Here's how you can hard-code your sample board to work with it:

int [] [] sudoku = new int [] [] {
    { 0, 8, 0, 4, 0, 2, 0, 6, 0 },
    { 0, 3, 4, 0, 0, 0, 9, 1, 0 },
    etc.
}

Here's some pseudo-code for swapping two rows.

Get first row # from user
Get second row # from user
Loop through each column in the board
    Swap(cell at first row #, current column, cell at second row #, current column)
End Loop

Swapping basically requires a temporary variable to hold one of the values while swapping:

Swap(a, b)
    Store a into Temp
    Store b into a
    Store Temp into b

As mellamokb's comment says, breaking things down into parts is the trick. At first glance I would probably do something like this:

  1. Hardcode in a sudoku board, and make a routine to print the board out
  2. Once that works right, then you'd need a menu. So extend your program to make a little menu that prints back the user's selection, and does nothing more
  3. Once that's going, you can start filling in the menu choices. So when the user selects option one, make the routine for swapping two rows. The other menu items can still just print out their number.

Once you've got that working, you're actually nearly done. You can take your routine from #3 and make copies of it with small changes to make other 4 modifications possible.

You don't mention where the sudoku board comes from. If it's hardcoded, you're done. If not, then all you have to is make that method. At this point, you already know that you can print the board right, show the menu, and change the board.

You mention problems with arrays, and they can be a bit of a leap. Is there a specific question you have about arrays that we might be able to help with? They are like anything else in programming. You start not knowing a ton about them, and just sort of following what guidance and code you find elsewhere. As you gain more experience (as you will on this project and future projects), they'll be less mysterious and make more sense until one day they're as easy as 3 + 7.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM