java数独游戏完整版分享
本文实例为大家分享了java数独游戏的具体代码,供大家参考,具体内容如下
自己写的数独游戏,共9关,代码如下:
1、DoShudu类用于产生数独数组
importjava.util.Random;
publicclassDoShudu{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
int[][]cells=newshudu();
//cells=changeShu(cells,9);
for(intk=0;k<9;k++){
for(inti=0;i<9;i++){
System.out.print(cells[k][i]);
}
System.out.println();
}
}
publicstaticint[][]newshudu(){
int[][]cells=newint[][]{
{1,2,3,4,5,6,7,8,9},
{4,5,6,7,8,9,1,2,3},
{7,8,9,1,2,3,4,5,6},
{2,3,1,5,6,4,8,9,7},
{5,6,4,8,9,7,2,3,1},
{8,9,7,2,3,1,5,6,4},
{3,1,2,6,4,5,9,7,8},
{6,4,5,9,7,8,3,1,2},
{9,7,8,3,1,2,6,4,5}
};
intcountH=newRandom().nextInt(10);
for(intk=0;k=9)?(m+3-9):m+3;
for(intj=0;j<9;j++){
temp[j]=cells[m][j];
cells[m][j]=cells[n][j];
cells[n][j]=temp[j];
}
returncells;
}
publicstaticint[][]lineTolie(int[][]cells){//行与列互换
inttemp=0;
for(intj=0;j<9;j++){
for(intk=j+1;k<9;k++){
temp=cells[k][j];
cells[k][j]=cells[j][k];
cells[j][k]=temp;
}
}
returncells;
}
}
2、界面运行类
importjava.awt.Button;
importjava.awt.Color;
importjava.awt.FlowLayout;
importjava.awt.Font;
importjava.awt.GridLayout;
importjava.awt.Point;
importjava.awt.TextField;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
importjava.awt.event.MouseMotionAdapter;
importjava.awt.event.TextEvent;
importjava.awt.event.TextListener;
importjava.util.Random;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importcom.sun.awt.AWTUtilities;
publicclassSudokuextendsJFrame{
finalprivateTextField[][]txtGame;
staticintnum=20;//空白格数量
staticintguan=5;//关卡数量
staticintadd=5;//没关过后增加的空白格数量
publicstaticvoidmain(String[]args){
Sudokushudu=newSudoku();
}
publicSudoku(){//对JFrame进行布局初始以及监听设置
txtGame=newTextField[9][9];//建立81个TextField对象
DoShudushudu=newDoShudu();
int[][]cells=shudu.getShudu();//获取数独数组
finalJPaneljpl=newJPanel();//建立JPanel对象
finalintspaceNum=num;//spaceNum表示需要设置空白TextField的数量
jpl.setLayout(newGridLayout(9,9));//JPanel布局
finalint[][]cellAn=newint[9][9];//数独数组的答案
System.arraycopy(cells,0,cellAn,0,cells.length);//答案从建立的数独数组中Copy
for(inti=0;i<9;i++){//把答案从Console打印出来
for(intj=0;j<9;j++){
System.out.print(cellAn[i][j]);
}
System.out.println();
}//打印结束
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setSize(600,600);
this.setResizable(false);
this.setTitle("黑马-李德国-数独游戏9关");
for(inti=0;i<9;i++){
for(intj=0;j<9;j++){
txtGame[i][j]=newTextField();
//设置TextField背景颜色
if((i<3&&j<3)||(i<6&&i>=3&&j>=3&&j<6)
||(i<9&&i>=6&&j>=6&&j<9)){
txtGame[i][j].setBackground(Color.ORANGE);
}
if((i<6&&i>=3&&j<3)||(i<3&&j>=6&&j<9)
||(i<9&&i>=6&&j>=3&&j<6)){
txtGame[i][j].setBackground(Color.GREEN);
}
if((i<9&&i>=6&&j<3)||(i<3&&j>=3&&j<6)
||(i<6&&i>=3&&j<9&&j>=6)){
txtGame[i][j].setBackground(Color.PINK);
}
txtGame[i][j].setFont(newFont("Dialog",Font.CENTER_BASELINE,
60));//设置字体大小
txtGame[i][j].setText(Integer.toString(cells[i][j]));
txtGame[i][j].setEnabled(false);
txtGame[i][j].setVisible(true);
jpl.add(txtGame[i][j]);
jpl.setVisible(true);
}
}
finalint[][]tempArray=newint[spaceNum][2];
finalJFramejfm=newJFrame("选择数字");
//取消JFrametitle
jfm.setUndecorated(true);
//增加JFrame拖拽功能
finalPointorigin=newPoint();
jfm.addMouseListener(newMouseAdapter(){
publicvoidmousePressed(MouseEvente){
origin.x=e.getX();
origin.y=e.getY();
}
});
jfm.addMouseMotionListener(newMouseMotionAdapter(){
publicvoidmouseDragged(MouseEvente){
Pointp=jfm.getLocation();
jfm.setLocation(p.x+e.getX()-origin.x,p.y+e.getY()
-origin.y);
}
});
//设置JFrame为半透明
AWTUtilities.setWindowOpacity(jfm,0.7f);
finalJPaneljpnl=newJPanel(newGridLayout(3,3));
jfm.setLayout(null);
jfm.setSize(190,200);
jfm.setResizable(false);
jpnl.setBounds(0,0,190,120);
jfm.setResizable(false);
for(inti=0;i=3&&ranD2>=3&&ranD2<6)
||(ranD1<9&&i>=6&&ranD2>=6&&ranD2<9)){
txtGame[ranD1][ranD2].setBackground(Color.ORANGE);
}
if((ranD1<6&&ranD1>=3&&ranD2<3)
||(ranD1<3&&ranD2>=6&&ranD2<9)
||(ranD1<9&&ranD1>=6&&ranD2>=3&&ranD2<6)){
txtGame[ranD1][ranD2].setBackground(Color.GREEN);
}
if((ranD1<9&&ranD1>=6&&ranD2<3)
||(ranD1<3&&ranD2>=3&&ranD2<6)
||(ranD1<6&&ranD1>=3&&ranD2<9&&ranD2>=6)){
txtGame[ranD1][ranD2].setBackground(Color.PINK);
}
txtGame[ranD1][ranD2].addMouseListener(newMouseAdapter(){
publicvoidmouseClicked(MouseEventmouseevent){
jfm.getContentPane().removeAll();//移出了所有的组件
jpnl.removeAll();
for(intf=0;f<9;f++){
finalButtonbtn=newButton((f+1)+"");
btn.setForeground(Color.RED);
btn.setBackground(Color.WHITE);
btn
.setFont(newFont("Dialog",
Font.CENTER_BASELINE,30));
btn.addMouseListener(newMouseAdapter(){
@Override
publicvoidmouseClicked(MouseEvente){
//TODOAuto-generatedmethodstub
txtGame[ranD1][ranD2].setText(btn.getLabel()
+txtGame[ranD1][ranD2].getText());
}
});
jpnl.add(btn);
}
ButtonbtnDel=newButton("清空");
btnDel.setForeground(Color.WHITE);
btnDel.setBackground(Color.RED);
btnDel
.setFont(newFont("Dialog",Font.CENTER_BASELINE,
30));
btnDel.setBounds(0,120,190,50);
btnDel.addMouseListener(newMouseAdapter(){
@Override
publicvoidmouseClicked(MouseEvente){
//TODOAuto-generatedmethodstub
txtGame[ranD1][ranD2].setText("");
}
});
jfm.add(jpnl);
jfm.add(btnDel);
jfm.setVisible(true);
}
});
txtGame[ranD1][ranD2].addTextListener(newTextListener(){//对空白的TextField添加监听,数值发生变化后进行答案对比,如果全部答对在Console打印“good”
@Override
publicvoidtextValueChanged(TextEvente){
TextFieldtmp=(TextField)e.getSource();
intcount=0;
for(intu=0;u
更多有趣的经典小游戏实现专题,分享给大家:
C++经典小游戏汇总
python经典小游戏汇总
python俄罗斯方块游戏集合
JavaScript经典游戏玩不停
java经典小游戏汇总
javascript经典小游戏汇总
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。