Guest User

SudokuChecker

a guest
Mar 21st, 2014
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. public class Board
  2. {
  3.         private int [][] boardBits;
  4.        
  5.         public Board(int [][] setup) {
  6.                 boardBits = setup;
  7.         }
  8.        
  9.         public String checkBoard() {
  10.                 int sum = 0;
  11.                 if (noBadSpots()) {
  12.                         for (int i = 0; i < 9; i++) {
  13.                                 for (int a = 0; a < 9; a++)
  14.                                         sum += boardBits[i][a];
  15.                                 if (sum != 45)
  16.                                         return "Invalid Table";
  17.                                 sum = 0;
  18.                         }
  19.                         sum = 0;
  20.                         for (int i = 0; i < 9; i++) {
  21.                                 for (int a = 0; a < 9; a++)
  22.                                         sum += boardBits[a][i];
  23.                                 if (sum != 45)
  24.                                         return "Invalid Table";
  25.                                 sum = 0;
  26.                         }
  27.                         sum = 0;
  28.                         for (int xbase = 0; xbase <= 6; xbase += 3) {
  29.                                 for (int ybase = 0; ybase <= 6; ybase += 3) {
  30.                                         for (int i = xbase; i <= xbase + 2; i++) {
  31.                                                 for (int a = ybase; a <= ybase + 2; a++)
  32.                                                         sum += boardBits[i][a];
  33.                                         }
  34.                                         if (sum != 45)
  35.                                                 return "Invalid Table";
  36.                                         sum = 0;
  37.                                 }
  38.                         }
  39.                         return "Good Table";
  40.                 } else {
  41.                         return "Invalid Table";
  42.                 }
  43.         }
  44.        
  45.         private boolean noBadSpots()
  46.         {
  47.                 for (int a = 0; a < 9; a++) {
  48.                         if (boardBits[a][0] == boardBits[a][2])
  49.                                 return false;
  50.                         for (int b = 0; b < 9; b++)
  51.                                 if (boardBits[a][b] < 1 || boardBits[a][b] > 9)
  52.                                         return false;
  53.                 }
  54.                 return true;
  55.         }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment