I ave written code for sudoku
But is not working
package sudoku;
import java.awt.*;
import java.awt.Event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class SudokuGame extends JFrame {
public static final int grid_size=9;
public static int subgrid_size =3;
public static final int Cell_size=60;
public static final int canvas_width = Cell_size* grid_size;
public static final int canvas_height = Cell_size*grid_size;
public static final Color open_cell_bgcolor = Color.YELLOW;
public static final Color open_cell_Text_yes = new Color(0,255,0);
public static final Color open_cell_no = Color.RED;
public static final Color closed_cell_bgcolor= new Color(240,240, 240);
public static final Color closed_cell_Text = Color.BLACK;
public static final Font Font_Numbers = new Font("Monospaced",Font.BOLD , 20);
int row,col;
InputListener listner =new InputListener();
private JTextField [][] tf_cells = new JTextField[grid_size][grid_size];
int count;
JTextField emptyCells = new JTextField();
private int[][] puzzle =
{{5,3,4,6,7,8,9,1,2},
{6,7,2,1,9,5,3,4,8},
{1,9,8,3,4,2,5,6,7},
{8,5,9,7,6,1,4,2,3},
{4,2,6,8,5,3,7,9,1},
{7,1,3,9,2,4,8,5,6},
{9,6,1,5,3,7,2,8,4},
{2,8,7,4,1,9,6,3,5},
{3,4,5,2,8,6,1,7,9}};
private boolean masks[][] = {
{false,false,false,false,true,true,true,false,true},
{false,false,false,true,false,false,false,false,true},
{true,false,false,true,false,false,false,false,false},
{false,false,false,true,true,false,true,false,false},
{false,false,true,false,false,true,false,true,false},
{false,true,false,true,false,true,true,false,false},
{false,false,false,false,true,false,false,false,false},
{false,false,true,true,false,false,false,false,false},
{false,false,true,true,false,true,false,false,true}};
public SudokuGame()
{
Container cp = getContentPane();
cp.setLayout(new GridLayout(grid_size,grid_size));
for(int row = 0; row<grid_size;++row)
{
for(int col=0;col<grid_size;++col)
{
tf_cells[row][col] = new JTextField();
cp.add(tf_cells[row][col]);
if(masks[row][col])
{
tf_cells[row][col].setText("");
tf_cells[row][col].setEditable(true);
tf_cells[row][col].setBackground(open_cell_bgcolor);
}
else
{
tf_cells[row][col].setText(puzzle[row][col]+"");
tf_cells[row][col].setEditable(false);
tf_cells[row][col].setBackground(closed_cell_bgcolor);
tf_cells[row][col].setForeground(closed_cell_Text);
}
tf_cells[row][col].setHorizontalAlignment(JTextField.CENTER);
tf_cells[row][col].setFont(Font_Numbers);
}
}
cp.setPreferredSize(new Dimension(canvas_width,canvas_width));
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Sudoku");
setVisible(true);
}
private class InputListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
int Row_selected = -1;
int column_selected =-1;
JTextField source = (JTextField)e.getSource();
boolean found=false;
for( row=0;row<grid_size&&!found;++row)
{
for(col=0;col<grid_size&&!found;++col){
if(tf_cells[row][col]==source)
{
Row_selected = row;
column_selected= col;
found = false;
}
}
}
String x = tf_cells[Row_selected][column_selected].getText();
int y = Integer.parseInt(x);
if(y==puzzle[Row_selected][column_selected])
{
tf_cells[Row_selected][column_selected].setBackground(Color.GREEN);
}
else
{
tf_cells[Row_selected][column_selected].setBackground(Color.RED);
}
}
}
public static void main(String[] args) {
SudokuGame sg = new SudokuGame();
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace();
}
}
}
What I have tried:
I have created method findlocationEmptyv