Click here to Skip to main content
15,910,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 {
	//Name constant for game properties
public static final int grid_size=9;
public static int subgrid_size =3;

//constant for ui controls(sizes and ccontrol 
	public static final  int Cell_size=60;  //cell width
	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;
	//private int countFilled;
	//private boolean ColumnHasNumber[][] = new boolean[9][9];
	//private boolean RowHasNumber[][];
	//private boolean LineHasNumber[] = new boolean[][][];
	//private boolean Block3x3HasNumber[][][];
	InputListener listner =new InputListener();
	//game board contains 9*9 Jtextfields 
	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) {
		// TODO Auto-generated method stub
		
		SudokuGame sg = new SudokuGame();
		
		try
		{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			
		}
		catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		

	}

}


What I have tried:

I have created method findlocationEmptyv
Posted
Updated 22-Jul-17 0:49am
Comments
Patrice T 22-Jul-17 0:57am    
"But is not working" is not informative. Give details.
tusharkaushik 22-Jul-17 1:00am    
i mean s that if if i enter unique number it does not turn green color if correct otherwise red
Patrice T 22-Jul-17 1:09am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
tusharkaushik 22-Jul-17 3:28am    
yes
tusharkaushik 22-Jul-17 1:00am    
how it can e possible

So use the debugger to find out exactly what is happening: Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900