java数独游戏完整版分享
程序员文章站
2024-02-18 18:38:34
本文实例为大家分享了java数独游戏的具体代码,供大家参考,具体内容如下
自己写的数独游戏,共9关,代码如下:
1、doshudu类用于产生数独数组...
本文实例为大家分享了java数独游戏的具体代码,供大家参考,具体内容如下
自己写的数独游戏,共9关,代码如下:
1、doshudu类用于产生数独数组
import java.util.random; public class doshudu { /** * @param args */ public static void main(string[] args) { // todo auto-generated method stub int[][] cells=newshudu(); //cells=changeshu(cells,9); for(int k=0;k<9;k++){ for(int i=0;i<9;i++){ system.out.print(cells[k][i]); } system.out.println(); } } public static int[][] newshudu(){ int[][] cells=new int[][]{ {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} }; int counth=new random().nextint(10); for(int k=0;k<counth;k++){ cells=linetolie(cells); } int count=0; for(int k=0;k<12;k++){ count=new random().nextint(9); cells=changeline(cells,count); } int counth2=new random().nextint(10); for(int k=0;k<counth2;k++){ cells=linetolie(cells); } return cells; } public static int [][] changeline(int[][] cells,int m){//行与行交换 int n=m; int [] temp=new int[9]; n=((m+3)>=9)?(m+3-9):m+3; for(int j=0;j<9;j++){ temp[j]=cells[m][j]; cells[m][j]=cells[n][j]; cells[n][j]=temp[j]; } return cells; } public static int[][] linetolie(int[][] cells){//行与列互换 int temp=0; for(int j=0;j<9;j++){ for(int k=j+1;k<9;k++){ temp=cells[k][j]; cells[k][j]=cells[j][k]; cells[j][k]=temp; } } return cells; } }
2、界面运行类
import java.awt.button; import java.awt.color; import java.awt.flowlayout; import java.awt.font; import java.awt.gridlayout; import java.awt.point; import java.awt.textfield; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.event.mousemotionadapter; import java.awt.event.textevent; import java.awt.event.textlistener; import java.util.random; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import com.sun.awt.awtutilities; public class sudoku extends jframe { final private textfield[][] txtgame; static int num=20;//空白格数量 static int guan=5;//关卡数量 static int add=5;//没关过后增加的空白格数量 public static void main(string[] args) { sudoku shudu = new sudoku(); } public sudoku() {// 对jframe进行布局初始以及监听设置 txtgame = new textfield[9][9];// 建立81个textfield对象 doshudu shudu = new doshudu(); int[][] cells = shudu.getshudu();// 获取数独数组 final jpanel jpl = new jpanel();// 建立jpanel对象 final int spacenum = num;// spacenum表示需要设置空白textfield的数量 jpl.setlayout(new gridlayout(9, 9));// jpanel布局 final int[][] cellan = new int[9][9];// 数独数组的答案 system.arraycopy(cells, 0, cellan, 0, cells.length);// 答案从建立的数独数组中copy for (int i = 0; i < 9; i++) {// 把答案从console打印出来 for (int j = 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 (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { txtgame[i][j] = new textfield(); // 设置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(new font("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); } } final int[][] temparray = new int[spacenum][2]; final jframe jfm = new jframe("选择数字"); // 取消jframe title jfm.setundecorated(true); // 增加jframe拖拽功能 final point origin = new point(); jfm.addmouselistener(new mouseadapter() { public void mousepressed(mouseevent e) { origin.x = e.getx(); origin.y = e.gety(); } }); jfm.addmousemotionlistener(new mousemotionadapter() { public void mousedragged(mouseevent e) { point p = jfm.getlocation(); jfm.setlocation(p.x + e.getx() - origin.x, p.y + e.gety() - origin.y); } }); // 设置jframe为半透明 awtutilities.setwindowopacity(jfm, 0.7f); final jpanel jpnl = new jpanel(new gridlayout(3, 3)); jfm.setlayout(null); jfm.setsize(190, 200); jfm.setresizable(false); jpnl.setbounds(0, 0, 190, 120); jfm.setresizable(false); for (int i = 0; i < spacenum; i++) {// 依据需要空白的textfield数量,随机对textfield设置为空 final int rand1 = new random().nextint(9); final int rand2 = new random().nextint(9); temparray[i][0] = rand1; temparray[i][1] = rand2; txtgame[rand1][rand2].settext(""); if ((rand1 < 3 && rand2 < 3) || (rand1 < 6 && rand1 >= 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(new mouseadapter() { public void mouseclicked(mouseevent mouseevent) { jfm.getcontentpane().removeall();// 移出了所有的组件 jpnl.removeall(); for (int f = 0; f < 9; f++) { final button btn = new button((f + 1) + ""); btn.setforeground(color.red); btn.setbackground(color.white); btn .setfont(new font("dialog", font.center_baseline, 30)); btn.addmouselistener(new mouseadapter() { @override public void mouseclicked(mouseevent e) { // todo auto-generated method stub txtgame[rand1][rand2].settext(btn.getlabel() + txtgame[rand1][rand2].gettext()); } }); jpnl.add(btn); } button btndel = new button(" 清 空 "); btndel.setforeground(color.white); btndel.setbackground(color.red); btndel .setfont(new font("dialog", font.center_baseline, 30)); btndel.setbounds(0, 120, 190, 50); btndel.addmouselistener(new mouseadapter() { @override public void mouseclicked(mouseevent e) { // todo auto-generated method stub txtgame[rand1][rand2].settext(""); } }); jfm.add(jpnl); jfm.add(btndel); jfm.setvisible(true); } }); txtgame[rand1][rand2].addtextlistener(new textlistener() {// 对空白的textfield添加监听,数值发生变化后进行答案对比,如果全部答对在console打印“good” @override public void textvaluechanged(textevent e) { textfield tmp = (textfield) e.getsource(); int count = 0; for (int u = 0; u < spacenum; u++) { if ((txtgame[temparray[u][0]][temparray[u][1]] .gettext()) .equals(integer .tostring(cellan[temparray[u][0]][temparray[u][1]]))) { count++; } } if (count == spacenum) { jpl.removeall(); flowlayout blt = new flowlayout(); jpl.setlayout(blt); if(num<=3){ jpl.add(new jlabel("恭喜你过关")); button btn = new button("进入下一关"); btn.addmouselistener(new mouseadapter() { @override public void mouseclicked(mouseevent e) { // todo auto-generated method stub sudoku.this.dispose(); jfm.dispose(); num=num+add; new sudoku(); } }); jpl.add(btn); } else{ jpl.add(new jlabel("恭喜 你已经完成所有关卡!")); } jpl.updateui(); system.out.println("good"); } } }); txtgame[rand1][rand2].setenabled(true); } this.add(jpl); this.setvisible(true); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Android仿京东、天猫商品详情页
下一篇: MySQL切分查询用法分析