Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Java - creating sudoku - tips?

Hello, I'm trying to program a sudoku game, just want to share my thoughts and ask for some tips. **I've seen other people's advice to download someone else's and go from there but I'd prefer to work from the thought process.

Right now I've created a grid using ━ ┃ ┏ ┓ ┗ ┛ ┣ ┫ ┳ ┻ ╋ and have filled it with random numbers. this is actually a school project so when we actually get the project we'll have set numbers to work with. I'm guessing creating a program that can create solvable sudoku games is way beyond our level. (as I heard the creation of such a program is what sparked its popularity)

Ok, so I have my grid, the functions I want it to be able to have are:

-undo (any number of times)

-reads input, checks if its a legal move. inserts if it is, else shows error message.

-game over when non-solvable

-victory cheer when solved

Some thoughts:

-create two methods called check row and check column, which then individually compares the input with previous inputs is the same row/column. ?

-checking within the 3x3 box seems rather complicated...my only solution would be two use a ton of if statements...

-Displaying a empty slot seems complicated, I'm guessing you create an array with string, and use the parsed-integer version for the calculations?

-for the victory thing, assuming all input is legal, I'm thinking I'll use a counter, and if it reaches 81, display victory message

-game over seems a bit harder...maybe...make a list of all possible inputs and if the input doesn't equal one of them, then game over? seems a bit iffy...

-so I guess the process is, input box asking for input, test if legal (row, column, box), if legal then place it in. Then check win condition, then check game over condition.

-undo is just ... confusing. I think it uses something we haven't learned yet ...

Update:

oh, we haven't really gotten to gui's yet, I'm using a coordinate method.

5 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Ideally, your game board should be represented by a 9x9 two dimensional array, probably of integers. A second grid should contain the initial game state so the user doesn't overwrite squares that they shouldn't. You can then use zero to represent blank squares.

    This should simplify things by allowing you to loop when checking for win rows/columns.

    The 3x3 squares can still be handled by nested loops.

    For example:

    int checkBox(int [][] grid, int bX, int bY) {

      int ret = 0; // Return 0 for a correctly filled box. 1 for a blank square. 2 for two or more squares containing the same number, i.e. illegal input. 3 for both an empty square and illegal input.

      int bits = 0; // Use bits 1 - 9 to represent each number used

      for (int x = 3 * bX; x < 3 * (bX + 1); ++x) {

        for (int y = 3 * bY; y < 3 * (bY + 1); ++y) {

          int val = grid[y][x]; // Assuming it uses a grid[row][column] setup

          if (val == 0)

            ret |= 1; // There's a blank square

          else if (bits & (1 << val))

            ret |= 2; // The user used the same number twice

          else

            bits |= 1 << val;

        }

      }

      return ret;

    }

    Undo/Redo can be handled using a stack of small objects representing the users actions. Each time the player makes a move, push the move onto the undo stack. Clear the redo stack if not empty. On undo, pop the undo stack, use the information in the object to undo the last move, and push it onto the redo stack. On redo, pop the redo stack, use the information in the object to redo the undone move and push it onto the undo stack.

    If you haven't encountered stacks yet, think of a stack of plates. You can only add/remove things from the top. That's basically how stack objects work. Traditionally, removing an item is called popping the stack and adding an item is pushing the item onto the stack.

    In Sudoku, there isn't really a game lost state - the closest you get is the user filling the table with incorrect numbers, which can be cleared/undone. If you use similar checkRow() and checkColumn() methods to my checkBox() method above, loop through every row, column and box, OR all the results together and if you get an answer of 2, it meant that game has no empty squares and there are rows/columns/boxes which have the same digit used twice. If it's 1 or 3, there are empty squares left. If it's 0, the input is good and the user won.

    The only time an unwinnable state can occur is if it's unwinnable from the very beginning, i.e. the starting data is bad.

  • ?
    Lv 7
    1 decade ago

    How do you want to find out that somebody wants to write to a certain cell? (In forms you could place text controls)

    In a console application you need to work with querying the cursor.

    Checking: you need to perform three checks:

    1. check whether there are unique numbers in the horizontal row

    2. as 1. for the vertical column

    3. nine square box.

    The check is always the same: if there is a repetition of numbers (i.e. 2 times 3) then you have an error and have to delete/mark the last entry.

  • 5 years ago

    Tips... okay 1) CALM DOWN. 2) If you cant solve it, throw it away. Try again later. 3) When you get back, continue reading the tips. 4) Look for numbers in a horizontal and vertical ways. 5) Look for numbers in 9-box boxes. 6) Take pictures and post it up so someone can solve them for you. 7) Don't give up. 8) Never ever give up. 9) Don't listen to me. 10) Disregard rule 9.

  • Anonymous
    5 years ago

    Need more info to answer

  • How do you think about the answers? You can sign in to vote the answer.
  • marian
    Lv 4
    5 years ago

    I was wondering the same thing too today

Still have questions? Get your answers by asking now.