Advertisement
jdalbey

Sudoku Verifier

Mar 27th, 2016
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.60 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.lang.*;
  3. import java.util.*;
  4.  
  5. /**
  6.  * Write a program to perform verification for a Sudoku board.
  7.  *
  8.  * @author your name here
  9.  * @version 0.1
  10.  */
  11. public class DefectiveSudoku
  12. {
  13.     /**
  14.      * Determine if a Sudoku board is in a legal configuration.
  15.      * The board may be partially complete, in which case empty
  16.      * cells are represented by a dot (.).
  17.      * @param board a string containing the symbols in a 9x9 or
  18.      * 16x16 Sudoku board in row-major order.
  19.      * @pre board.length() is 81 or 256
  20.      * @pre board 9x9 board uses symbols 1-9, 16x16 board uses A-P (uppercase).
  21.      * @return true if the board configuration is legal, false otherwise.  
  22.      * That is, no row,
  23.      * column or square contains a duplicate symbol.
  24.      */
  25.     public boolean verify(String board)
  26.     {
  27.         if (board.length() == 81 || board.length() == 256)
  28.         {
  29.             int save = board.length();
  30.             int stop = 0;
  31.             int k = 0;
  32.        
  33.             save = (int) Math.sqrt(save);
  34.        
  35.             for (int i = save; i < save * save + save; i = i + save)
  36.             {
  37.                 if (!verifyNumberOnce(board.substring(i - save, i), save))
  38.                 {
  39.                     return false;
  40.                 }
  41.             }
  42.            
  43.             for (int i = 0; i < save; i++)
  44.             {
  45.                 String boardCol = "";
  46.                 for (int j = i; j < save * save; j = j + save)
  47.                 {
  48.                     boardCol = boardCol + board.charAt(j);
  49.                 }
  50.            
  51.                 if (!verifyNumberOnce(boardCol, save))
  52.                 {
  53.                     return false;
  54.                 }
  55.             }
  56.            
  57.             if (save == 9)
  58.             {
  59.                 if (!verifyNine(board))
  60.                 {
  61.                     return false;
  62.                 }
  63.             }
  64.             else if (save == 16)
  65.             {
  66.                 if (!verifySixteen(board))
  67.                 {
  68.                     return false;
  69.                 }
  70.             }
  71.         }
  72.         else
  73.         {
  74.         return false;
  75.         }
  76.        
  77.         return true;
  78.     }
  79.    
  80.     /**
  81.      * When Sudoku board is 9x9.
  82.      *
  83.      * @pre     The data holding the data for the boxes part of the Sudoku.
  84.      */
  85.     public boolean verifyNine(String board)
  86.     {
  87.         for (int i = 0; i < 63; i = i + 3)
  88.         {
  89.             if (i == 9) i = 27;
  90.             if (i == 36) i = 54;
  91.             String boardThrees = "";
  92.            
  93.             boardThrees = boardThrees + board.charAt(i)
  94.                 + board.charAt(i + 1) + board.charAt(i + 2);
  95.             boardThrees = boardThrees + board.charAt(i + 9)
  96.                 + board.charAt(i + 1 + 9) + board.charAt(i + 2 + 9);
  97.             boardThrees = boardThrees + board.charAt(i + 18)
  98.                 + board.charAt(i + 1 + 18) + board.charAt(i + 2 + 18);
  99.            
  100.             if (verifyNumberOnce(boardThrees, 9))
  101.             {
  102.                 return true;
  103.             }
  104.            
  105.         }
  106.         return true;
  107.     }
  108.    
  109.     /**
  110.      * When Sudoku board is 16x16.
  111.      *
  112.      * @pre     The data holding the data for the boxes part of the Sudoku.
  113.      */
  114.     public boolean verifySixteen(String board)
  115.     {
  116.         for (int i = 0; i < 208; i = i + 4)
  117.         {
  118.             if (i == 16)
  119.             {
  120.                 i = 64;
  121.             }
  122.             if (i == 80)
  123.             {
  124.                 i = 128;
  125.             }
  126.             if (i == 144)
  127.             {
  128.                 i = 192;
  129.             }
  130.             String boardFours = "";
  131.            
  132.             boardFours = boardFours + board.charAt(i)
  133.             + board.charAt(i + 1) + board.charAt(i + 2) + board.charAt(i + 3);
  134.             boardFours = boardFours + board.charAt(i + 16)
  135.                 + board.charAt(i + 1 + 16) + board.charAt(i + 2 + 16)
  136.                 + board.charAt(i + 3 + 16);
  137.             boardFours = boardFours + board.charAt(i + 32)
  138.                 + board.charAt(i + 1 + 32) + board.charAt(i + 2 + 32)
  139.                 + board.charAt(i + 3 + 32);
  140.             boardFours = boardFours + board.charAt(i + 48)
  141.                 + board.charAt(i + 1 + 48) + board.charAt(i + 2 + 48)
  142.                 + board.charAt(i + 3 + 48);
  143.            
  144.             if (!verifyNumberOnce(boardFours, 16))
  145.             {
  146.                 return false;
  147.             }
  148.            
  149.         }
  150.         return true;
  151.     }
  152.    
  153.     /**
  154.      * When Sudoku board is split up. Check to see if split up part is valid.
  155.      *
  156.      * @pre     The data holding the data for the split part of the Sudoku.
  157.      */
  158.     public boolean verifyNumberOnce(String board, int save)
  159.     {
  160.         for (int i = 0; i < board.length(); i++)
  161.         {
  162.             for (int j = 0; j < board.length(); j++)
  163.             {
  164.                 if (board.charAt(i) == board.charAt(j)
  165.                     && board.charAt(j) != '.'
  166.                     && i != j)
  167.                 {
  168.                     return false;
  169.                 }
  170.             }
  171.         }
  172.         return true;
  173.     }
  174.    
  175.     /**
  176.      * A local main for console testing.
  177.      * Get the data to calculate if Sudoku is valid.
  178.      *
  179.      * @pre     The data holding the data of the Sudoku.
  180.      */
  181.     public static void main(String[] args)
  182.     {
  183.         Scanner sc = new Scanner(System.in);
  184.        
  185.         while (sc.hasNextLine())
  186.         {
  187.             String str = sc.nextLine();
  188.             DefectiveSudoku app = new DefectiveSudoku();
  189.             System.out.println(app.verify(str));
  190.         }
  191.     }
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement