I am developing sudoku. Random numbers are generating but i want to place rthem into particular fields. here is my code
package sudoku;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.JOptionPane;
import javax.swing.undo.StateEdit;
public class SudokuDisplay
{
private static final Dimension FIELD_SIZE = new Dimension(60, 60);
private static final int EMPTY = 0;
private JPanel GridPanel = new JPanel();
private JPanel[][] subGridPanel = new JPanel[3][3];
private JTextField[][] cell = new JTextField[9][9];
private int randomInt;
int[] randomArray;
private String hint;
private int cellVal;
StateEdit edit;
public SudokuDisplay()
{
JToolBar toolBar = new JToolBar();
GridPanel.setBorder(BorderFactory.createLineBorder(Color.black, 3));
GridPanel.setLayout(new GridLayout(3, 3));
for (int i = 0; i < subGridPanel.length; i++)
{
for (int j = 0; j < subGridPanel[i].length; j++)
{
subGridPanel[i][j] = new JPanel(new GridLayout(3, 3));
subGridPanel[i][j].setBorder(
BorderFactory.createLineBorder(Color.black, 2));
GridPanel.add(subGridPanel[i][j]);
}
}
for (int i = 0; i < cell.length; i++)
{
for (int j = 0; j < cell[i].length; j++)
{
cell[i][j] = new JTextField();
cell[i][j].setPreferredSize(FIELD_SIZE);
cell[i][j].setFont( new Font("sans-serif", Font.BOLD, 20));
cell[i][j].setHorizontalAlignment(SwingConstants.CENTER);
subGridPanel[i/3][j/3].add(cell[i][j]);
}
}
}
public JComponent getPanel()
{
return GridPanel;
}
public int getHint()
{
int count =cell.length ;
int grid[][] = null;
for (int i = 0; i < cell.length; i++)
{
for (int j = 0; j < cell[i].length; j++)
{
//cellVal = Integer.parseInt(cell[i][j].getText());
if(grid[i][j]==EMPTY && count == 1)
hint = ("Only one value at row" + "(" + i + ")" + "and column" + "(" + j + ")");
else
hint = ("No hints available...");
}
}
int val = Integer.parseInt(hint);
return val;
}
public void clearBoard()
{
for (int r = 0; r < 9 ; r++ )
for (int c = 0; c < 9 ; c++)
cell[r][c].setText(" ");
}
public int getCell()
{
try
{
for (int i = 0; i < cell.length; i++)
for (int j = 0; j < cell[i].length; j++)
cellVal = Integer.parseInt(cell[i][j].getText());
}
catch (NumberFormatException e)
{
cellVal = EMPTY;
}
return cellVal;
}
public void setCell(int num, int row, int col)
{
Random generateRandomNum = new Random();
for (int index = 1; index <= 9; index++)
randomArray[index] = generateRandomNum.nextInt();
for (int m = 0; m <9; m++)
randomInt = randomArray[m];
for (int i = 0; i < cell.length; i++)
for (int j = 0; j < cell[i].length; j++)
cell[i][j].setText(String.valueOf(randomInt));
//cell[i][j].setText(String.valueOf(randomInt));
cell[row][col].addKeyListener(new KeyListener()
{
JTextField jtf;
public void keyTyped(KeyEvent e)
{
jtf = (JTextField) e.getComponent();
if(e.getKeyChar()=='N')
{
newGame();
}
if(e.getKeyChar()=='s')
{
//saveGame();
}
if(e.getKeyChar()=='L')
{
//solveSudoku();
}
if(e.getKeyChar()=='X')
{
System.exit(0);
}
if(e.getKeyChar()=='z')
{
//undo
}
if(e.getKeyChar()=='y')
{
//redo
}
if(e.getKeyChar()=='H')
{
//GetHint();
}
if(e.getKeyChar()=='A')
{
//
}
if(e.getKeyChar()>='1'||e.getKeyChar()<='9')
{
jtf.setForeground(Color.RED);
jtf.setBackground(Color.LIGHT_GRAY);
}
}
public void keyPressed(KeyEvent e) { }
public void keyReleased(KeyEvent e) { }
});
}
public boolean solveSudoku (int row, int col)
{
int bsteps = 0;
if(++row == 8)
return true;
if (cell[row][col] == null)
return(solveSudoku(row++, col++));
for(int num = 1; num <= 9; ++num)
{
if(isLegal(row, col, num))
{
cell[row][col].setText(String.valueOf(num));
bsteps++;
if(solveSudoku(row, col++))
return true;
}
}
return false;
}
public boolean isLegal (int row, int col, int num)
{
int puzzle[][] = null;
for (int i = 0; i < cell.length; i++)
for (int j = 0; j < cell[i].length; j++)
puzzle[i][j]=Integer.parseInt(cell[i][j].getText());
for(int i = 0; i < 9; i++)//checks for the same values row-wise
if (num == puzzle[i][col])
return false;
for(int j = 0; j < 9; j++)//checks for the same values column-wise
if (num == puzzle[row][j])
return false;
int boxR = (row/3) * 3;
int boxC = (col/3) * 3;
for(int m = 0; m < 3; m++)//checks 3x3 boxes
{
for (int n = 0; n < 3; n++)
{
if (num == puzzle[boxR + m][boxC + n])
return false;
}
}
return true; //no violations, so legal
}
public void showMsg(String msg)
{
JOptionPane.showMessageDialog(null,msg);
}
public void newGame()
{
for (int i = 0; i < cell.length; i++)
{
for (int j = 0; j < cell[i].length; j++)
{
cell[i][j].setText(" ");
cell[i][j].setEnabled(true);
}
}
//showStatus(“New Game Selected.”);
showMsg("New Game");
}
public void SaveGame()
{
for (int i = 0; i < cell.length; i++)
{
for (int j = 0; j < cell[i].length; j++)
{
cell[i][j].setText(" ");
cell[i][j].setEnabled(true);
}
}
//showStatus(“New Game Selected.”);
showMsg("New Game");
}
public void Undo()
{
edit.undo();
}
public void Redo()
{
edit.redo();
}
public void abtUs()
{
JTextArea txtmsg = new JTextArea();
String msg = "Title: Sudoku\n" + "Creator: Orwell IT Solution\n" +
"Version: 1.0";
txtmsg.setText(msg);
txtmsg.setAlignmentX(SwingConstants.CENTER);
txtmsg.setEditable(false);
}
}