Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- /**
- * The Sudoku field, containing 9x9 squares.
- *
- * @author Lucas Taubert
- * @version 2009-10-26
- */
- public class Field{
- Square[][] squares;
- int size;
- // private ArrayList<Square> squares;
- /**
- * Initiates a field with the sides 'size'.
- * @param size The number of squares on each side.
- */
- public Field(int size){
- this.size = size;
- squares = new Square[size][size];
- for(int i=0;i<size;i++){
- for(int j=0;j<size;j++){
- squares[i][j] = new Square(i+1, j+1);
- }
- }
- }
- /**
- * Removes choices in the row, column and 3x3 square of
- * selected square.
- * @param square The square of which to receive data.
- * @return False if the square didn't have any value, else true.
- */
- private boolean removeChoices(Square square){
- boolean removed = false;
- int x = square.getX()-1;
- int y = square.getY()-1;
- if(square.getValue() == null){
- return false;
- }
- int value = square.getValue();
- for(int i=0; i<size;i++){ //Purges a column with X-value x.
- if(squares[x-1][i].purgeChoice(value)){
- removed = true;
- }
- }
- for(int i=0;i<size;i++){ //Purges a row with Y-value y.
- if(squares[i][y-1].purgeChoice(value)){
- removed = true;
- }
- }
- for(int xx = x - x%3; xx< (x - x%3) + 3; xx++){ //Purges the 3x3 of the x and y coordinates.
- for(int yy = y - y%3; yy<(y - y%3) + 3; yy++){
- if(squares[xx][yy].purgeChoice(value)){
- removed = true;
- }
- }
- }
- return removed;
- }
- /**
- * Removes choices through a square on specified position.
- * @param x The X-coordinate.
- * @param y The Y-coordinate.
- */
- public boolean removeChoices(int x, int y){
- return removeChoices(squares[x][y]);
- }
- /**
- * Prints the field.
- * @return The string which to print
- */
- public String toString(){
- String myString = "";
- for(int xx = 0; xx<size; xx++){
- for(int yy= 0; yy<size; yy++){
- myString = myString + " " + squares[xx][yy];
- }
- myString = myString + "\n";
- }
- return myString;
- }
- /**
- * Prints the field.
- */
- public void printField(){
- System.out.print("\n" + this);
- }
- /**
- * Adds a value in the square of specified coordinates.
- * @param x The X-coordinate.
- * @param y The Y-coordinate.
- * @param value The value which the square shall have.
- */
- public void addValue(int x, int y, int value){
- squares[x-1][y-1].setValue(value);
- }
- public void addValues(int[] x, int[] y, int[] value){
- int counter = 0;
- for(int i=0; i<x.length; i++){
- for(int j=0; j<y.length; j++){
- addValue(x[i], y[j], value[counter]);
- counter++;
- }
- }
- }
- /**
- * Removes choices by checking data on all squares
- * on the field.
- */
- public boolean removeField(){
- boolean removed = false;
- for(int xx=0; xx<size; xx++){
- for(int yy=0; yy<size; yy++){
- if(removeChoices(squares[xx][yy])){
- removed = true;
- }
- }
- }
- return removed;
- }
- /**
- * Checks if any fields only have one choice left. Updates value
- * if it does.
- */
- public boolean checkField(){
- boolean changed = false;
- for(int xx=0; xx<size; xx++){
- for(int yy=0; yy<size; yy++){
- if(squares[xx][yy].check()){
- changed = true;
- }
- }
- }
- return changed;
- }
- /**
- * Attempts to solve the puzzle.
- * @param number of tries.
- */
- public void solve(int tries){
- boolean solved=false;
- int counter=0;
- while(!solved && counter++<tries){
- solved = true;
- if(checkField() | removeField()){
- solved = false;
- }
- }
- }
- /**
- * Initiate a test puzzle.
- */
- public void initiatePuzzle(){
- addValues({1},{1,4,6,7},{5,1,6,4);
- addValues({2},{2,3,4},{2,4,5});
- addValues({3},{2,6,9},{1,7,8});
- addValues({4},{5,7,9},{2,7,1});
- addValues({5},{1,7,9},{6,5,9});
- addValues({6},{4,5},{7,1});
- addValues({7},{3},{5});
- addValues({8},{2,5,6},{7,8});
- addValues({9},{7,8},{1,4});
- }
- }
Add Comment
Please, Sign In to add comment