Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public boolean isValid() {
- 37
- return (this.isRowValid() & this.isComValid() & this.isGridValid());
- 38
- }
- 39
- 40
- /**
- 41
- * proves if the rows is valide after the rules for sudoku.
- 42
- *
- 43
- * @return boolean if the rows is valide
- 44
- */
- 45
- public boolean isRowValid() {
- 46
- for (int col9 = 0; col9 < 9; col9++) {
- 47
- for (int row9 = 0; row9 < 9; row9++) {
- 48
- for (int row2 = 0; row2 < 9; row2++) {
- 49
- if (getValueAt(row9, col9) != -1) {
- 50
- if ((getValueAt(row9, col9) == getValueAt(row2, col9)) && (row9 != row2)) {
- 51
- return false;
- 52
- }
- 53
- }
- 54
- }
- 55
- }
- 56
- }
- 57
- return true;
- 58
- }
- 59
- 60
- /**
- 61
- * proves if the colums is valide after the rules for sudoku.
- 62
- *
- 63
- * @return boolean if the colums is valide
- 64
- */
- 65
- public boolean isComValid() {
- 66
- for (int row0 = 0; row0 < 9; row0++) {
- 67
- for (int col0 = 0; col0 < 9; col0++) {
- 68
- for (int col2 = 0; col2 < 9; col2++) {
- 69
- if (getValueAt(row0, col0) != -1) {
- 70
- if ((getValueAt(row0, col0) == getValueAt(row0, col2)) && (col0 != col2)) {
- 71
- return false;
- 72
- }
- 73
- }
- 74
- }
- 75
- }
- 76
- }
- 77
- return true;
- 78
- }
- 79
- 80
- /**
- 81
- * prooves if one spesific 3 times 3 grid on the board is valid.
- 82
- *
- 83
- * @param x for the cordinat of the upper left corner of the grid.
- 84
- * @param y for the cordinat of the upper left corner of the grid.
- 85
- * @return boolean if that one grid its called on is valid.
- 86
- */
- 87
- /*
- 88
- * public boolean isOneGridValid(int x, int y) { Set<Integer> set = new
- 89
- * HashSet<>(); for (int i = 1; i < 10; i++) set.add(i); set.add(-1); for (int
- 90
- * row = x; row < x + 4; row++) { for (int col = y; col < y + 4; col++) { if
- 91
- * (!set.contains(getValueAt(row, col))) { return false; }
- 92
- * set.remove(getValueAt(row, col)); set.add(-1); } } return true; }
- 93
- */
- 94
- /**
- 95
- * look if there are no dobble number in one 3 times 3 grid.
- 96
- *
- 97
- * @param x coordiate for upper left corner of grid
- 98
- * @param y coordiate for upper left corner of grid
- 99
- * @return if one grid is valid
- 100
- */
- 101
- public boolean isOneGridValid(int x, int y) {
- 102
- for (int col = y; col <= y + 3; col++) {
- 103
- for (int row = x; row <= x + 3; row++) {
- 104
- for (int col1 = col; col1 <= y + 2; col1++) {
- 105
- for (int row1 = row; row1 <= x + 2; row1++) {
- 106
- if (getValueAt(row, col) != -1) {
- 107
- if ((getValueAt(row, col) == getValueAt(row1, col1)) && ((col != col1) && (row != row1))) {
- 108
- return false;
- 109
- }
- 110
- }
- 111
- }
- 112
- }
- 113
- }
- 114
- }
- 115
- return true;
- 116
- }
- 117
- 118
- /**
- 119
- * this mathod proves if the number is the grids are valid.
- 120
- *
- 121
- * @return boolean if the whole grid is valid.
- 122
- */
- 123
- public boolean isGridValid() {
- 124
- for (int row6 = 0; row6 < 7; row6 += 3) {
- 125
- for (int col6 = 0; col6 < 7; col6 += 3) {
- 126
- if (!this.isOneGridValid(row6, col6))
- 127
- // if (!this.isOneGridValid(row6, col6))
- 128
- return false;
- 129
- }
- 130
- }
- 131
- return true;
- 132
- }
- 133
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement