Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. /**
  2. * proves if the boad is valide after the rules for sudoku.
  3. *
  4. * @return boolean if the board is valide
  5. */
  6. public boolean isValid() {
  7. return (this.isRowValid() & this.isComValid() & this.isGridValid());
  8. }
  9.  
  10. /**
  11. * proves if the rows is valide after the rules for sudoku.
  12. *
  13. * @return boolean if the rows is valide
  14. */
  15. public boolean isRowValid() {
  16. for (int col9 = 0; col9 < 9; col9++) {
  17. for (int row9 = 0; row9 < 9; row9++) {
  18. for (int row2 = 0; row2 < 9; row2++) {
  19. if (getValueAt(row9, col9) != -1) {
  20. if ((getValueAt(row9, col9) == getValueAt(row2, col9)) && (row9 != row2)) {
  21. return false;
  22. }
  23. }
  24. }
  25. }
  26. }
  27. return true;
  28. }
  29.  
  30. /**
  31. * proves if the colums is valide after the rules for sudoku.
  32. *
  33. * @return boolean if the colums is valide
  34. */
  35. public boolean isComValid() {
  36. for (int row0 = 0; row0 < 9; row0++) {
  37. for (int col0 = 0; col0 < 9; col0++) {
  38. for (int col2 = 0; col2 < 9; col2++) {
  39. if (getValueAt(row0, col0) != -1) {
  40. if ((getValueAt(row0, col0) == getValueAt(row0, col2)) && (col0 != col2)) {
  41. return false;
  42. }
  43. }
  44. }
  45. }
  46. }
  47. return true;
  48. }
  49.  
  50. /**
  51. * prooves if one spesific 3 times 3 grid on the board is valid.
  52. *
  53. * @param x for the cordinat of the upper left corner of the grid.
  54. * @param y for the cordinat of the upper left corner of the grid.
  55. * @return boolean if that one grid its called on is valid.
  56. */
  57. /*
  58. * public boolean isOneGridValid(int x, int y) { Set<Integer> set = new
  59. * HashSet<>(); for (int i = 1; i < 10; i++) set.add(i); set.add(-1); for (int
  60. * row = x; row < x + 4; row++) { for (int col = y; col < y + 4; col++) { if
  61. * (!set.contains(getValueAt(row, col))) { return false; }
  62. * set.remove(getValueAt(row, col)); set.add(-1); } } return true; }
  63. */
  64. /**
  65. * look if there are no dobble number in one 3 times 3 grid.
  66. *
  67. * @param x coordiate for upper left corner of grid
  68. * @param y coordiate for upper left corner of grid
  69. * @return if one grid is valid
  70. */
  71. public boolean isOneGridValid(int x, int y) {
  72. for (int col = y; col <= y + 3; col++) {
  73. for (int row = x; row <= x + 3; row++) {
  74. for (int col1 = col; col1 <= y + 2; col1++) {
  75. for (int row1 = row; row1 <= x + 2; row1++) {
  76. if (getValueAt(row, col) != -1) {
  77. if ((getValueAt(row, col) == getValueAt(row1, col1)) && ((col != col1) && (row != row1))) {
  78. return false;
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. return true;
  86. }
  87.  
  88. /**
  89. * this mathod proves if the number is the grids are valid.
  90. *
  91. * @return boolean if the whole grid is valid.
  92. */
  93. public boolean isGridValid() {
  94. for (int row6 = 0; row6 < 7; row6 += 3) {
  95. for (int col6 = 0; col6 < 7; col6 += 3) {
  96. if (!this.isOneGridValid(row6, col6))
  97. // if (!this.isOneGridValid(row6, col6))
  98. return false;
  99. }
  100. }
  101. return true;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement