Create a Sudoku board GUI in Java. Next to the Sudoku board create 9 buttons fro
ID: 3679534 • Letter: C
Question
Create a Sudoku board GUI in Java. Next to the Sudoku board create 9 buttons from number 1 to 9 and a button called CHECK. Include few numbers in any square boards of the Sudoku. The user will then fill up the rest of square board that don’t have a number by clicking a numbered button. After that, the user will then click the button CHECK to see if there is no duplicates numbers in a row and column based on the Sudoku rule. If there is duplicate numbers then the color of the number will change to red. If there is no duplicate numbers then the color of the number remains in color black. Run the java code in Eclipse. The output must show a Sudoku board, #1 to #9 buttons, and the button "CHECK". Also, the output must show the numbers in color red if there is duplicate numbers in a row and column as the picture shown below(example).
2-8-7 3-1-6 4-5-9 1-4-6 2-9-5 8-3-7 9-3-5 4-7-8 2-6-1 8-5-2 1-3-4 7-9-6 7-9-4 6-5-2 3-1-8 4-2-8 9-6-3 1-7-5 5-6-1 8-4-7 9|2|3Explanation / Answer
Create a new Java Project in Eclipse. Copy these files into the src directory, then run
Rep.java
//created for graphical user interface
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Rep extends JFrame implements ActionListener {
Container con;
JButton b[][] = new JButton[9][9];
TextField t[] = new TextField[61];
JMenuBar mbar;
JMenu file, help, option, setting;
JMenuItem submit, exit, about, Reset;
int[][] cp = new int[9][9];
int[][] ip = new int[9][9];
Rep() {
super("Sudoku Game");
setSize(750,650);
con = getContentPane();
con.setLayout(new GridLayout(9, 9));
Mylogic ob1 = new Mylogic();
ob1.complet_puzzle();
ob1.puzzle();
int c = 0;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++) {
b[i][j] = new JButton("" + ip[i][j]);
b[i][j].setFont(new Font("ARIALBD", Font.BOLD, 20));
b[i][j].setForeground(Color.BLUE);
if (ip[i][j] == 0) {
// b[i][j]=new JButton("");
b[i][j].setText("");
b[i][j].setBackground(Color.WHITE);
b[i][j].addActionListener(this);
}
con.add(b[i][j]);
if (i == 3 || i == 4 || i == 5 || j == 3 || j == 4 || j == 5) {
if (2 < i && i < 6 && 2 < j && j < 6) {
b[i][j].setBackground(Color.CYAN);
continue;
}
b[i][j].setBackground(Color.pink);
}
else
b[i][j].setBackground(Color.CYAN);
}
mbar = new JMenuBar();
setJMenuBar(mbar);
file = new JMenu("File");
option = new JMenu("Option");
setting = new JMenu("Setting");
help = new JMenu("Help");
about = new JMenuItem("About");
Reset = new JMenuItem("reset");
submit = new JMenuItem("Submit");
exit = new JMenuItem("Exit");
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int r = 0;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if (cp[i][j] != Integer.parseInt(b[i][j].getText())) {
r = 1;
break;
}
for (int i = 0; i < 9; i++) {
System.out.println();
for (int j = 0; j < 9; j++) {
System.out.print(cp[i][j]);
System.out.print(Integer.parseInt(b[i][j].getText()) + " ");
}
}
System.out.print(" " + r);
if (r == 0)
JOptionPane.showMessageDialog(Rep.this, "You won the Game");
// System.out.println("You won the Game");
else
// System.out.println("You lose the Game");
JOptionPane.showMessageDialog(Rep.this, "You lose the Game");
}
});
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
about.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(Rep.this,
"FARSiM & HASiB JIgni Dost! SUST CSE 15batch PRODUCT....Our first project of java language ",
"About", JOptionPane.PLAIN_MESSAGE);
}
});
Reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// recall();
}
});
file.add(Reset);
file.add(submit);
file.addSeparator();
file.add(exit);
mbar.add(file);
mbar.add(option);
mbar.add(setting);
mbar.add(help);
mbar.add(about);
show();
// ob1.complet_puzzle();
MyWindowAdapter mwa = new MyWindowAdapter();
addWindowListener(mwa);
}
class Mylogic extends Logic {
void complet_puzzle() {
cp = save();
/*
* for(int i=0;i<9;i++) { for(int j=0;j<9;j++) System.out.print(cp[i][j]+" "); System.out.println(); }
*/
}
void puzzle() {
ip = hide();
/*
* System.out.print(" "+"nhiding puzzle : "); for(int i=0;i<9;i++) { for(int j=0;j<9;j++)
* System.out.print(ip[i][j]+" "); System.out.println(); }
*/
}
}
class MyWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++) {
if (e.getSource() == b[i][j]) {
String s = JOptionPane.showInputDialog("enter your number");
int c = Integer.parseInt(s);
if (0 < c && 10 > c) {
b[i][j].setText(s);
// b[i][j].setBackground(Color.GRAY);
b[i][j].setFont(new Font("ARIALBD", Font.BOLD, 25));
b[i][j].setForeground(Color.RED);
}
break;
}
}
}
void recall() {
Rep rs = new Rep();
}
}
Logic.java
//created for main logic
import java.util.*;//for random number
class Logic {
int[][] blockS = { { 4, 3, 5, 8, 7, 6, 1, 2, 9 }, { 8, 7, 6, 2, 1, 9, 3, 4, 5 },
{ 2, 1, 9, 4, 3, 5, 7, 8, 6 }, { 5, 2, 3, 6, 4, 7, 8, 9, 1 }, { 9, 8, 1, 5, 2, 3, 4, 6, 7 },
{ 6, 4, 7, 9, 8, 1, 2, 5, 3 }, { 7, 5, 4, 1, 6, 8, 9, 3, 2 }, { 3, 9, 2, 7, 5, 4, 6, 1, 8 },
{ 1, 6, 8, 3, 9, 2, 5, 7, 4 } };
Random R_num = new Random(); // random numbers to exchange rows
Random Grid_R_num = new Random();// random numbers to exchange GRIDS
Random R_exnum = new Random();
Random H_Rnum = new Random();
int firstrow, secondrow, firstcol, secondcol, firstgrid, secondgrid, gc = 0;
int carry[] = new int[9];
int blockh[][] = new int[9][9];
int blockc[][] = new int[9][9];
int[][] generate() { // //switching the rows
// randomly choosing one of the tow rows to be changed
int x = 10 + R_num.nextInt(10);
for (int y = 0; y < x; y++) {
for (int a = 0; a < 3; a++) {
// System.out.println("a="+a);
if (a == 0) {
firstrow = R_num.nextInt(3);
secondrow = R_num.nextInt(3);
}
else if (a == 1) {
firstrow = 3 + R_num.nextInt(3);
secondrow = 3 + R_num.nextInt(3);
}
else if (a == 2) {
firstrow = 6 + R_num.nextInt(3);
secondrow = 6 + R_num.nextInt(3);
}
for (int i = 0; i < 9; i++) {
carry[i] = blockS[firstrow][i];
blockS[firstrow][i] = blockS[secondrow][i];
blockS[secondrow][i] = carry[i];
}
}
for (int a = 0; a < 3; a++) {
// System.out.println("a="+a);
if (a == 0) {
firstcol = R_num.nextInt(3);
secondcol = R_num.nextInt(3);
}
else if (a == 1) {
firstcol = 3 + R_num.nextInt(3);
secondcol = 3 + R_num.nextInt(3);
}
else if (a == 2) {
firstcol = 6 + R_num.nextInt(3);
secondcol = 6 + R_num.nextInt(3);
}
for (int i = 0; i < 9; i++) {
carry[i] = blockS[i][firstcol];
blockS[i][firstcol] = blockS[i][secondcol];
blockS[i][secondcol] = carry[i];
}
}
}
firstgrid = 1 + Grid_R_num.nextInt(3);
secondgrid = 1 + Grid_R_num.nextInt(3);
if ((firstgrid == 1 && secondgrid == 2) || (firstgrid == 2 && secondgrid == 1)) {
for (int i = 0; i < 3; i++)
for (int j = 0; j < 9; j++) {
carry[j] = blockS[i][j];
blockS[i][j] = blockS[i + 3][j];
blockS[i + 3][j] = carry[j];
}
} else if ((firstgrid == 2 && secondgrid == 3) || (firstgrid == 3 && secondgrid == 2)) {
for (int i = 3; i < 6; i++)
for (int j = 0; j < 9; j++) {
carry[j] = blockS[i][j];
blockS[i][j] = blockS[i + 3][j];
blockS[i + 3][j] = carry[j];
}
} else if ((firstgrid == 1 && secondgrid == 3) || (firstgrid == 3 && secondgrid == 1)) {
for (int i = 0; i < 3; i++)
for (int j = 0; j < 9; j++) {
carry[j] = blockS[i][j];
blockS[i][j] = blockS[i + 6][j];
blockS[i + 6][j] = carry[j];
}
}
// Swithing complete of tow grids
// suffling the puzzle
int firstnum, secondnum, shuffle;
shuffle = 3 + R_num.nextInt(6);
// System.out.println("Shufflenum="+shuffle);
for (int k = 0; k < shuffle; k++) {
firstnum = 1 + R_exnum.nextInt(9);
secondnum = 1 + R_exnum.nextInt(9);
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++) {
if (blockS[i][j] == firstnum) {
blockS[i][j] = secondnum;
continue;
}
if (blockS[i][j] == secondnum) blockS[i][j] = firstnum;
}
}
return blockS;
}
// will save the complet puzzle
int[][] save() {
if (gc == 0) blockc = generate();
gc = 1;
return blockc;
}
// will hide number of puzzle
int[][] hide() {
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
blockh[i][j] = blockc[i][j];
int row, column, hidingnum;
hidingnum = 50 + R_num.nextInt(10);
for (int i = 0; i < hidingnum; i++) {
row = H_Rnum.nextInt(9);
column = H_Rnum.nextInt(9);
// System.out.println(" row,col = ("+row+","+column+")");
blockh[row][column] = 0;
}
return blockh;
}
}
Main.java
//Main Code
public class Main {
public static void main(String args[]) {
Rep ob = new Rep();
}
}