Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. public boolean isValid() {
  2. 37
  3. return (this.isRowValid() & this.isComValid() & this.isGridValid());
  4. 38
  5. }
  6. 39
  7.  
  8. 40
  9. /**
  10. 41
  11. * proves if the rows is valide after the rules for sudoku.
  12. 42
  13. *
  14. 43
  15. * @return boolean if the rows is valide
  16. 44
  17. */
  18. 45
  19. public boolean isRowValid() {
  20. 46
  21. for (int col9 = 0; col9 < 9; col9++) {
  22. 47
  23. for (int row9 = 0; row9 < 9; row9++) {
  24. 48
  25. for (int row2 = 0; row2 < 9; row2++) {
  26. 49
  27. if (getValueAt(row9, col9) != -1) {
  28. 50
  29. if ((getValueAt(row9, col9) == getValueAt(row2, col9)) && (row9 != row2)) {
  30. 51
  31. return false;
  32. 52
  33. }
  34. 53
  35. }
  36. 54
  37. }
  38. 55
  39. }
  40. 56
  41. }
  42. 57
  43. return true;
  44. 58
  45. }
  46. 59
  47.  
  48. 60
  49. /**
  50. 61
  51. * proves if the colums is valide after the rules for sudoku.
  52. 62
  53. *
  54. 63
  55. * @return boolean if the colums is valide
  56. 64
  57. */
  58. 65
  59. public boolean isComValid() {
  60. 66
  61. for (int row0 = 0; row0 < 9; row0++) {
  62. 67
  63. for (int col0 = 0; col0 < 9; col0++) {
  64. 68
  65. for (int col2 = 0; col2 < 9; col2++) {
  66. 69
  67. if (getValueAt(row0, col0) != -1) {
  68. 70
  69. if ((getValueAt(row0, col0) == getValueAt(row0, col2)) && (col0 != col2)) {
  70. 71
  71. return false;
  72. 72
  73. }
  74. 73
  75. }
  76. 74
  77. }
  78. 75
  79. }
  80. 76
  81. }
  82. 77
  83. return true;
  84. 78
  85. }
  86. 79
  87.  
  88. 80
  89. /**
  90. 81
  91. * prooves if one spesific 3 times 3 grid on the board is valid.
  92. 82
  93. *
  94. 83
  95. * @param x for the cordinat of the upper left corner of the grid.
  96. 84
  97. * @param y for the cordinat of the upper left corner of the grid.
  98. 85
  99. * @return boolean if that one grid its called on is valid.
  100. 86
  101. */
  102. 87
  103. /*
  104. 88
  105. * public boolean isOneGridValid(int x, int y) { Set<Integer> set = new
  106. 89
  107. * HashSet<>(); for (int i = 1; i < 10; i++) set.add(i); set.add(-1); for (int
  108. 90
  109. * row = x; row < x + 4; row++) { for (int col = y; col < y + 4; col++) { if
  110. 91
  111. * (!set.contains(getValueAt(row, col))) { return false; }
  112. 92
  113. * set.remove(getValueAt(row, col)); set.add(-1); } } return true; }
  114. 93
  115. */
  116. 94
  117. /**
  118. 95
  119. * look if there are no dobble number in one 3 times 3 grid.
  120. 96
  121. *
  122. 97
  123. * @param x coordiate for upper left corner of grid
  124. 98
  125. * @param y coordiate for upper left corner of grid
  126. 99
  127. * @return if one grid is valid
  128. 100
  129. */
  130. 101
  131. public boolean isOneGridValid(int x, int y) {
  132. 102
  133. for (int col = y; col <= y + 3; col++) {
  134. 103
  135. for (int row = x; row <= x + 3; row++) {
  136. 104
  137. for (int col1 = col; col1 <= y + 2; col1++) {
  138. 105
  139. for (int row1 = row; row1 <= x + 2; row1++) {
  140. 106
  141. if (getValueAt(row, col) != -1) {
  142. 107
  143. if ((getValueAt(row, col) == getValueAt(row1, col1)) && ((col != col1) && (row != row1))) {
  144. 108
  145. return false;
  146. 109
  147. }
  148. 110
  149. }
  150. 111
  151. }
  152. 112
  153. }
  154. 113
  155. }
  156. 114
  157. }
  158. 115
  159. return true;
  160. 116
  161. }
  162. 117
  163.  
  164. 118
  165. /**
  166. 119
  167. * this mathod proves if the number is the grids are valid.
  168. 120
  169. *
  170. 121
  171. * @return boolean if the whole grid is valid.
  172. 122
  173. */
  174. 123
  175. public boolean isGridValid() {
  176. 124
  177. for (int row6 = 0; row6 < 7; row6 += 3) {
  178. 125
  179. for (int col6 = 0; col6 < 7; col6 += 3) {
  180. 126
  181. if (!this.isOneGridValid(row6, col6))
  182. 127
  183. // if (!this.isOneGridValid(row6, col6))
  184. 128
  185. return false;
  186. 129
  187. }
  188. 130
  189. }
  190. 131
  191. return true;
  192. 132
  193. }
  194. 133
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement