⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 game_shudu.java

📁 这是一个Applet数独游戏.有涂改以前数字的功能,玩起来很方便.它有自动填数功能,帮助你完成数独游戏
💻 JAVA
字号:
import java.applet.*;import java.awt.*;
import java.awt.event.*;import java.lang.*;
import javax.swing.JOptionPane;

public class Game_ShuDu extends Applet implements ActionListener
{
	BorderLayout border,border1;                    //全局变量
	GridLayout grid;
	Button button[][];
	Button button1,button2;
	String s;
	int m,n;
	Panel p,p1;
	int num1[]=new int[9];
	
	public void init()                      //设计布局
	{
		border=new BorderLayout();
		border1=new BorderLayout();
		grid=new GridLayout(10,9);
		button=new Button[10][9];
		button1=new Button("清除数字");
		button2=new Button("重新游戏");
        p=new Panel();
		p1=new Panel();
		setLayout(border);
		p.setLayout(grid);
		p1.setLayout(border1);
        Font f1=new Font("Times New Roman",Font.BOLD,30);
		Font f2=new Font("Courier",Font.BOLD,20);
		int i,j;
		for(i=0;i<9;i++)
		{
			for(j=0;j<9;j++)
			{
				button[i][j]=new Button();
				button[i][j].addActionListener(this);
				p.add(button[i][j]);
			}
		}
		for(j=0;j<9;j++)
		{
            String k=String.valueOf(j+1);
			button[9][j]=new Button(k);
			button[9][j].setForeground(Color.orange);
			button[9][j].setBackground(Color.black);
			button[9][j].setFont(f1);
			button[9][j].addActionListener(this);
			p.add(button[9][j]);
		}
		button1.setForeground(Color.white);
	    button1.setBackground(Color.darkGray);
		button1.setFont(f1);
		button1.addActionListener(this);
		button2.setForeground(Color.white);
	    button2.setBackground(Color.darkGray);
		button2.setFont(f1);
		button2.addActionListener(this);
		Label label=new Label("数独游戏",Label.CENTER);
		label.setForeground(Color.magenta);
	    label.setBackground(Color.yellow);
		label.setFont(f2);
		p1.add(label,"Center");p1.add(button1,"East");p1.add(button2,"West");
		add(p,"Center");add(p1,"North");
	}
	 
	public void actionPerformed(ActionEvent e)           //响应点击按扭事件
	{
		s="";
		int i,j;
		Font f1=new Font("Times New Roman",Font.BOLD,30);
		if(e.getSource()==button2)
			for(i=0;i<9;i++)
			for(j=0;j<9;j++)
			button[i][j].setLabel("");
			
		if(e.getSource()==button1)
			button[m][n].setLabel("");
		for(j=0;j<9;j++)                                  //若选填数字,则给刚才选的按扭赋值
		{
			if(e.getSource()==button[9][j])
			{  
				s=button[9][j].getLabel();
				button[m][n].setLabel(s);
				button[m][n].setForeground(Color.red);
			    button[m][n].setFont(f1);
				find();                                     //自动填数
				break;
			}
		}
		for(i=0;i<9;i++)                            //若选定一个要赋值的按扭,则记下这个按扭的位置,同时确定这个按扭的取值范围
		{
			for(j=0;j<9;j++)
			{
				if(e.getSource()==button[i][j])
				{
					m=i;n=j;
					check(m,n);
					int a,b;
					for(a=0;a<9;a++)
			            button[9][a].setEnabled(true);
					for(a=0;a<9;a++)                          //检测并设置不能选的按扭为不能控制
		            {
			             for(b=0;b<9;b++)
				             if(button[9][a].getLabel().equals(String.valueOf(num1[b])))
			                 {
				                 button[9][a].setEnabled(false);
					             break;
				             }
		            }
					     return;
				}
			}
		}
	}

    public void joinTwoArray(int A[],int B[])           //合并两个数组
	{
		int i,j,k,flag;
		for(k=0;k<9;k++)                        //如果A中的数全不为0,则表示A中有1~9的所有数,此时返回
			if(A[k]==0)
			    break;
		if(k==9)
			return;

        for(j=0;j<9;j++)                    
		{
			flag=0;                          //flag是B中某个非零数与A中所有非零数不同的标志
			if(B[j]==0)
				break;
			else
			{
				for(i=0;i<9;i++)
			    {
					if(A[i]==0)
						break;
					else
					{
						if(B[j]==A[i])
						    flag=1;
					}
			    }
			}
			if(flag==0)
				A[k++]=B[j];
		}
	}

	public void check(int k,int l)                       //求得不能填的数
    {   
		int i,j,u;
		u=0;
		for(i=0;i<9;i++)
			num1[i]=0;
		for(j=0;j<9;j++)                               //取得第k行的数
			if(!button[k][j].getLabel().equals(""))
			     num1[u++]=Integer.parseInt(button[k][j].getLabel());
        u=0;
		int num2[]=new int[9];
		for(i=0;i<9;i++)
			num2[i]=0;
		for(i=0;i<9;i++)                               //取得第l列的数
			if(!button[i][l].getLabel().equals(""))
			     num2[u++]=Integer.parseInt(button[i][l].getLabel());
        joinTwoArray(num1,num2);
		//取小宫的数
        for(i=0;i<9;i++)
			num2[i]=0;
		if(k%3==0)
		{
			if(l%3==0)
		    {       
					u=0;
					for(i=k;i<k+3;i++)
						for(j=l;j<l+3;j++)
				            if(!button[i][j].getLabel().equals(""))
								 num2[u++]=Integer.parseInt(button[i][j].getLabel());
			}
            if(l%3==1)
		    {       
					u=0;
					for(i=k;i<k+3;i++)
						for(j=l-1;j<l+2;j++)
				             if(!button[i][j].getLabel().equals(""))
								 num2[u++]=Integer.parseInt(button[i][j].getLabel());
			}
            if(l%3==2)
		    {       
					u=0;
					for(i=k;i<k+3;i++)
						for(j=l-2;j<l+1;j++) 
						     if(!button[i][j].getLabel().equals(""))
								 num2[u++]=Integer.parseInt(button[i][j].getLabel());
			}

		}
		else if(k%3==1)
		{
			if(l%3==0)
		    {       
					u=0;
					for(i=k-1;i<k+2;i++)
						for(j=l;j<l+3;j++)
				             if(!button[i][j].getLabel().equals(""))
								 num2[u++]=Integer.parseInt(button[i][j].getLabel());
			}
            if(l%3==1)
		    {       
					u=0;
					for(i=k-1;i<k+2;i++)
						for(j=l-1;j<l+2;j++)
				             if(!button[i][j].getLabel().equals(""))
								 num2[u++]=Integer.parseInt(button[i][j].getLabel());
			}
            if(l%3==2)
		    {       
					u=0;
					for(i=k-1;i<k+2;i++)
						for(j=l-2;j<l+1;j++)
				             if(!button[i][j].getLabel().equals(""))
								 num2[u++]=Integer.parseInt(button[i][j].getLabel());
			}            
		}
		else
		{
			if(l%3==0)
		    {       
					u=0;
					for(i=k-2;i<k+1;i++)
						for(j=l;j<l+3;j++)
				             if(!button[i][j].getLabel().equals(""))
								 num2[u++]=Integer.parseInt(button[i][j].getLabel());
			}
            if(l%3==1)
		    {       
					u=0;
					for(i=k-2;i<k+1;i++)
						for(j=l-1;j<l+2;j++)
				             if(!button[i][j].getLabel().equals(""))
								 num2[u++]=Integer.parseInt(button[i][j].getLabel());
			}
            if(l%3==2)
		    {       
					u=0;
					for(i=k-2;i<k+1;i++)
						for(j=l-2;j<l+1;j++)
				            if(!button[i][j].getLabel().equals(""))
								 num2[u++]=Integer.parseInt(button[i][j].getLabel());
			}
		}
		joinTwoArray(num1,num2);
	}
		
	public void find()                             //帮你填好只有一种选择的数
	{
		int i,j,k,d;                           
		String s1="";
		Font f1=new Font("Times New Roman",Font.BOLD,30);
		for(i=0;i<9;i++)
		{
			for(j=0;j<9;j++)
			{
				d=0;
				if(button[i][j].getLabel().equals(""))
				{
					check(i,j);
                    for(k=0;k<9;k++)
						if(num1[k]!=0)
						{
							d=d+1;
						}
					if(d==9)
					{
						JOptionPane.showMessageDialog(this,"你的方案是错的!","警告",JOptionPane.WARNING_MESSAGE);
					    return;
					}
                 
				}
			}
		}
		for(i=0;i<9;i++)
		{
			for(j=0;j<9;j++)
			{
				d=0;
				if(button[i][j].getLabel().equals(""))
				{
					check(i,j);
                    for(k=0;k<9;k++)
						if(num1[k]!=0)
						{
							d=d+1;
						}
					if(d==8)
					{
						int a,b,c;
						for(a=1;a<=9;a++)
						{   
							c=0;
							for(b=0;b<8;b++)
							{
								if(a==num1[b])
								   break;
								c=c+1;
							}
							if(c==8)
							{
								s1=String.valueOf(a);
                                break;
                            }
							
                        }

						button[i][j].setLabel(s1);
						button[i][j].setForeground(Color.blue);
			            button[i][j].setFont(f1);
						    find();
				        return;
					}
                 
				}
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -