数独谜题,即无响应 [英] Sudoku puzzle bug i.e not responding

查看:110
本文介绍了数独谜题,即无响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为数独写了代码

但是没有工作





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
		}
		

	}

}





我尝试过:



我创建了方法findlocationEmptyv



What I have tried:

I have created method findlocationEmptyv

推荐答案

因此,使用调试器确切地了解发生了什么:在函数的第一行放置一个断点,然后通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


抱歉,但我们不能为您做到这一点 - 时间让您学习一门新的(非常非常有用的)技能:调试!
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!


参见Java教程示例代码 [ ^ ]。


这篇关于数独谜题,即无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆