package sudoku;
import java.util.*;
public class SudokuMaker {
//This is so I can use random numbers later on
Random randomnum = new Random();
//So I can ask questions of the user
Scanner reader = new Scanner(System.in);
//my state variables
int x;
//this method asks if you want directions or not by using Scanner with a string
//if you say yes it prints the directions if no it does nothing
//if you do something else it ends the program
public void Directions(){
String choice;
System.out.println("Do you want the directions? Yes or no.");
choice=reader.nextLine();
if (choice.equalsIgnoreCase("Yes") || choice.equalsIgnoreCase("Y")){
System.out.println("This program will print out a Sudoku puzzle and answer for you.\nIn the puzzle it will will have numbers placed in a 9 by 9 box.\nThere will be columns, rows and 9 3 by 3 boxes.\nYou can only have one copy of the numbers 1 through 9 in each column, row, and 3 by 3 box.\nThere will be boxes with the number 0 in them.\nThis is where you put a number that could possibly be there.\nThis program will aslo tell you the solution.");
}
else if(choice.equalsIgnoreCase("No")||choice.equalsIgnoreCase("N")){
}
else{
System.out.println("That was not an option. No Sudoku for you. Now ending program.");
System.exit(0);
}
}
//this program asks what difficulty you want
//choice 1 is easy and choice 2 is hard
//this uses a scanner and int to read that
//there is a while loop in there for if in case you pick something other than 1 or 2 it ends the program
public void Difficulty(){
boolean validInput=false;
System.out.println("Pick a difficulty.\n1. Easy\n2. Hard");
while(!validInput){
try{
x=reader.nextInt();
validInput=true;
if(x>3){
System.out.println("That was not an option. No Sudoku for you. Now ending program.");
System.exit(0);
}
else if(x<1){
System.out.println("That was not an option. No Sudoku for you. Now ending program.");
System.exit(0);
}
}
catch(InputMismatchException e){
System.out.println("That was not an option. No Sudoku for you. Now ending program.");
System.exit(0);
}
}
}
//this program sets up a nested loop that will print any 9by9 array sent to it
public void PrintArray(int k[][]) {
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
System.out.print(k[row][col] + "\t");
}
System.out.println(" ");
}
}
//this program makes the solution
//it does this by having two integers one for columns and one for rows
//it then calls a method which places all the numbers
//it does this by using a loop from 1-9 and sending it the number that the loop is using
//the it returns the solution
public int[][] Solution(int sol[][]) {
int col=0;
int row=0;
for(int num=1;num<10;num++){
NumberPlacer(num,sol);
}
return sol;
}
//this says if there is a number greater than zero do not put a number here
//this makes it so if a number was already placed somewhere it won't be overwritten
public boolean Forbidden(int row, int col,int sol[][]){
if(sol[row][col]>0){
return true;
}
return false;
}
//this method does most of the work
//it starts with box 5(the middle box) and puts a number there
//it then goes to the boxes on the left(box 4)and puts a number there while also making sure it is possible to put the number there
//after that it does the same for the box to the right(box 6), above(box 2), and below(box 7) of box 5
//it then does the four corner boxes, box 1, box 3, box 7, box 9
//it does this for all numbers sent which is one through 9
public void NumberPlacer(int num, int sol[][]){
int col=0;
int row=0;
//box5
while(true){
row=RowSet2(row);
col=ColSet2(col);
while(Forbidden(row,col,sol)){
row=RowSet2(row);
col=ColSet2(col);
}
sol[row][col]=num;
if(!CheckCol(col,row,num,sol)||!CheckRow(row,col,num,sol)){
sol[row][col]=0;
}
else if(CheckCol(col,row,num,sol)&&CheckRow(row,col,num,sol)){
break;
}
}
//box4
while(true){
row=RowSet2(row);
col=ColSet1(col);
while(Forbidden(row,col,sol)){
row=RowSet2(row);
col=ColSet1(col);
}
sol[row][col]=num;
if(!CheckRow(row,col,num,sol)){
sol[row][col]=0;
}
else if(CheckRow(row,col,num,sol)){
break;
}
}
//box6
while(true){
row=RowSet2(row);
col=ColSet3(col);
while(Forbidden(row,col,sol)){
row=RowSet2(row);
col=ColSet3(col);
}
sol[row][col]=num;
if(!CheckRow(row,col,num,sol)){
sol[row][col]=0;
}
else if(CheckRow(row,col,num,sol)){
break;
}
}
//box2
while(true){
row=RowSet1(row);
col=ColSet2(col);
while(Forbidden(row,col,sol)){
row=RowSet1(row);
col=ColSet2(col);
}
sol[row][col]=num;
if(!CheckCol(col,row,num,sol)){
sol[row][col]=0;
}
else if(CheckCol(col,row,num,sol)){
break;
}
}
//box8
while(true){
row=RowSet3(row);
col=ColSet2(col);
while(Forbidden(row,col,sol)){
row=RowSet3(row);
col=ColSet2(col);
}
sol[row][col]=num;
if(!CheckCol(col,row,num,sol)){
sol[row][col]=0;
}
else if(CheckCol(col,row,num,sol)){
break;
}
}
//box1
while(true){
row=RowSet1(row);
col=ColSet1(col);
while(Forbidden(row,col,sol)){
row=RowSet1(row);
col=ColSet1(col);
}
sol[row][col]=num;
if(!CheckCol(col,row,num,sol)||!CheckRow(row,col,num,sol)){
sol[row][col]=0;
}
else if(CheckCol(col,row,num,sol)&&CheckRow(row,col,num,sol)){
break;
}
}
//box3
while(true){
row=RowSet1(row);
col=ColSet3(col);
while(Forbidden(row,col,sol)){
row=RowSet1(row);
col=ColSet3(col);
}
sol[row][col]=num;
if(!CheckCol(col,row,num,sol)||!CheckRow(row,col,num,sol)){
sol[row][col]=0;
}
else if(CheckCol(col,row,num,sol)&&CheckRow(row,col,num,sol)){
break;
}
}
//box7
while(true){
row=RowSet3(row);
col=ColSet1(col);
while(Forbidden(row,col,sol)){
row=RowSet3(row);
col=ColSet1(col);
}
sol[row][col]=num;
if(!CheckCol(col,row,num,sol)||!CheckRow(row,col,num,sol)){
sol[row][col]=0;
}
else if(CheckCol(col,row,num,sol)&&CheckRow(row,col,num,sol)){
break;
}
}
//box9
while(true){
row=RowSet3(row);
col=ColSet3(col);
while(Forbidden(row,col,sol)){
row=RowSet3(row);
col=ColSet3(col);
}
sol[row][col]=num;
if(!CheckCol(col,row,num,sol)||!CheckRow(row,col,num,sol)){
sol[row][col]=0;
}
else if(CheckCol(col,row,num,sol)&&CheckRow(row,col,num,sol)){
break;
}
}
}
//this is for the first row of boxes or rows 0-2
public int RowSet1(int row){
row=randomnum.nextInt(3);
return row;
}
//this is for the second row of boxes or rows 3-5
public int RowSet2(int row){
row=randomnum.nextInt(3)+3;
return row;
}
//this is for the third row of boxes or rows 6-8
public int RowSet3(int row){
row=randomnum.nextInt(3)+6;
return row;
}
//this is for the first cols of boxes or cols 0-2
public int ColSet1(int col){
col=randomnum.nextInt(3);
return col;
}
//this is for the second cols of boxes or cols 3-5
public int ColSet2(int col){
col=randomnum.nextInt(3)+3;
return col;
}
//this is for the first cols of boxes or cols 6-8
public int ColSet3(int col){
col=randomnum.nextInt(3)+6;
return col;
}
//this checks the row for the number it is sent and makes sure it is not in that row other than that spot the number is in
public boolean CheckRow(int row, int y, int num, int sol[][]) {
for (int col = 0; col < 9; col++) {
if((row==row)&&(col==y)){
continue;
}
if (sol[row][col] == num) {
return false;
}
}
return true;
}
//this checks the col for the number it is sent and makes sure it is not in that col other than that spot the number is in
public boolean CheckCol(int col,int x, int num, int sol[][]) {
for (int row = 0; row < 9; row++) {
if((col==col)&&(row==x)){
continue;
}
if (sol[row][col] == num) {
return false;
}
}
return true;
}
//this is to set two 9by9 arrays equal to each other
public int[][] Equalizer(int sol[][], int puzz[][]){
for(int row=0;row<9;row++){
for(int col=0;col<9;col++){
puzz[row][col]=sol[row][col];
}
}
return puzz;
}
//this creates the puzzle depending on what difficulty you chose
//it chooses random spots to set too 0
//these are still solvable
public int[][] PuzzleMaker(int puzz[][]){
int numberRemoved=0;
int row=0;
int col=0;
if(x==1){
//box1
while(numberRemoved<4){
row=RowSet1(row);
col=ColSet1(col);
while(puzz[row][col]==0){
row=RowSet1(row);
col=ColSet1(col);
}
puzz[row][col]=0;
numberRemoved++;
}
numberRemoved=0;
//box2
while(numberRemoved<4){
row=RowSet1(row);
col=ColSet2(col);
while(puzz[row][col]==0){
row=RowSet1(row);
col=ColSet2(col);
}
puzz[row][col]=0;
numberRemoved++;
}
numberRemoved=0;
//box3
while(numberRemoved<4){
row=RowSet1(row);
col=ColSet3(col);
while(puzz[row][col]==0){
row=RowSet1(row);
col=ColSet3(col);
}
puzz[row][col]=0;
numberRemoved++;
}
numberRemoved=0;
//box4
while(numberRemoved<4){
row=RowSet2(row);
col=ColSet1(col);
while(puzz[row][col]==0){
row=RowSet2(row);
col=ColSet1(col);
}
puzz[row][col]=0;
numberRemoved++;
}
numberRemoved=0;
//box5
while(numberRemoved<4){
row=RowSet2(row);
col=ColSet2(col);
while(puzz[row][col]==0){
row=RowSet2(row);
col=ColSet2(col);
}
puzz[row][col]=0;
numberRemoved++;
}
numberRemoved=0;
//box6
while(numberRemoved<4){
row=RowSet2(row);
col=ColSet3(col);
while(puzz[row][col]==0){
row=RowSet2(row);
col=ColSet3(col);
}
puzz[row][col]=0;
numberRemoved++;
}
numberRemoved=0;
//box7
while(numberRemoved<4){
row=RowSet3(row);
col=ColSet1(col);
while(puzz[row][col]==0){
row=RowSet3(row);
col=ColSet1(col);
}
puzz[row][col]=0;
numberRemoved++;
}
numberRemoved=0;
//box8
while(numberRemoved<4){
row=RowSet3(row);
col=ColSet2(col);
while(puzz[row][col]==0){
row=RowSet3(row);
col=ColSet2(col);
}
puzz[row][col]=0;
numberRemoved++;
}
numberRemoved=0;
//box9
while(numberRemoved<4){
row=RowSet3(row);
col=ColSet3(col);
while(puzz[row][col]==0){
row=RowSet3(row);
col=ColSet3(col);
}
puzz[row][col]=0;
numberRemoved++;
}
}
else if(x==2){
//box1
while(numberRemoved<5){
row=RowSet1(row);
col=ColSet1(col);
while(puzz[row][col]==0){
row=RowSet1(row);
col=ColSet1(col);
}
puzz[row][col]=0;
numberRemoved++;
}
numberRemoved=0;
//box2
while(numberRemoved<5){
row=RowSet1(row);
col=ColSet2(col);
while(puzz[row][col]==0){
row=RowSet1(row);
col=ColSet2(col);
}
puzz[row][col]=0;
numberRemoved++;
}
numberRemoved=0;
//box3
while(numberRemoved<5){
row=RowSet1(row);
col=ColSet3(col);
while(puzz[row][col]==0){
row=RowSet1(row);
col=ColSet3(col);
}
puzz[row][col]=0;
numberRemoved++;
}
numberRemoved=0;
//box4
while(numberRemoved<5){
row=RowSet2(row);
col=ColSet1(col);
while(puzz[row][col]==0){
row=RowSet2(row);
col=ColSet1(col);
}
puzz[row][col]=0;
numberRemoved++;
}
numberRemoved=0;
//box5
while(numberRemoved<5){
row=RowSet2(row);
col=ColSet2(col);
while(puzz[row][col]==0){
row=RowSet2(row);
col=ColSet2(col);
}
puzz[row][col]=0;
numberRemoved++;
}
numberRemoved=0;
//box6
while(numberRemoved<5){
row=RowSet2(row);
col=ColSet3(col);
while(puzz[row][col]==0){
row=RowSet2(row);
col=ColSet3(col);
}
puzz[row][col]=0;
numberRemoved++;
}
numberRemoved=0;
//box7
while(numberRemoved<5){
row=RowSet3(row);
col=ColSet1(col);
while(puzz[row][col]==0){
row=RowSet3(row);
col=ColSet1(col);
}
puzz[row][col]=0;
numberRemoved++;
}
numberRemoved=0;
//box8
while(numberRemoved<5){
row=RowSet3(row);
col=ColSet2(col);
while(puzz[row][col]==0){
row=RowSet3(row);
col=ColSet2(col);
}
puzz[row][col]=0;
numberRemoved++;
}
numberRemoved=0;
//box9
while(numberRemoved<5){
row=RowSet3(row);
col=ColSet3(col);
while(puzz[row][col]==0){
row=RowSet3(row);
col=ColSet3(col);
}
puzz[row][col]=0;
numberRemoved++;
}
}
return puzz;
}
}