java数独程序Word文档格式.docx
- 文档编号:21238038
- 上传时间:2023-01-28
- 格式:DOCX
- 页数:14
- 大小:17.76KB
java数独程序Word文档格式.docx
《java数独程序Word文档格式.docx》由会员分享,可在线阅读,更多相关《java数独程序Word文档格式.docx(14页珍藏版)》请在冰豆网上搜索。
privateMenuItemitem_next=newMenuItem("
开局"
privateMenuItemitem_auther=newMenuItem("
作者"
privateMenuItemitem_exit=newMenuItem("
退出"
//分别创建答案提交重来开局作者退出按钮
privateJButtonbutton_sol=newJButton("
privateJButtonbutton_ans=newJButton("
privateJButtonbutton_rem=newJButton("
privateJButtonbutton_ext=newJButton("
privateJButtonbutton_aur=newJButton("
privateJButtonbutton_next=newJButton("
//创建具有默认数据模型的复选框组件box
privateJComboBoxbox=newJComboBox();
//创建静态整形二维数组data\ansdata\row\col和三维数组sq1
staticintdata[][]=newint[10][10];
staticintansdata[][]=newint[10][10];
staticintrow[][]=newint[10][10];
staticintcol[][]=newint[10][10];
staticintsql[][][]=newint[4][4][10];
//创建二维数组文本框组件text
staticJTextFieldtext[][]=newJTextField[10][10];
staticStringatext[][]=newString[10][10];
staticinthard=2;
staticintdatahard[]={2,3,5};
//不同的难度
classShuDuAnsextendsJFrame
{//shuDuAns继承JFrame
privateJTextFieldatext[][]=newJTextField[10][10];
publicShuDuAns()
{
super("
this.setSize(300,300);
//大小
this.setLocation(200,200);
//位置
this.setVisible(true);
//可见性
this.setResizable(false);
//重设大小
JPanelapanel_but=newJPanel(newFlowLayout(FlowLayout.LEFT));
//流程布局
JPanelapanel_txt=newJPanel(newGridLayout(3,3,2,2));
//栏格布局
JPanelapanel[]=newJPanel[10];
//创建面板数组apanel
for(inti=1;
i<
=9;
i++)//栏格布局算法
apanel[i]=newJPanel(newGridLayout(3,3));
//给面板数组apanel添加GridLayout布局管理器apanel_txt.add(apanel[i]);
//将面板数组apanel添加到有GridLayout布局管理器的面板apanel_txt中
intm=(i+2)/3*3-2;
intn=((i-1)%3+1)*3-2;
for(intj=m;
j<
=m+2;
j++)
for(intk=n;
k<
=n+2;
k++)
atext[j][k]=newJTextField(Integer.toString(data[j][k]));
//文本框
atext[j][k].setHorizontalAlignment(JTextField.CENTER);
//横向对齐
atext[j][k].setEditable(false);
//可编辑的
apanel[i].add(atext[j][k]);
//将二维数组文本框atext添加到数组面板apanel中
}
this.add(apanel_txt);
staticintDFS()
i++)
for(intj=1;
if(data[i][j]==0)
for(intk=1;
if(row[i][k]==0&
&
col[j][k]==0&
sql[(i+2)/3][(j+2)/3][k]==0)
data[i][j]=k;
row[i][k]=1;
col[j][k]=1;
sql[(i+2)/3][(j+2)/3][k]=1;
if(DFS()==1)
return1;
else
data[i][j]=0;
row[i][k]=0;
col[j][k]=0;
sql[(i+2)/3][(j+2)/3][k]=0;
if(k==9)
return0;
staticvoidset_data_zero()
for(inti=0;
for(intj=0;
staticvoidsetnum()
setzero();
set_data_zero();
intn=(int)(Math.random()*100)+1;
//产生随机的数字
intj=i*3-((i+2)/3*8-6);
data[i][j]=n%9+1;
intk=data[i][j];
DFS();
staticvoidsetzero()
for(intk=0;
col[i][k]=0;
staticvoidsettext()
{//hard代表难度
{
intn=(int)(Math.random()*100)+1;
if(n%hard==0)
text[i][j]=newJTextField(Integer.toString(data[i][j]));
text[i][j].setEditable(false);
text[i][j]=newJTextField();
text[i][j].setHorizontalAlignment(JTextField.CENTER);
atext[i][j]=text[i][j].getText();
staticintgettext()
{//判断输入的是否是数字
try
intk=Integer.parseInt(text[i][j].getText());
ansdata[i][j]=k;
catch(NumberFormatExceptionnfe)//捕获异常
JOptionPane.showMessageDialog(null,"
数据中包括非数字,请重新输入!
"
staticintans()
{//判断输入的数字是否重复
intk=ansdata[i][j];
if(k>
9||k<
1)
if(row[i][k]==1||col[j][k]==1||sql[(i+2)/3][(j+2)/3][k]==1)
publicShuDu()
数独游戏"
this.setSize(470,500);
//设置当前组件大小
this.setLocation(260,130);
//设置当前组件的新位置
//设置当前组件可见
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
/////////////////////////
//设置窗体不可以由用户调整大小
JPanelpanel_but=newJPanel(newFlowLayout(FlowLayout.LEFT));
//定义一个面板对象panel_but并为该对象配制了FlowLayout布局管理器
JPanelpanel_txt=newJPanel(newGridLayout(3,3,2,2));
//定义一个面板对象panel_txt并为该对象配制了GridLayout布局管理器
JPanelpanel[]=newJPanel[10];
//定义了一个面板数组
{//suanfa算法,数独的算法
panel[i]=newJPanel(newGridLayout(3,3));
panel_txt.add(panel[i]);
panel[i].add(text[j][k]);
this.add(panel_but,"
South"
this.add(panel_txt);
menubar.add(menu_file);
//建立菜单栏
menubar.add(menu_edit);
menubar.add(menu_help);
menu_file.add(item_next);
menu_file.add(item_ans);
menu_file.add(item_exit);
menu_edit.add(item_sol);
menu_edit.add(item_rem);
menu_help.add(item_auther);
item_exit.addActionListener(this);
//为菜单项添加动作监听器
item_next.addActionListener(this);
item_ans.addActionListener(this);
item_sol.addActionListener(this);
item_auther.addActionListener(this);
item_rem.addActionListener(this);
this.setMenuBar(menubar);
//将当前对象的菜单栏设置为指定的menubar菜单栏
Objectpro[]={"
简单"
"
一般"
困难"
};
//选择游戏的难度,建立一个数组
box=newJComboBox(pro);
if(hard==datahard[0])
box.setSelectedIndex(0);
if(hard==datahard[1])
box.setSelectedIndex
(1);
if(hard==datahard[2])
box.setSelectedIndex
(2);
panel_but.add(box);
box.addItemListener(this);
panel_but.add(button_next);
//将按钮button_next添加到面板panel_but中
button_next.addActionListener(this);
//给按钮添加动作监听器
panel_but.add(button_ans);
//将按钮button_ans添加到面板panel_but中
button_ans.addActionListener(this);
//给按钮添加动作监听器
panel_but.add(button_sol);
//将按钮button_sol添加到面板panel_but中
button_sol.addActionListener(this);
panel_but.add(button_rem);
//将按钮button_rem添加到面板panel_but中
button_rem.addActionListener(this);
panel_but.add(button_ext);
//将按钮button_ext添加到面板panel_but中
button_ext.addActionListener(this);
panel_but.add(button_aur);
//将按钮button_aur添加到面板panel_but中
button_aur.addActionListener(this);
publicvoidactionPerformed(ActionEvente)
{//按钮点击方法动作事件
if(e.getSource()==item_auther||e.getSource()==button_aur)//作者
作者:
王洪晓、王美惠、路盟盟\n"
if(e.getSource()==item_exit||e.getSource()==button_ext)//退出
System.exit(0);
if(e.getSource()==item_sol||e.getSource()==button_sol)//提交
if(gettext()==1)
if(ans()==1)
答案正确,恭喜!
//展示信息
答案错误,请再接再厉!
if(e.getSource()==item_rem||e.getSource()==button_rem)
{//重来
text[i][j].setText(atext[i][j]);
if(e.getSource()==item_ans||e.getSource()==button_ans)
{//答案
newShuDuAns();
if(e.getSource()==item_next||e.getSource()==button_next)
{//开局
setnum();
settext();
this.setVisible(false);
newShuDu();
publicvoiditemStateChanged(ItemEvente)
{//项目的状态改变,选择游戏难度
if(box.getSelectedIndex()==0)
{//easy容易
hard=datahard[0];
if(box.getSelectedIndex()==1)
{//normal一般
hard=datahard[1];
if(box.getSelectedIndex()==2)
{//hard困难
hard=datahard[2];
publicstaticvoidmain(Stringargs[])
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 程序
![提示](https://static.bdocx.com/images/bang_tan.gif)