Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sudokuGenerator;
- import java.util.*;
- import java.util.Scanner;
- public class SudokuBoard {
- private ArrayList<ArrayList<SudokuSquare>> board = new ArrayList<ArrayList<SudokuSquare>>(9);
- private int arrayWidth = 9;
- private int arrayLength = 9;
- public SudokuBoard()
- {
- fillFirst(board);
- fillWithNum(board);
- }
- private boolean infringesRow(ArrayList<ArrayList<SudokuSquare>> board, SudokuSquare a)
- {
- for (int i = 0; i < board.get(0).size(); i ++)
- {
- if (a.getColumn() == i)
- continue;
- if (board.get(a.getRow()).get(i).getValue() == a.getValue())
- return true;
- }
- return false;
- }
- private boolean infringesColumn(ArrayList<ArrayList<SudokuSquare>> board, SudokuSquare a){
- for (int i = 0; i < board.size(); i ++)
- {
- if (a.getRow() == i)
- continue;
- if (board.get(i).get(a.getColumn()).getValue() == a.getValue())
- return true;
- }
- return false;
- }
- private boolean infringesSquare(ArrayList<ArrayList<SudokuSquare>> board,SudokuSquare a){
- for (int i = 0; i < board.size(); i ++ )
- {
- for (int j = 0; j < board.get(0).size(); j++)
- {
- if (a.getColumn() == j && a.getRow() == i)
- continue;
- if (board.get(i).get(j).getSquare() == a.getSquare() && board.get(i).get(j).getValue() == a.getValue())
- return true;
- }
- }
- return false;
- }
- private boolean infringesConditions(ArrayList<ArrayList<SudokuSquare>> board,SudokuSquare a )
- {
- if (infringesRow(board, a) == true)
- return true;
- else if (infringesColumn(board, a) == true)
- return true;
- else if (infringesSquare(board,a) == true)
- return true;
- return false;
- }
- private void resetRow(ArrayList<ArrayList<SudokuSquare>> board, int row)
- {
- for (int i = 0; i < board.get(row).size(); i ++)
- {
- board.get(row).get(i).setValue(-1);
- }
- }
- public void fillFirst(ArrayList<ArrayList<SudokuSquare>> board)
- {
- for (int i = 0; i < arrayLength; i++)
- {
- board.add(i, new ArrayList<SudokuSquare>(9));
- for (int j = 0; j < arrayWidth; j++)
- {
- board.get(i).add(j, new SudokuSquare());
- board.get(i).get(j).setValue(-1);;
- board.get(i).get(j).setColumn(j);
- board.get(i).get(j).setRow(i);
- if (i <= 2 && j <= 2)
- board.get(i).get(j).setSquare(0);
- else if (i <= 2 && j <= 5)
- board.get(i).get(j).setSquare(1);
- else if (i <= 2 && j <= 8)
- board.get(i).get(j).setSquare(2);
- else if (i <= 5 && j <= 2)
- board.get(i).get(j).setSquare(3);
- else if (i <= 5 && j <= 5)
- board.get(i).get(j).setSquare(4);
- else if (i <= 5 && j <= 8)
- board.get(i).get(j).setSquare(5);
- else if (i <= 8 && j <= 2)
- board.get(i).get(j).setSquare(6);
- else if (i <= 8 && j <= 5)
- board.get(i).get(j).setSquare(7);
- else if (i <= 8 && j <= 8)
- board.get(i).get(j).setSquare(8);
- }
- }
- }
- private void fillWithNum(ArrayList<ArrayList<SudokuSquare>> board)
- {
- int counter = 0;
- int counter2 = 0;
- boolean shouldBreak = false;
- boolean meetsConditions = true;
- for (int i = 0; i < board.size();i++)
- {
- conditionalLoop:
- for (int j = 0; j < board.get(0).size(); j ++)
- {
- while(meetsConditions == true){
- board.get(i).get(j).setValue( (int) (Math.random()*9 + 1));
- meetsConditions = infringesConditions(board, board.get(i).get(j));
- counter++;
- //counter greater than x, where x can be reduced, at the cost of a bit of time
- if (counter >= 50){
- shouldBreak = true;
- meetsConditions = false;
- }
- }
- meetsConditions = true;
- counter = 0;
- if (shouldBreak == true && i >= 0)
- {
- resetRow(board,i);
- counter2++;
- i--;
- if (counter2 >= 10 && i >= 0)
- {
- i--;
- counter2 = 0;
- break conditionalLoop;
- }
- shouldBreak = false;
- break conditionalLoop;
- }
- }
- }
- }
- /*public String toString()
- {
- String output = "";
- for (int i = 0; i < this.board.size(); i ++)
- {
- for (int j = 0; j < this.board.get(0).size(); j++)
- {
- output += "" + (this.board.get(i).get(j).getValue()) + " ";
- }
- output += "\n";
- }
- return output;
- }
- public int getLength() {return board.size();}
- public int getLength(int a) {return board.get(0).size();}
- public int getElement(int a, int b){return board.get(a).get(b).getValue();}
- */
- @Override
- public String toString() {
- StringBuilder s = new StringBuilder();
- s.append("+-------+-------+-------+\n");
- for (int z = 0; z < 9; z++) {
- s.append("| ");
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- int num = board.get(z).get(i*3 + j).getValue();
- s.append(num == 0 ? "_" : num);
- if (j != 2) {
- s.append(" ");
- }
- }
- if (i != 2) {
- s.append(" | ");
- }
- }
- s.append(" |");
- if (z != 8) {
- s.append("\n" + ((z+1) % 3 == 0 ? "+-------+-------+-------+\n" : ""));
- }
- }
- s.append("\n+-------+-------+-------+");
- return s.toString();
- }
- public static void main(String[] args) {
- long startTime = System.nanoTime();
- //your code that you run
- SudokuBoard board = new SudokuBoard();
- System.out.println(board);
- long endTime = System.nanoTime();
- double duration = (endTime - startTime)/100000000.0;
- System.out.println("Seconds taken: " + duration);
- }
- }
- ////////////////////////////////////////////////////////////////
- package sudokuGenerator;
- public class SudokuSquare {
- private int value;
- private int row;
- private int column;
- private int square;
- public SudokuSquare()
- {
- }
- public SudokuSquare(int a)
- {
- value = a;
- }
- public String toString()
- {
- return "" + value;
- }
- public void setValue(int a){value = a;}
- public void setRow(int a){row = a;}
- public void setColumn(int a){column = a;}
- public void setSquare(int a){square = a;}
- public int getValue(){return value;}
- public int getRow(){return row;}
- public int getColumn(){return column;}
- public int getSquare(){return square;}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement