Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will write a program in any language that is capable of solving 6x6 Sudoku p

ID: 3732772 • Letter: Y

Question

You will write a program in any language that is capable of solving 6x6 Sudoku problems using the search technique I presecribed below Your program needs to be able to read in the data concerning the initial values that are specified for a given board. The format for this is up to you. You can go to several sites to get test data for known boards with a solution.

*****MUST BE A 6X6 BOARD!!!****** NOT A 9X9! I can not use this****

Arc consistency -

implement the algorithm using "arc consistency", to determine a solution via repeated applications of the binary inequality constraints to reduce each location's domain of possibilities given the domain constraints specified by the input. Keep track of the total number of constraints that you processed to eventually get to the unique solution. Print out this value and the solution.

Explanation / Answer

Java program for solving sudoku 6*6 game by using algorithm arc consistency.

Class 1:

import java.util.LinkedList;

public class PriorityQueue<T> {
   int max = 0;
   int priority = 0;
   LinkedList<PriorityNode<T>> queue = new LinkedList<PriorityNode<T>>();

   public PriorityQueue() {
   }

   public void enqueue(T node) {
       enqueue(node, priority++);
   }

   public void enqueue(T node, int val) {
       PriorityNode<T> n = new PriorityNode<T>(node, val);
       queue.add(n);

       if (val > this.max) {
           this.max = val;
       }
   }

   public T dequeue() {
       int min = this.max;
       PriorityNode<T> minNode = null;
       int index = 0;
       int minIndex = 0;

       for (PriorityNode<T> node : queue) {
           if (node.priority <= min) {
               minNode = node;
               min = node.priority;
               minIndex = index;
           }

           index++;
       }

       if (queue.size() == 0) {
           this.max = 0;
       }

       if (minNode != null) {
           queue.remove(minIndex);

           return minNode.elem;
       }

       return null;
   }

   public boolean hasElement(T checkNode) {
       for (PriorityNode<T> node : queue) {
           if (node.elem.equals(checkNode)) {
               return true;
           }
       }

       return false;
   }

   public void updatePriorityIfHigher(T updateNode, int val) {
       for (PriorityNode<T> node : queue) {
           if (node.elem.equals(updateNode)) {
               if (val < node.priority) {
                   node.priority = val;
               }
           }
       }
   }

   public int size()
   {
       return queue.size();
   }

   public class PriorityNode<T> {
       T elem;
       int priority = 0;

       public PriorityNode(T elem, int priority) {
           this.elem = elem;
           this.priority = priority;
       }
   }
}

Class 2:

import java.util.*;

public class SudokuPuzzle
{
   public int unitSide = 2;
   public int puzzleSide = 6;
   private Puzzleval[][] puzzle;

   private class Puzzleval {
       int val;
       LinkedList<Integer> domain;

       public Puzzleval(int num) {
           this.makeval(num, false);
       }

       public Puzzleval(int num, boolean entireDomain) {
           this.makeval(num, true);
       }

       public void makeval(int num, boolean entireDomain) {
           this.val = num;
           this.domain = new LinkedList<Integer>();

           if (entireDomain) {
               this.domain.add(num);
           } else {
               for (int i = 1; i <= puzzleSide; i++) {
                   this.domain.add(i);
               }
           }
       }

       public String toString() {
           return this.val + "";
       }
   }

   public SudokuPuzzle() {
       this.makePuzzle();
   }

   public Puzzleval nil() {
       return new Puzzleval(0);
   }

   public void makePuzzle() {
       Puzzleval[][] puz = {
           { nil(), nil(), nil(), nil(), nil(), nil()},
           { nil(), nil(), nil(), nil(), nil(), nil()},
           { nil(), nil(), nil(), nil(), nil(), nil()},
           { nil(), nil(), nil(), nil(), nil(), nil()},
           { nil(), nil(), nil(), nil(), nil(), nil()},
           { nil(), nil(), nil(), nil(), nil(), nil()}
          
       };
       this.puzzle = puz;
   }

   public SudokuPuzzle(int[][] puzzle) {
       this.makePuzzle();

       for (int r = 0; r < puzzle.length; r++) {
           for (int c = 0; c < puzzle.length; c++) {
               if (puzzle[r][c] != 0) {
                   this.puzzle[r][c] = new Puzzleval(puzzle[r][c], true);
               }
           }
       }
   }

   public void print() {
       System.out.println("|-------|-------|");

       for (int r = 0; r < this.puzzleSide; r++) {
           for (int c = 0; c < this.puzzleSide; c++) {
               String val = this.puzzle[r][c].toString();
               String piece = val.equals("0") ? "*" : val;

               if (c == 0) {
                   piece = "| " + piece;
               }
               else if (c == 2 || c == 5 || c == 8) {
                   piece += " |";
               }

               System.out.print(piece + " ");
           }

           System.out.println();

           if ((r + 1) % 3 == 0) {
               System.out.println("|-------|-------|");
           }
       }
   }

   public int get(int r2, int c2) {
       return this.puzzle[r2][c2].val;
   }

   public LinkedList<Integer> getDomain(int r2, int c2) {
       return this.puzzle[r2][c2].domain;
   }

   public void set(int r2, int c2, int val) {
       this.puzzle[r2][c2] = new Puzzleval(val);
   }

   public void unset(int r2, int c2) {
       this.puzzle[r2][c2] = nil();
   }

   public LinkedList<Integer> getDomainCopy(int r2, int c2) {
       LinkedList<Integer> domain = this.getDomain(r2, c2);
       LinkedList<Integer> domainCopy = new LinkedList<Integer>();

       for (int i = 0; i < domain.size(); i++) {
           domainCopy.add(domain.get(i));
       }

       return domainCopy;
   }

   public void setDomain(int r2, int c2, LinkedList<Integer> domain) {
       this.puzzle[r2][c2].domain = domain;
   }

   public void removeFromDomain(int r2, int c2, int val) {
       this.puzzle[r2][c2].domain.remove((Integer) val);
   }

   public void addToDomain(int r2, int c2, int val) {
       this.puzzle[r2][c2].domain.add((Integer) val);
   }

   public boolean inDomain(int r2, int c2, int val) {
       return this.puzzle[r2][c2].domain.contains((Integer) val);
   }

   public int getDomainLength(int r2, int c2) {
       return this.puzzle[r2][c2].domain.size();
   }

   public SudokuPuzzle getCopy()
   {
       SudokuPuzzle newPuzzle = new SudokuPuzzle();

       for (int r = 0; r < this.puzzleSide; r++) {
           for (int c = 0; c < this.puzzleSide; c++) {
               newPuzzle.set(r, c, this.get(r, c));
               newPuzzle.setDomain(r, c, this.getDomainCopy(r, c));
           }
       }

       return newPuzzle;
   }
}

Class 3:

import java.util.*;

public class SudokuPuzzleSolver
{
   private static class Index
   {
       int r1;
       int c1;

       public Index(int r1, int c1)
       {
           this.r1 = r1;
           this.c1 = c1;
       }

       public String toString()
       {
           return "[" + r1 + "," + c1 + "]";
       }
   }

   private static class Inference extends Index
   {
       int exclusion;

       public Inference(int r1, int c1, int exclusion) {
           super(r1, c1);
           this.exclusion = exclusion;
       }

       public boolean equals(Object otherObj) {
           Inference other = (Inference) otherObj;
           return (this.r1 == other.r1 && this.c1 == other.c1 && this.exclusion == other.exclusion);
       }

       public String toString() {
           return super.toString() + "-" + exclusion;
       }
   }

   static int numSteps = 0;
   static double start = 0.0;
   static double thinkingTime = 0.0;
   static double doingTime = 0.0;
   static enum Algorithm { DFS, FORWARD_CHECKING, ARC_CONSISTENCY, AllDifferent };
  
   //use arc consistency algorithm.
   static Algorithm algorithm = Algorithm.ARC_CONSISTENCY;

   public static void main(String ar[])
   {
       int[][] easyPuzzle = {
           { 0, 3, 0, 1, 8, 0},
           { 0, 9, 6, 0, 3, 0},
           { 0, 8, 0, 7, 0, 6},
           { 0, 1, 9, 0, 0, 3},
           { 0, 0, 7, 0, 0, 0},
           { 0, 0, 3, 5, 0, 0}
          
       };

       int[][] mediumPuzzle =
       {
           { 0, 2, 0, 8, 0, 0},
           { 6, 9, 7, 0, 0, 5},
           { 0, 0, 8, 4, 0, 0},
           { 1, 0, 0, 7, 0, 8},
           { 0, 3, 0, 0, 0, 0},
           { 0, 0, 2, 3, 0, 9}
          
       };

       int[][] hardPuzzle = {
           { 7, 0, 0, 9, 0, 4},
           { 9, 0, 8, 0, 1, 0},
           { 4, 2, 0, 0, 0, 5},
           { 0, 0, 7, 0, 0, 8},
           { 0, 0, 0, 0, 0, 0},
           { 0, 6, 9, 5, 0, 0}
       };

       int[][] veryHardPuzzle = {
           { 9, 0, 0, 0, 0, 0},
           { 2, 0, 0, 7, 0, 6},
           { 0, 4, 0, 0, 2, 5},
           { 0, 8, 9, 0, 0, 4},
           { 7, 0, 0, 0, 0, 0},
           { 5, 0, 1, 0, 0, 0}
          
       };

       int[][] emptyPuzzle = {
           { 0, 0, 0, 0, 0, 0},
           { 0, 0, 0, 0, 0, 0},
           { 0, 0, 0, 0, 0, 0},
           { 0, 0, 0, 0, 0, 0},
           { 0, 0, 0, 0, 0, 0},
           { 0, 0, 0, 0, 0, 0}
          
       };

       SudokuPuzzle puzzle = new SudokuPuzzle(veryHardPuzzle);
       SudokuPuzzle solution = backtrackingSearch(puzzle);

       if (solution == null) {
           System.out.println("No solution");
       } else {
           solution.print();
       }

       System.out.println();
      
   }

   public static void startThinking() {
       start = System.currentTimeMillis();
   }

   public static void startDoing() {
       startThinking();
   }

   public static void stopThinking() {
       if (start != 0) {
           double stop = System.currentTimeMillis();
           thinkingTime += (stop - start);
           start = 0;
       }
   }

   public static void stopDoing() {
       if (start != 0) {
           double stop = System.currentTimeMillis();
           doingTime += (stop - start);
           start = 0;
       }
   }

   public static SudokuPuzzle backtrackingSearch(SudokuPuzzle puzzle) {
       startDoing();
       SudokuPuzzle solution = backtrack(puzzle);
       stopDoing();

       return solution;
   }

   public static SudokuPuzzle backtrack(SudokuPuzzle puzzle) {
       if (isFilledIn(puzzle)) {
           return puzzle;
       }

       Index unassigned = selectUnAssignedVariable(puzzle);
       LinkedList<Integer> domainValues = orderDomainValues(unassigned, puzzle);

       for (int i = 0; i < domainValues.size(); i++) {
           LinkedList<Inference> inferences = null;
           int value = domainValues.get(i);
           puzzle.set(unassigned.r1, unassigned.c1, value);

           numSteps++;

           if (isGoodSoFar(puzzle)) {
               stopDoing();
               startThinking();
               inferences = getInferences(puzzle, unassigned, value);
               stopThinking();
               startDoing();

               if (inferences == null) {
                   continue;
               }

               stopDoing();
               startThinking();
               applyInferences(puzzle, inferences);
               stopThinking();
               startDoing();
               SudokuPuzzle result = backtrack(puzzle);

               if (result != null) {
                   return result;
               }
           }

           puzzle.unset(unassigned.r1, unassigned.c1);
           unapplyInferences(puzzle, inferences);
       }

       return null;
   }

   public static void unapplyInferences(SudokuPuzzle puzzle, LinkedList<Inference> inferences) {
       if (inferences == null || inferences.size() == 0) {
           return;
       }

       for (Inference inference : inferences) {
           if (!puzzle.inDomain(inference.r1, inference.c1, inference.exclusion)) {
               puzzle.addToDomain(inference.r1, inference.c1, inference.exclusion);
           }
       }
   }

   public static void applyInferences(SudokuPuzzle puzzle, LinkedList<Inference> inferences) {
       if (inferences == null || inferences.size() == 0) {
           return;
       }

       for (Inference inference : inferences) {
           puzzle.removeFromDomain(inference.r1, inference.c1, inference.exclusion);
       }
   }

   public static LinkedList<Inference> getInferences(SudokuPuzzle puzzle, Index assigned, int value) {
       LinkedList<Inference> inferences = new LinkedList<Inference>();

       if (algorithm == Algorithm.DFS) {
           return inferences;
       } else if (algorithm == Algorithm.FORWARD_CHECKING) {
           return forwardCheckInferences(puzzle, assigned, value);
       } else if (algorithm == Algorithm.ARC_CONSISTENCY) {
           if (AC3(puzzle)) {
               return inferences;
           }

           return null;
       } else if (algorithm == Algorithm.AllDifferent) {
           if (AllDifferent(puzzle, inferences)) {
               return inferences;
           }

           return null;
       }

       return null;
   }

   public static void applyInference(SudokuPuzzle puzzle, SudokuPuzzle inference) {
       for (int r = 0; r < puzzle.puzzleSide; r++) {
           for (int c = 0; c < puzzle.puzzleSide; c++) {
               puzzle.set(r, c, inference.get(r, c));
               puzzle.setDomain(r, c, inference.getDomain(r, c));
           }
       }
   }

   public static void prepareQueue(SudokuPuzzle puzzle, PriorityQueue<Index[]> arcs)
   {
       for (int assignedr1 = 0; assignedr1 < puzzle.puzzleSide; assignedr1++)
       {
           for (int assignedc1 = 0; assignedc1 < puzzle.puzzleSide; assignedc1++)
           {
               /* Queue squares in current unit */
               int startr1 = (assignedr1 / puzzle.unitSide) * puzzle.unitSide;
               int startc1 = (assignedc1 / puzzle.unitSide) * puzzle.unitSide;
               int endr1 = startr1 + puzzle.unitSide;
               int endc1 = startc1 + puzzle.unitSide;

               for (int r1 = startr1; r1 < endr1; r1++) {
                   for (int c1 = startc1; c1 < endc1; c1++) {
                       if (r1 != assignedr1 && c1 != assignedc1) {
                           arcs.enqueue(new Index[] { new Index(r1, c1), new Index(assignedr1, assignedc1) });
                       }
                   }
               }

               /* Queue squares in current r1 */
               for (int r1 = 0; r1 < puzzle.puzzleSide; r1++) {
                   if (r1 != assignedr1) {
                       arcs.enqueue(new Index[] { new Index(r1, assignedc1), new Index(assignedr1, assignedc1) });
                   }
               }

               /* Queue squares in current c1umn */
               for (int c1 = 0; c1 < puzzle.puzzleSide; c1++) {
                   if (c1 != assignedc1) {
                       arcs.enqueue(new Index[] { new Index(assignedr1, c1), new Index(assignedr1, assignedc1) });
                   }
               }
           }
       }
   }

   public static boolean AllDifferent(SudokuPuzzle puzzle, LinkedList<Inference> inferences) {
       boolean AllDifferent = true;

       for (char r1 = 'A'; r1 <= 'I'; r1++) {
           AllDifferent &= AllDifferent(puzzle, inferences, r1);
       }

       for (int c1 = 0; c1 < puzzle.puzzleSide; c1++) {
           AllDifferent &= AllDifferent(puzzle, inferences, c1);
       }

       for (int r1 = 0; r1 < puzzle.puzzleSide; r1 += puzzle.unitSide) {
           for (int c1 = 0; c1 < puzzle.puzzleSide; c1 += puzzle.unitSide) {
               AllDifferent &= AllDifferent(puzzle, inferences, r1, c1);
           }
       }

       return AllDifferent;
   }

   public static boolean AllDifferent(SudokuPuzzle puzzle, LinkedList<Inference> inferences, LinkedList<Index> variables, LinkedList<LinkedList<Integer>> domains) {
       boolean areSingleton = true;
       LinkedList<Inference> tempInferences = new LinkedList<Inference>();

       while (areSingleton) {
           areSingleton = false;

           for (int i = 0; i < variables.size(); i++) {
               Index index = variables.get(i);

               if (index == null) {
                   continue;
               }

               int value = puzzle.get(index.r1, index.c1);

               if (puzzle.getDomainLength(index.r1, index.c1) != 1) {
                   continue;
               }

               int domainValue = domains.get(i).get(0);

               areSingleton = true;
               variables.set(i, null);

               for (int j = 0; j < variables.size(); j++) {
                   domains.get(j).remove((Integer) domainValue);
                   Index var = variables.get(j);

                   if (var == null) {
                       continue;
                   }

                   tempInferences.add(new Inference(var.r1, var.c1, domainValue));

                   if (domains.get(j).size() == 0) {
                       return false;
                   }
               }
           }

           int numVars = 0;
           int numDomainValues = 0;
           LinkedList<Integer> domain = new LinkedList<Integer>();

           for (int i = 0; i < variables.size(); i++) {
               if (variables.get(i) == null) {
                   continue;
               }

               numVars++;

               for (Integer j : domains.get(i)) {
                   if (!domain.contains(j)) {
                       domain.add(j);
                       numDomainValues++;
                   }
               }
           }

           if (numVars > numDomainValues) {
               return false;
           }
       }

       for (Inference inference : tempInferences) {
           inferences.add(inference);
       }

       return true;
   }

   public static boolean AllDifferent(SudokuPuzzle puzzle, LinkedList<Inference> inferences, char r1Char) {
       int r1 = r1Char - 'A';
       LinkedList<Index> variables = new LinkedList<Index>();
       LinkedList<LinkedList<Integer>> domains = new LinkedList<LinkedList<Integer>>();

       for (int c1 = 0; c1 < puzzle.puzzleSide; c1++) {
           variables.add(new Index(r1, c1));
           domains.add(puzzle.getDomainCopy(r1, c1));
       }

       return AllDifferent(puzzle, inferences, variables, domains);
   }

   public static boolean AllDifferent(SudokuPuzzle puzzle, LinkedList<Inference> inferences, int c1) {
       LinkedList<Index> variables = new LinkedList<Index>();
       LinkedList<LinkedList<Integer>> domains = new LinkedList<LinkedList<Integer>>();

       for (int r1 = 0; r1 < puzzle.puzzleSide; r1++) {
           variables.add(new Index(r1, c1));
           domains.add(puzzle.getDomainCopy(r1, c1));
       }

       return AllDifferent(puzzle, inferences, variables, domains);
   }

   public static boolean AllDifferent(SudokuPuzzle puzzle, LinkedList<Inference> inferences, int r1, int c1) {
       LinkedList<Index> variables = new LinkedList<Index>();
       LinkedList<LinkedList<Integer>> domains = new LinkedList<LinkedList<Integer>>();

       for (int r = r1; r < r1 + puzzle.unitSide; r++) {
           for (int c = c1; c < c1 + puzzle.unitSide; c++) {
               variables.add(new Index(r, c));
               domains.add(puzzle.getDomainCopy(r, c));
           }
       }

       return AllDifferent(puzzle, inferences, variables, domains);
   }

   public static boolean AC3(SudokuPuzzle puzzle) {
       PriorityQueue<Index[]> arcs = new PriorityQueue<Index[]>();
       prepareQueue(puzzle, arcs);

       while (arcs.size() > 0) {
           Index[] edge = arcs.dequeue();

           if (!revise(puzzle, edge[0], edge[1])) {
               continue;
           }

           int assignedr1 = edge[0].r1;
           int assignedc1 = edge[0].c1;
           int otherr1 = edge[1].r1;
           int otherc1 = edge[1].c1;

           if (puzzle.getDomainLength(assignedr1, assignedc1) == 0) {
               return false;
           }

           /* Queue squares in current unit */
           int startr1 = (assignedr1 / puzzle.unitSide) * puzzle.unitSide;
           int startc1 = (assignedc1 / puzzle.unitSide) * puzzle.unitSide;
           int endr1 = startr1 + puzzle.unitSide;
           int endc1 = startc1 + puzzle.unitSide;

           for (int r1 = startr1; r1 < endr1; r1++) {
               for (int c1 = startc1; c1 < endc1; c1++) {
                   if (r1 != assignedr1 && c1 != assignedc1 && r1 != otherr1 && c1 != otherc1) {
                       arcs.enqueue(new Index[] { new Index(r1, c1), new Index(assignedr1, assignedc1) });
                   }
               }
           }

           /* Queue squares in current r1 */
           for (int r1 = 0; r1 < puzzle.puzzleSide; r1++) {
               if (r1 != assignedr1 && r1 != otherr1) {
                   arcs.enqueue(new Index[] { new Index(r1, assignedc1), new Index(assignedr1, assignedc1) });
               }
           }

           /* Queue squares in current c1umn */
           for (int c1 = 0; c1 < puzzle.puzzleSide; c1++) {
               if (c1 != assignedc1 && c1 != otherc1) {
                   arcs.enqueue(new Index[] { new Index(assignedr1, c1), new Index(assignedr1, assignedc1) });
               }
           }
       }

       return true;
   }

   public static boolean revise(SudokuPuzzle puzzle, Index i, Index j) {
       boolean revised = false;

       LinkedList<Integer> domainI = puzzle.getDomainCopy(i.r1, i.c1);
       LinkedList<Integer> domainJ = puzzle.getDomain(j.r1, j.c1);

       for (Integer x : domainI) {
           boolean satisfiable = false;

           for (Integer y : domainJ) {
               if ((int) y != (int) x) {
                   satisfiable = true;
               }
           }

           if (!satisfiable) {
               puzzle.removeFromDomain(i.r1, i.c1, x);
               revised = true;
           }
       }

       return revised;
   }

   public static LinkedList<Inference> forwardCheckInferences(SudokuPuzzle puzzle, Index assigned, int value) {
       LinkedList<Inference> inferences = new LinkedList<Inference>();

       /* Flush domain for current square */
       for (int i = 1; i <= 9; i++) {
           if (i != value) {
               addInference(puzzle, inferences, assigned.r1, assigned.c1, i);
           }
       }

       /* Flush domains in current unit */
       int startr1 = (assigned.r1 / puzzle.unitSide) * puzzle.unitSide;
       int startc1 = (assigned.c1 / puzzle.unitSide) * puzzle.unitSide;
       int endr1 = startr1 + puzzle.unitSide;
       int endc1 = startc1 + puzzle.unitSide;

       for (int r1 = startr1; r1 < endr1; r1++) {
           for (int c1 = startc1; c1 < endc1; c1++) {
               if (r1 == assigned.r1 || c1 == assigned.c1) {
                   continue;
               }

               if (!addInference(puzzle, inferences, r1, c1, value)) {
                   return null;
               }
           }
       }

       /* Flush domains in current r1 */
       for (int r = 0; r < puzzle.puzzleSide; r++) {
           if (r == assigned.r1) {
               continue;
           }

           if (!addInference(puzzle, inferences, r, assigned.c1, value)) {
               return null;
           }
       }

       /* Flush domains in current c1umn */
       for (int c = 0; c < puzzle.puzzleSide; c++) {
           if (c == assigned.c1) {
               continue;
           }

           if (!addInference(puzzle, inferences, assigned.r1, c, value)) {
               return null;
           }
       }

       return inferences;
   }

   public static boolean addInference(SudokuPuzzle puzzle, LinkedList<Inference> inferences, int r1, int c1, int value) {
       Inference newInference = new Inference(r1, c1, value);

       if (!inferences.contains(newInference) && puzzle.inDomain(r1, c1, value)) {
           if (puzzle.getDomainLength(r1, c1) == 1) {
               return false;
           }

           inferences.add(newInference);
       }

       return true;
   }

   public static LinkedList<Integer> orderDomainValues(Index var, SudokuPuzzle puzzle) {
       LinkedList<Integer> domain = puzzle.getDomain(var.r1, var.c1);

       return domain;
   }

   public static Index selectUnAssignedVariable(SudokuPuzzle puzzle) {
       for (int r1 = 0; r1 < puzzle.puzzleSide; r1++) {
           for (int c1 = 0; c1 < puzzle.puzzleSide; c1++) {
               if (puzzle.get(r1, c1) == 0) {
                   return new Index(r1, c1);
               }
           }
       }

       return null;
   }

   public static boolean isFilledIn(SudokuPuzzle puzzle) {
       for (int r1 = 0; r1 < puzzle.puzzleSide; r1++) {
           for (int c1 = 0; c1 < puzzle.puzzleSide; c1++) {
               if (puzzle.get(r1, c1) == 0) {
                   return false;
               }
           }
       }

       return true;
   }

   public static boolean isSolved(SudokuPuzzle puzzle) {
       return isFilledIn(puzzle) && isGoodSoFar(puzzle);
   }

   public static boolean isGoodSoFar(SudokuPuzzle puzzle) {
       /* Check each unit */
       for (int ur1 = 0; ur1 < puzzle.unitSide; ur1++) {
           for (int uc1 = 0; uc1 < puzzle.unitSide; uc1++) {
               int[] nums = new int[9];
               int startr1 = ur1 * puzzle.unitSide;
               int startc1 = uc1 * puzzle.unitSide;
               int endr1 = startr1 + puzzle.unitSide;
               int endc1 = startc1 + puzzle.unitSide;
               int at = 0;

               for (int r1 = startr1; r1 < endr1; r1++) {
                   for (int c1 = startc1; c1 < endc1; c1++) {
                       nums[at++] = puzzle.get(r1, c1);
                   }
               }

               Arrays.sort(nums);

               for (int i = 1; i < at; i++) {
                   if (nums[i] != 0 && nums[i - 1] != 0 && nums[i] == nums[i - 1]) {
                       return false;
                   }
               }
           }
       }

       /* Check each r1 */
       for (int r1 = 0; r1 < puzzle.puzzleSide; r1++) {
           int[] nums = new int[9];

           for (int c1 = 0; c1 < puzzle.puzzleSide; c1++)
           {
               nums[c1] = puzzle.get(r1, c1);
           }

           Arrays.sort(nums);

           for(int i=1;i<nums.length;i++)
           {
               if (nums[i] != 0 && nums[i - 1] != 0 && nums[i] == nums[i - 1]) {
                   return false;
               }
           }
       }

       /* Check each c1umn */
       for (int c1 = 0; c1 < puzzle.puzzleSide; c1++) {
           int[] nums = new int[6];

           for (int r1 = 0; r1 < puzzle.puzzleSide; r1++) {
               nums[r1] = puzzle.get(r1, c1);
           }

           Arrays.sort(nums);

           for (int i = 1; i < nums.length; i++) {
               if (nums[i] != 0 && nums[i - 1] != 0 && nums[i] == nums[i - 1])
               {
                   return false;
               }
           }
       }

       return true;
}

}

write and save all three clases in the same directory and run the java program:

Execution and output:

javac SudokuPuzzleSolver.java

java SudokuPuzzleSolver
|------- |------ -|
| 9 1 2 | 4 5 3 |
| 2 3 4 | 7 1 6 |
| 1 4 3 | 6 2 5 |
|-----------|----------|
| 3 8 9 | 1 6 4 |
| 7 2 6 | 5 3 1 |
| 5 6 1 | 3 4 2 |
|----------|---------|


  
  
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote