New Posts  All Forums:Forum Nav:

Sudoku in Java?

post #1 of 5
Thread Starter 
I have a 2d array, I'm getting user input, now I'm to the point of checking if their input is right.

If you know how Sudoku works, then maybe you can help. I have to be able to know when the user's guess is incorrect (already that number in the same row, column or 3x3 square) and I have to know when the board is complete.

I could probably do it with a bunch of if else statements, but that sounds messy.
    
CPUMotherboardGraphicsRAM
AMD Phenom II X6 1090T Black Edition ASUS M4A89GTD PRO/USB3 XFX HD-577A-ZNFC Radeon HD 5770 G.SKILL 8gb (2x4gb) 1333mhz 
Hard DriveOSMonitorKeyboard
WD Caviar Black WD1002FAEX 1TB 7200 R Windows 7 Home Premium (none yet) Nothing special 
PowerCaseMouse
CORSAIR CMPSU-850TX 850W COOLER MASTER HAF 922 Logitech G700 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
AMD Phenom II X6 1090T Black Edition ASUS M4A89GTD PRO/USB3 XFX HD-577A-ZNFC Radeon HD 5770 G.SKILL 8gb (2x4gb) 1333mhz 
Hard DriveOSMonitorKeyboard
WD Caviar Black WD1002FAEX 1TB 7200 R Windows 7 Home Premium (none yet) Nothing special 
PowerCaseMouse
CORSAIR CMPSU-850TX 850W COOLER MASTER HAF 922 Logitech G700 
  hide details  
Reply
post #2 of 5
How is the sudoku being generated? If it was me I would create 2 arrays, one for user input and one with the solution already generated, and then check if the number in both positions are the same.

Still, if would help if you could explain how your program works, or upload the code so we can see.
First Build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k @4.5Ghz Gigabyte Z68X-UD7 B3 MSI 6950 G.Skill Sniper 2x4GB 
Hard DriveHard DriveHard DriveCooling
Crucial M4 64GB Samsung SpinPoint 250GB Hitachi 1TB Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows Server 2012 Samsung S22B300 22' ViewSonic VA703B 17" CM Quickfire TK 
PowerCaseMouse
Corsair HX850 NZXT Phantom Mionix Naos 3200 
  hide details  
Reply
First Build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k @4.5Ghz Gigabyte Z68X-UD7 B3 MSI 6950 G.Skill Sniper 2x4GB 
Hard DriveHard DriveHard DriveCooling
Crucial M4 64GB Samsung SpinPoint 250GB Hitachi 1TB Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows Server 2012 Samsung S22B300 22' ViewSonic VA703B 17" CM Quickfire TK 
PowerCaseMouse
Corsair HX850 NZXT Phantom Mionix Naos 3200 
  hide details  
Reply
post #3 of 5
Thread Starter 
Quote:
Originally Posted by Barbaroti View Post

How is the sudoku being generated? If it was me I would create 2 arrays, one for user input and one with the solution already generated, and then check if the number in both positions are the same.
Still, if would help if you could explain how your program works, or upload the code so we can see.

I have a 2D array of numers (9x9) representing the Sudoku board. I have user input of (row)(col)(number) I have it check to see if that square is empty, or was empty when the game started, then put that as the value. I have no way of checking if it's correct though.
    
CPUMotherboardGraphicsRAM
AMD Phenom II X6 1090T Black Edition ASUS M4A89GTD PRO/USB3 XFX HD-577A-ZNFC Radeon HD 5770 G.SKILL 8gb (2x4gb) 1333mhz 
Hard DriveOSMonitorKeyboard
WD Caviar Black WD1002FAEX 1TB 7200 R Windows 7 Home Premium (none yet) Nothing special 
PowerCaseMouse
CORSAIR CMPSU-850TX 850W COOLER MASTER HAF 922 Logitech G700 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
AMD Phenom II X6 1090T Black Edition ASUS M4A89GTD PRO/USB3 XFX HD-577A-ZNFC Radeon HD 5770 G.SKILL 8gb (2x4gb) 1333mhz 
Hard DriveOSMonitorKeyboard
WD Caviar Black WD1002FAEX 1TB 7200 R Windows 7 Home Premium (none yet) Nothing special 
PowerCaseMouse
CORSAIR CMPSU-850TX 850W COOLER MASTER HAF 922 Logitech G700 
  hide details  
Reply
post #4 of 5
i would try using 2D arrays something like this from my studies in java from a year ago..
Code:
import java.util.Scanner;

public class Sudoku { 
  public static void main(String[] args) {
    // Read a Sudoku puzzle
    int[][] grid = readAPuzzle();

    if (!isValid(grid))
      System.out.println("Invalid input");
    else if (search(grid)) {
      System.out.println("The solution is found:");
      printGrid(grid);
    }   
    else
      System.out.println("No solution");
  }
  
  /** Read a Sudoku puzzle from the keyboard */
  public static int[][] readAPuzzle() {
    // Create a Scanner
    Scanner input = new Scanner(System.in);

    System.out.println("Enter a Sudoku puzzle:");
    int[][] grid = new int[9][9];
    for (int i = 0; i < 9; i++) 
      for (int j = 0; j < 9; j++)
        grid[i][j] = input.nextInt();
    
    return grid;
  }

  /** Obtain a list of free cells from the puzzle */
  public static int[][] getFreeCellList(int[][] grid) {
    // Determine the number of free cells 
    int numberOfFreeCells = 0;   
    for (int i = 0; i < 9; i++)
      for (int j = 0; j < 9; j++) 
        if (grid[i][j] == 0) 
          numberOfFreeCells++;
    
    // Store free cell positions into freeCellList 
    int[][] freeCellList = new int[numberOfFreeCells][2];
    int count = 0;
    for (int i = 0; i < 9; i++)
      for (int j = 0; j < 9; j++) 
        if (grid[i][j] == 0) {
          freeCellList[count][0] = i;
          freeCellList[count++][1] = j;
        }
    
    return freeCellList;
  }

  /** Print the values in the grid */
  public static void printGrid(int[][] grid) {
    for (int i = 0; i < 9; i++) {
      for (int j = 0; j < 9; j++)
        System.out.print(grid[i][j] + " ");
      System.out.println();
    }
  }

  /** Search for a solution */
  public static boolean search(int[][] grid) {
    int[][] freeCellList = getFreeCellList(grid); // Free cells
    int k = 0; // Start from the first free cell    
    boolean found = false; // Solution found?
    
    while (!found) {
      int i = freeCellList[k][0];
      int j = freeCellList[k][1];
      if (grid[i][j] == 0) 
        grid[i][j] = 1; // Start with 1

      if (isValid(i, j, grid)) {
        if (k + 1 == freeCellList.length) { // No more free cells 
          found = true; // A solution is found
        }
        else { // Move to the next free cell
          k++;
        }
      }
      else if (grid[i][j] < 9) {
        grid[i][j] = grid[i][j] + 1; // Check the next possible value
      }
      else { // grid[i][j] is 9, backtrack
        while (grid[i][j] == 9) {
          grid[i][j] = 0; // Reset to free cell
          if (k == 0) {
            return false; // No possible value
          }
          k--; // Backtrack
          i = freeCellList[k][0];
          j = freeCellList[k][1];
        }

        grid[i][j] = grid[i][j] + 1; // Check the next possible value
      }
    }

    return true; // A solution is found
  }

  /** Check whether grid[i][j] is valid in the grid */
  public static boolean isValid(int i, int j, int[][] grid) {
    // Check whether grid[i][j] is valid at the i's row
    for (int column = 0; column < 9; column++)
      if (column != j && grid[i][column] == grid[i][j])
        return false;

    // Check whether grid[i][j] is valid at the j's column
    for (int row = 0; row < 9; row++)
      if (row != i && grid[row][j] == grid[i][j])
        return false;

    // Check whether grid[i][j] is valid in the 3 by 3 box
    for (int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++)
      for (int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++)
        if (row != i && col != j && grid[row][col] == grid[i][j])
          return false;

    return true; // The current value at grid[i][j] is valid
  }

  /** Check whether the fixed cells are valid in the grid */
  public static boolean isValid(int[][] grid) {
    for (int i = 0; i < 9; i++)
      for (int j = 0; j < 9; j++)
        if (grid[i][j] != 0 && !isValid(i, j, grid)) return false;

    return true; // The fixed cells are valid
  }
}

this is just sample code, try it and see how it works and how you could code your own and improve on it smile.gif

also a sample puzzle to try
enter these values
Code:
0 6 0 1 0 4 0 5 0  
0 0 8 3 0 5 6 0 0
2 0 0 0 0 0 0 0 1
8 0 0 4 0 7 0 0 6
0 0 6 0 0 0 3 0 0
7 0 0 9 0 1 0 0 4
5 0 0 0 0 0 0 0 2
0 0 7 2 0 6 9 0 0
0 4 0 5 0 8 0 7 0

Edited by stubass - 10/14/12 at 3:52pm
 
Wife's Rig
(12 items)
 
 
CPUMotherboardGraphicsRAM
i7-3770k Gigabyte GA-Z77X-UP5 TH Asus HD7950 DCII Summer has killed my OC :( G.Skill TridentX 2400 2 x 4GB 
Hard DriveHard DriveHard DriveOptical Drive
Samsung 830 128GB Plextor M5p 128GB 2 x WD 500GB Blues LG 
CoolingCoolingOSMonitor
Prolimatech Megahalems Rev C Arctic F12 CO PWM x 2 Push/Pull Windows 7 64 bit Ultimate LG IPS235V 
PowerCase
Corsair AX850 Coolermaster HAF 912 advanced (Asia version) 
CPUMotherboardGraphicsRAM
I5-3570K Gigabyte Z68X-UD3H-B3 MSI GTX460 Cyclone 1GD5 8GB G.Skill RipjawsX 1600 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 64GB WD black 500GB Liteon Coolermaster Hyper 212+ EVO 
OSMonitorPowerCase
Win 7 ultimate Samsung Syncmaster 932gwe+ OCZ ZT series 550W Coolermaster 410 
CPUMotherboardRAMHard Drive
1.67Ghz Atom stock 1GB DDR2 2 x 500GB Western Digital Cavier Blacks [RAID 1] 
CoolingOSPowerCase
stock DSM3.2 [built on linux] stock stock 
OtherOther
Synology DX510 Expansion unit 5 x 1TB WD Cavier Blacks in a RAID 6 
  hide details  
Reply
 
Wife's Rig
(12 items)
 
 
CPUMotherboardGraphicsRAM
i7-3770k Gigabyte GA-Z77X-UP5 TH Asus HD7950 DCII Summer has killed my OC :( G.Skill TridentX 2400 2 x 4GB 
Hard DriveHard DriveHard DriveOptical Drive
Samsung 830 128GB Plextor M5p 128GB 2 x WD 500GB Blues LG 
CoolingCoolingOSMonitor
Prolimatech Megahalems Rev C Arctic F12 CO PWM x 2 Push/Pull Windows 7 64 bit Ultimate LG IPS235V 
PowerCase
Corsair AX850 Coolermaster HAF 912 advanced (Asia version) 
CPUMotherboardGraphicsRAM
I5-3570K Gigabyte Z68X-UD3H-B3 MSI GTX460 Cyclone 1GD5 8GB G.Skill RipjawsX 1600 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 64GB WD black 500GB Liteon Coolermaster Hyper 212+ EVO 
OSMonitorPowerCase
Win 7 ultimate Samsung Syncmaster 932gwe+ OCZ ZT series 550W Coolermaster 410 
CPUMotherboardRAMHard Drive
1.67Ghz Atom stock 1GB DDR2 2 x 500GB Western Digital Cavier Blacks [RAID 1] 
CoolingOSPowerCase
stock DSM3.2 [built on linux] stock stock 
OtherOther
Synology DX510 Expansion unit 5 x 1TB WD Cavier Blacks in a RAID 6 
  hide details  
Reply
post #5 of 5
Here is a visual version of a Sudoku solver in Java written by my professor some time ago. You can try to read the code and understand how it works.
sudoku.zip 8k .zip file
Hackintosh
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770K MSI Z77 MPower Asus GTX 670 DCII G.SKILL Ares 2x4GB 
Hard DriveCoolingOSOS
Samsung 830 SSD NZXT Havik 140 Mac OSX 10.8 Windows 8 Professional 
MonitorKeyboardPowerCase
Yamakasi Catleap Q270 Logitech G510 Seasonic X750 Corsair 500R 
MouseAudio
Logitech G9x Logitech Z623 
  hide details  
Reply
Hackintosh
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770K MSI Z77 MPower Asus GTX 670 DCII G.SKILL Ares 2x4GB 
Hard DriveCoolingOSOS
Samsung 830 SSD NZXT Havik 140 Mac OSX 10.8 Windows 8 Professional 
MonitorKeyboardPowerCase
Yamakasi Catleap Q270 Logitech G510 Seasonic X750 Corsair 500R 
MouseAudio
Logitech G9x Logitech Z623 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming