Guest User

Sudoku

a guest
Dec 3rd, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.38 KB | None | 0 0
  1. /**
  2.  * A program to solve "easy" Sudoku puzzles.
  3.  *
  4.  * @author Lykos
  5.  * @version 1.0
  6.  */
  7. public class Sudoku
  8. {
  9.     private int[][] problem;
  10.    
  11.     /**
  12.      * Creates a 9x9 array of numbers and sets it up as a
  13.      * Sudoku problem. Zeros in the array indicate places to be filled in.
  14.      * This method is complete. Do not change this method.
  15.      */
  16.     public Sudoku()
  17.     {
  18.         problem = new int[][] { { 0, 0, 4,   0, 0, 0,   0, 6, 7 },
  19.                   { 3, 0, 0,   4, 7, 0,   0, 0, 5 },
  20.                   { 1, 5, 0,   8, 2, 0,   0, 0, 3 },
  21.            
  22.                   { 0, 0, 6,   0, 0, 0,   0, 3, 1 },
  23.                   { 8, 0, 2,   1, 0, 5,   6, 0, 4 },
  24.                   { 4, 1, 0,   0, 0, 0,   9, 0, 0 },
  25.            
  26.                   { 7, 0, 0,   0, 8, 0,   0, 4, 6 },
  27.                   { 6, 0, 0,   0, 1, 2,   0, 0, 0 },
  28.                   { 9, 3, 0,   0, 0, 0,   7, 1, 0 } };
  29.     }
  30.  
  31.     /**
  32.      * Takes a 9x9 array of numbers and sets it up as a Sudoku problem.
  33.      *
  34.      * @param problem The array representing the Sudoku problem.
  35.      * This method is complete. Do not change this method.
  36.      */
  37.     public Sudoku(int[][] problem)
  38.     {
  39.         this.problem = problem;
  40.     }
  41.    
  42.      /**
  43.      * Solves this Sudoku problem.
  44.      * This method is complete. Do not change this method.
  45.      */
  46.     public void solve()
  47.     {
  48.         if (!solve(0, 0))
  49.         {
  50.             System.out.println("No solution possible!");
  51.         }
  52.     }
  53.      /**
  54.      * Recursively solves this Sudoku problem.
  55.      * This method is complete. Do not change this method.
  56.      */
  57.     private boolean solve(int row, int col)
  58.     {
  59.         // Go to next row if at the end
  60.         if (col >= 9)
  61.         {
  62.             ++row;
  63.             col = 0;
  64.         }
  65.        
  66.         // If all spaces are filled, then the puzzle is solved
  67.         if (row >= 9)
  68.         {
  69.             print();
  70.             return true;
  71.         }
  72.        
  73.         // If the space is marked, go to the next one
  74.         if (0 != problem[row][col])
  75.         {
  76.             return solve(row, col + 1);
  77.         }
  78.         else
  79.         {
  80.             // Try all possible numbers
  81.             boolean solved = false;
  82.             for (int i = 1; i <= 9; ++i)
  83.             {
  84.                 if (isPossibleDigit(i, row, col))
  85.                 {
  86.                     // Mark it and recurse on the next box
  87.                     problem[row][col] = i;
  88.                     solved = solved || solve(row, col + 1);
  89.                     // Unmark it after recursing
  90.                     problem[row][col] = 0;
  91.                 }
  92.             }
  93.             return solved;
  94.         }
  95.     }
  96.    
  97.      /**
  98.      * Prints the 9x9 array (problem) as a Sudoku board.
  99.      * If the array element is 0, print a . instead of the 0.
  100.      */
  101.     public void print()
  102.     {  
  103.         //complete this method
  104.         for(int index = 0; index < 9; index++)
  105.         {
  106.             if((index % 3) == 0)
  107.             {
  108.                 System.out.println("+-------+-------+-------+");
  109.             }
  110.             for(int i = 0; i < 9; i++)
  111.             {
  112.                 if((i % 3) == 0)
  113.                 {
  114.                     System.out.print("| ");
  115.                 }
  116.                 if(problem[index][i] == 0)
  117.                 {
  118.                     System.out.print(". ");
  119.                 }
  120.                 else
  121.                 {
  122.                     System.out.print(problem[index][i] + " ");
  123.                 }
  124.             }
  125.             System.out.println("|");
  126.         }
  127.         System.out.println("+-------+-------+-------+");
  128.     }
  129.  
  130.     /**
  131.      * Returns a 3x3 array representing a "box" of the 9x9 array.
  132.      * The parameters boxRow and boxColumn are in the range 0 to 2,
  133.      * since there are three rows and  three columns of "boxes."
  134.      *
  135.      * @param boxRow The row number, 0, 1, or 2.
  136.      * @param boxColumn  The column number, 0, 1, or 2.
  137.      * @return The 3x3 array representing a "box" of the 9x9 array.
  138.      */
  139.     public int[][] getBox(int boxRow, int boxColumn)
  140.     {
  141.         int[][] box = new int[3][3];
  142.        
  143.         //complete this method
  144.        
  145.         if(boxRow > 2)
  146.         {
  147.            
  148.             boxRow = 2;
  149.         }
  150.         else if(boxRow < 0)
  151.         {
  152.            
  153.             boxRow = 0;
  154.         }
  155.        
  156.        
  157.         if(boxColumn > 2)
  158.         {
  159.            
  160.             boxColumn = 2;
  161.         }
  162.         else if(boxColumn < 0)
  163.         {
  164.            
  165.             boxColumn = 0;
  166.         }
  167.        
  168.         int rowOffset = boxRow * 3;
  169.         int colOffset = boxColumn * 3;
  170.        
  171.         for(int index = 0; index < 3; index++)
  172.         {
  173.             for(int i = 0; i < 3; i++)
  174.             {
  175.                 box[index][i] = problem[rowOffset + index][colOffset + i];
  176.             }
  177.         }
  178.         return box;
  179.  
  180.     }
  181.  
  182.     /**
  183.      * Returns true if the given digit (which must be 1..9) occurs
  184.      * in the given row (rows are 0..8), and false otherwise.
  185.      *
  186.      * @param digit The digit to be searched for in the given row.
  187.      * @param row The row to search.
  188.      * @return true if the digit is found, otherwise return false.
  189.      */
  190.     public boolean occursInRow(int digit, int row)
  191.     {
  192.         boolean digitCheck = false;
  193.         if(digit > 9)
  194.         {
  195.             System.out.println("Digit too high.  Setting to 9.");
  196.             digit = 9;
  197.         }
  198.         else if(digit < 1)
  199.         {
  200.             System.out.println("Digit is too small.  Setting to 1");
  201.             digit = 1;
  202.         }
  203.         for(int index = 0; index < 9; index++)
  204.         {
  205.             if(problem[row][index] == digit)
  206.             {
  207.                 digitCheck = true;
  208.             }
  209.         }
  210.        
  211.         return digitCheck;
  212.     }
  213.  
  214.     /**
  215.      * Returns true if the given digit (which must be 1..9) occurs
  216.      * in the given column (columns are 0..8), and false otherwise.
  217.      *
  218.      * @param digit The digit to be searched for in the given column.
  219.      * @param column The column to search.
  220.      * @return true if the digit is found, otherwise return false.
  221.      */
  222.     public boolean occursInColumn(int digit, int column)
  223.     {
  224.         //complete this method
  225.         boolean digitCheck = false;
  226.         if(digit > 9)
  227.         {
  228.             System.out.println("Digit too high.  Setting to 9.");
  229.             digit = 9;
  230.         }
  231.         else if(digit < 1)
  232.         {
  233.             System.out.println("Digit is too small.  Setting to 1");
  234.             digit = 1;
  235.         }
  236.        
  237.         for(int index = 0; index < 9; index++)
  238.         {
  239.             if(problem[index][column] == digit)
  240.             {
  241.                 digitCheck = true;
  242.             }
  243.         }
  244.        
  245.         return digitCheck;
  246.     }
  247.    
  248.     /**
  249.      * Returns true if the given digit (which must be 1..9) occurs
  250.      * in the given 3x3 box, and false otherwise.
  251.      *
  252.      * @param digit The digit to search for.
  253.      * @param box A 3x3 array in in which to search.
  254.      * @return true if the given digit is found, otherwise return false.
  255.      */
  256.     public boolean occursInBox(int digit, int[][] box)
  257.     {
  258.         //complete this method
  259.         boolean digitCheck = false;
  260.         if(digit > 9)
  261.         {
  262.             System.out.println("Digit too high.  Setting to 9.");
  263.             digit = 9;
  264.         }
  265.         else if(digit < 1)
  266.         {
  267.             System.out.println("Digit is too small.  Setting to 1");
  268.             digit = 1;
  269.         }
  270.        
  271.         for(int index = 0; index < 3; index++)
  272.         {
  273.             for(int i = 0; i < 3; i++)
  274.             {
  275.                 if(box[index][i] == digit)
  276.                 {
  277.                     digitCheck = true;
  278.                 }
  279.             }
  280.         }
  281.         return digitCheck;
  282.     }
  283.  
  284.  
  285.     /**
  286.      * Returns true if the given digit (which must be 1..9) occurs in the box
  287.      * containing the location at the given row and column of the 9x9 array, and
  288.      * false otherwise. Note that this method is given a row and column in the
  289.      * complete 9x9 array, but must search for the given digit in the box containing
  290.      * that (row, column) location.
  291.      *
  292.      * @param digit The digit to be searched for in the appropriate box.
  293.      * @param row A row number in the range 0 to 8.
  294.      * @param column  A column number in the range 0 to 8.
  295.      * @return true if the given digit is found in the same box
  296.      * that contains the given row and column, otherwise return false.
  297.      */
  298.     public boolean occursInBox(int digit, int row, int column)
  299.     {
  300.         if(digit > 9)
  301.         {
  302.             System.out.println("Digit too high.  Setting to 9.");
  303.             digit = 9;
  304.         }
  305.         else if(digit < 1)
  306.         {
  307.             System.out.println("Digit is too small.  Setting to 1");
  308.             digit = 1;
  309.         }
  310.        
  311.         if(row > 8)
  312.         {
  313.             row = 8;
  314.         }
  315.         else if(row < 0)
  316.         {
  317.             row = 0;
  318.         }
  319.         if(column > 8)
  320.         {
  321.             column = 8;
  322.         }
  323.         else if(column < 0)
  324.         {
  325.             column = 0;
  326.         }
  327.        
  328.         boolean digitCheck = false;
  329.         int[][] boxCheck = getBox((row / 3), (column / 3));
  330.  
  331.         if(occursInBox(digit, boxCheck))
  332.         {
  333.             digitCheck = true;
  334.         }      
  335.        
  336.         return digitCheck;
  337.        
  338.        
  339.     }
  340.  
  341.    
  342.     /**
  343.      * Returns true if the given digit (which must be 1..9) does not occur in the
  344.      * given row, or in the given column, or in the box containing this row and
  345.      * column, and false otherwise. That is, this digit is a possible candidate for
  346.      * putting in this location; there may be other candidates.
  347.      *
  348.      * @param digit The candidate digit.
  349.      * @param row The row in which we wish to place the digit.
  350.      * @param column  The column in which we wish to place the digit.
  351.      * @return true if the candidate digit does not already occur
  352.      *         in the same row, or in the same column, or in the same box.
  353.      */
  354.     public boolean isPossibleDigit(int digit, int row, int column)
  355.     {
  356.         //complete this method
  357.         boolean digitCheck = true;
  358.        
  359.         if(occursInRow(digit, row) || occursInColumn(digit, column) || occursInBox(digit, row, column))
  360.         {
  361.             digitCheck = false;
  362.         }
  363.        
  364.         return digitCheck;
  365.     }
  366. }
Advertisement
Add Comment
Please, Sign In to add comment