Guest User

Untitled

a guest
Jul 16th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. /**
  4. * The Sudoku field, containing 9x9 squares.
  5. *
  6. * @author Lucas Taubert
  7. * @version 2009-10-26
  8. */
  9. public class Field{
  10. Square[][] squares;
  11. int size;
  12. // private ArrayList<Square> squares;
  13.  
  14. /**
  15. * Initiates a field with the sides 'size'.
  16. * @param size The number of squares on each side.
  17. */
  18. public Field(int size){
  19. this.size = size;
  20. squares = new Square[size][size];
  21. for(int i=0;i<size;i++){
  22. for(int j=0;j<size;j++){
  23. squares[i][j] = new Square(i+1, j+1);
  24. }
  25. }
  26. }
  27.  
  28. /**
  29. * Removes choices in the row, column and 3x3 square of
  30. * selected square.
  31. * @param square The square of which to receive data.
  32. * @return False if the square didn't have any value, else true.
  33. */
  34. private boolean removeChoices(Square square){
  35. boolean removed = false;
  36. int x = square.getX()-1;
  37. int y = square.getY()-1;
  38. if(square.getValue() == null){
  39. return false;
  40. }
  41. int value = square.getValue();
  42. for(int i=0; i<size;i++){ //Purges a column with X-value x.
  43. if(squares[x-1][i].purgeChoice(value)){
  44. removed = true;
  45. }
  46. }
  47. for(int i=0;i<size;i++){ //Purges a row with Y-value y.
  48. if(squares[i][y-1].purgeChoice(value)){
  49. removed = true;
  50. }
  51. }
  52. for(int xx = x - x%3; xx< (x - x%3) + 3; xx++){ //Purges the 3x3 of the x and y coordinates.
  53. for(int yy = y - y%3; yy<(y - y%3) + 3; yy++){
  54. if(squares[xx][yy].purgeChoice(value)){
  55. removed = true;
  56. }
  57. }
  58. }
  59. return removed;
  60. }
  61.  
  62. /**
  63. * Removes choices through a square on specified position.
  64. * @param x The X-coordinate.
  65. * @param y The Y-coordinate.
  66. */
  67. public boolean removeChoices(int x, int y){
  68. return removeChoices(squares[x][y]);
  69. }
  70.  
  71. /**
  72. * Prints the field.
  73. * @return The string which to print
  74. */
  75. public String toString(){
  76. String myString = "";
  77. for(int xx = 0; xx<size; xx++){
  78. for(int yy= 0; yy<size; yy++){
  79. myString = myString + " " + squares[xx][yy];
  80. }
  81. myString = myString + "\n";
  82. }
  83. return myString;
  84. }
  85.  
  86. /**
  87. * Prints the field.
  88. */
  89. public void printField(){
  90. System.out.print("\n" + this);
  91. }
  92.  
  93. /**
  94. * Adds a value in the square of specified coordinates.
  95. * @param x The X-coordinate.
  96. * @param y The Y-coordinate.
  97. * @param value The value which the square shall have.
  98. */
  99. public void addValue(int x, int y, int value){
  100. squares[x-1][y-1].setValue(value);
  101. }
  102.  
  103. public void addValues(int[] x, int[] y, int[] value){
  104. int counter = 0;
  105. for(int i=0; i<x.length; i++){
  106. for(int j=0; j<y.length; j++){
  107. addValue(x[i], y[j], value[counter]);
  108. counter++;
  109. }
  110. }
  111. }
  112.  
  113. /**
  114. * Removes choices by checking data on all squares
  115. * on the field.
  116. */
  117. public boolean removeField(){
  118. boolean removed = false;
  119. for(int xx=0; xx<size; xx++){
  120. for(int yy=0; yy<size; yy++){
  121. if(removeChoices(squares[xx][yy])){
  122. removed = true;
  123. }
  124. }
  125. }
  126. return removed;
  127. }
  128.  
  129. /**
  130. * Checks if any fields only have one choice left. Updates value
  131. * if it does.
  132. */
  133. public boolean checkField(){
  134. boolean changed = false;
  135. for(int xx=0; xx<size; xx++){
  136. for(int yy=0; yy<size; yy++){
  137. if(squares[xx][yy].check()){
  138. changed = true;
  139. }
  140. }
  141. }
  142. return changed;
  143. }
  144.  
  145. /**
  146. * Attempts to solve the puzzle.
  147. * @param number of tries.
  148. */
  149. public void solve(int tries){
  150. boolean solved=false;
  151. int counter=0;
  152. while(!solved && counter++<tries){
  153. solved = true;
  154. if(checkField() | removeField()){
  155. solved = false;
  156. }
  157. }
  158. }
  159.  
  160. /**
  161. * Initiate a test puzzle.
  162. */
  163. public void initiatePuzzle(){
  164. addValues({1},{1,4,6,7},{5,1,6,4);
  165. addValues({2},{2,3,4},{2,4,5});
  166. addValues({3},{2,6,9},{1,7,8});
  167. addValues({4},{5,7,9},{2,7,1});
  168. addValues({5},{1,7,9},{6,5,9});
  169. addValues({6},{4,5},{7,1});
  170. addValues({7},{3},{5});
  171. addValues({8},{2,5,6},{7,8});
  172. addValues({9},{7,8},{1,4});
  173. }
  174. }
Add Comment
Please, Sign In to add comment