|
| 1 | +import javax.swing.*; |
| 2 | +import java.awt.*; |
| 3 | +import java.awt.Font; |
| 4 | +import java.awt.event.*; |
| 5 | +import java.awt.event.ActionListener; |
| 6 | + |
| 7 | + |
| 8 | +public class ProjectSudoku extends JPanel{ |
| 9 | + |
| 10 | + //Declaration of all components. |
| 11 | + public int count; //For counting the number of backtracking |
| 12 | + public static final int SIZE = 9; //The size of grid |
| 13 | + |
| 14 | + public static int[][] get = { //Array of 9*9 & values for initialization of Sudoku |
| 15 | + {6,5,0,8,7,3,0,9,0}, |
| 16 | + {0,0,3,2,5,0,0,0,8}, |
| 17 | + {9,8,0,1,0,4,3,5,7}, |
| 18 | + {1,0,5,0,0,0,0,0,0}, |
| 19 | + {4,0,0,0,0,0,0,0,2}, |
| 20 | + {0,0,0,0,0,0,5,0,3}, |
| 21 | + {5,7,8,3,0,1,0,2,6}, |
| 22 | + {2,0,0,0,4,8,9,0,0}, |
| 23 | + {0,9,0,6,2,5,0,8,1} |
| 24 | + }; |
| 25 | + |
| 26 | + JTextField[][] textField; //For creating a 9*9 blocks |
| 27 | + JPanel panel; //For holding the blocks & button |
| 28 | + JButton button; |
| 29 | + Font font; //Setting the fonts |
| 30 | + |
| 31 | + public final void gui(int r, int c,String s){ //Giving color for textfields(blocks) |
| 32 | + int row_start = (r/3)*3; //Finding of starting position of row in 3*3 block |
| 33 | + int col_start = (c/3)*3; //Finding of starting position of column in 3*3 block |
| 34 | + for(int i=row_start;i<row_start+3;i++){ |
| 35 | + for(int j=col_start;j<col_start+3;j++){ |
| 36 | + if(s=="blue") |
| 37 | + textField[i][j].setBackground(Color.blue); |
| 38 | + if(s=="orange") |
| 39 | + textField[i][j].setBackground(Color.orange); |
| 40 | + if(s=="red") |
| 41 | + textField[i][j].setBackground(Color.red); |
| 42 | + if(s=="green") |
| 43 | + textField[i][j].setBackground(Color.green); |
| 44 | + if(s=="white") |
| 45 | + textField[i][j].setBackground(Color.white); |
| 46 | + if(s=="pink") |
| 47 | + textField[i][j].setBackground(Color.pink); |
| 48 | + if(s=="cyan") |
| 49 | + textField[i][j].setBackground(Color.cyan); |
| 50 | + if(s=="yellow"){ |
| 51 | + textField[i][j].setBackground(Color.black); |
| 52 | + textField[i][j].setForeground(Color.yellow); //Gives the font color |
| 53 | + } |
| 54 | + if(s=="lightGray") |
| 55 | + textField[i][j].setBackground(Color.lightGray); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + //Constructor |
| 61 | + ProjectSudoku() { |
| 62 | + JFrame frame = new JFrame("Sudoku Solver"); //Create a frame |
| 63 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Terminating the program |
| 64 | + font=new Font("SansSerif",Font.BOLD,40); |
| 65 | + textField = new JTextField[9][9]; //Create a array[9][9] to hold values |
| 66 | + button=new JButton("solve"); //Create a button |
| 67 | + panel = new JPanel (); //Create a panel |
| 68 | + panel.setPreferredSize(new Dimension(600,600)); //Setting the size of panel |
| 69 | + GridLayout grid =new GridLayout(10,9); //Creating a 10*9 grids(1 for button) |
| 70 | + panel.setLayout(grid); //Add the gridlayout |
| 71 | + frame.setContentPane(panel); //add panel to frame |
| 72 | + |
| 73 | + for(int i = 0; i < 9; i++) { |
| 74 | + for(int j = 0; j < 9; j++) { |
| 75 | + panel.add(textField[i][j]=new JTextField(5)); //Add array[9][9] to panel with size 5 |
| 76 | + if(get[i][j]==0){ |
| 77 | + textField[i][j].setHorizontalAlignment(textField[i][j].CENTER); |
| 78 | + //make values to be align center |
| 79 | + textField[i][j].setFont(font); //set the font |
| 80 | + continue; |
| 81 | + } |
| 82 | + else{ |
| 83 | + |
| 84 | + textField[i][j].setText(""+get[i][j]);//add values into text field |
| 85 | + textField[i][j].setHorizontalAlignment(textField[i][j].CENTER); |
| 86 | + //make values to be align center |
| 87 | + textField[i][j].setFont(font);//set the font |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + gui(1,1,"blue"); //Giving color to 3*3 block |
| 92 | + gui(1,3,"orange"); |
| 93 | + gui(1,6,"red"); |
| 94 | + gui(4,1,"green"); |
| 95 | + gui(4,3,"white"); |
| 96 | + gui(4,7,"pink"); |
| 97 | + gui(6,1,"cyan"); |
| 98 | + gui(6,3,"yellow"); |
| 99 | + gui(6,7,"lightGray"); |
| 100 | + |
| 101 | + panel.add(button); //Add solve button to panel |
| 102 | + //Onclick of button perform..... |
| 103 | + button.addActionListener(new ActionListener(){ |
| 104 | + @Override |
| 105 | + public void actionPerformed(ActionEvent ae){ |
| 106 | + for(int i = 0; i < 9; i++) { |
| 107 | + for(int j = 0; j < 9; j++) { |
| 108 | + if(textField[i][j].getText().equals("")){ |
| 109 | + get[i][j]=0; |
| 110 | + } |
| 111 | + else |
| 112 | + get[i][j]=Integer.parseInt(textField[i][j].getText()); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if (solveSudoku()) |
| 117 | + { |
| 118 | + System.out.println("Number of Backtrackings:"+count); |
| 119 | + printSudoku(); |
| 120 | + } |
| 121 | + else |
| 122 | + System.out.println("No solution"); |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + public boolean solveSudoku() |
| 127 | + { |
| 128 | + int row=0; |
| 129 | + int col=0; |
| 130 | + int[] a = FindEmptyLoction(row, col); |
| 131 | + //if there is no empty locations then the sudoku is already solved |
| 132 | + if(a[0] == 0) |
| 133 | + return true; |
| 134 | + row = a[1]; //get the position of empty row |
| 135 | + col = a[2]; //get the position of empty column |
| 136 | + for(int i=1;i<=SIZE;i++) //attempt to fill number between 1 to 9 |
| 137 | + { |
| 138 | + if(Numberissafe(i, row, col)) //Check the number i can be filled or not into empty location |
| 139 | + { |
| 140 | + get[row][col] = i; //Fill the number i |
| 141 | + //Backtracking |
| 142 | + if(solveSudoku()) |
| 143 | + return true; |
| 144 | + /* Know how backtracking works |
| 145 | + for(int k=0;k<SIZE;k++) 1 0 3 0 |
| 146 | + { 0 0 2 1 |
| 147 | + for(int j=0;j<SIZE;j++) 0 1 0 2 |
| 148 | + { 2 4 0 0 |
| 149 | + System.out.print(get[k][j]+"\t"); |
| 150 | + } |
| 151 | + System.out.println(""); |
| 152 | + }System.out.println("000000000"); |
| 153 | + */ |
| 154 | + count++; //For knowing the number of times backtracking happens |
| 155 | + |
| 156 | + get[row][col]=0; //Solution is wrong Reassign the value |
| 157 | + } |
| 158 | + } |
| 159 | + return false; |
| 160 | + } |
| 161 | + |
| 162 | + |
| 163 | + public int[] FindEmptyLoction(int row, int col) //Finding the empty location |
| 164 | + { |
| 165 | + int flag= 0; |
| 166 | + for(int i=0;i<SIZE;i++){ |
| 167 | + for(int j=0;j<SIZE;j++){ |
| 168 | + if(get[i][j] == 0) //Check cell is empty |
| 169 | + { |
| 170 | + row = i; //Changing the values of row and col |
| 171 | + col = j; |
| 172 | + flag = 1; |
| 173 | + int[] a = {flag, row, col}; |
| 174 | + return a; |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + int[] a = {flag, -1, -1}; |
| 179 | + return a; |
| 180 | + } |
| 181 | + |
| 182 | + |
| 183 | +public boolean Numberissafe(int n, int r, int c){ |
| 184 | + |
| 185 | + for(int i=0;i<SIZE;i++){ //Checking in col |
| 186 | + if(get[r][i] == n) //If there is a cell with value equal to i |
| 187 | + return false; |
| 188 | + } |
| 189 | + |
| 190 | + for(int i=0;i<SIZE;i++){ //Checking row |
| 191 | + if(get[i][c] == n) //If there is a cell with value equal to i |
| 192 | + return false; |
| 193 | + } |
| 194 | + |
| 195 | + |
| 196 | + int row_start = (r/3)*3; |
| 197 | + int col_start = (c/3)*3; |
| 198 | + for(int i=row_start;i<row_start+3;i++){ //Checking sub matrix |
| 199 | + for(int j=col_start;j<col_start+3;j++){ |
| 200 | + if(get[i][j]==n) |
| 201 | + return false; |
| 202 | + } |
| 203 | + } |
| 204 | + return true; |
| 205 | + } |
| 206 | + |
| 207 | + |
| 208 | + public void printSudoku(){ //Printing the Sudoku |
| 209 | + for(int i = 0; i < 9; i++) { |
| 210 | + for(int j = 0; j < 9; j++) { |
| 211 | + textField[i][j].setText(""+get[i][j]); //Print the values on to board |
| 212 | + textField[i][j].setHorizontalAlignment(textField[i][j].CENTER); |
| 213 | + textField[i][j].setFont(font); |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + }); //End of action performed |
| 218 | + |
| 219 | + frame.pack(); //To change the size Dynamically |
| 220 | + frame.setVisible(true); |
| 221 | + } |
| 222 | + public static void main(String[] args) { |
| 223 | + ProjectSudoku sample = new ProjectSudoku(); |
| 224 | + } |
| 225 | +} |
| 226 | + |
| 227 | + |
| 228 | + |
0 commit comments