java數(shù)獨(dú)游戲完整版分享
本文實(shí)例為大家分享了java數(shù)獨(dú)游戲的具體代碼,供大家參考,具體內(nèi)容如下
自己寫(xiě)的數(shù)獨(dú)游戲,共9關(guān),代碼如下:
1、DoShudu類用于產(chǎn)生數(shù)獨(dú)數(shù)組
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、界面運(yùn)行類
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;//空白格數(shù)量
static int guan=5;//關(guān)卡數(shù)量
static int add=5;//沒(méi)關(guān)過(guò)后增加的空白格數(shù)量
public static void main(String[] args) {
Sudoku shudu = new Sudoku();
}
public Sudoku() {// 對(duì)JFrame進(jìn)行布局初始以及監(jiān)聽(tīng)設(shè)置
txtGame = new TextField[9][9];// 建立81個(gè)TextField對(duì)象
DoShudu shudu = new DoShudu();
int[][] cells = shudu.getShudu();// 獲取數(shù)獨(dú)數(shù)組
final JPanel jpl = new JPanel();// 建立JPanel對(duì)象
final int spaceNum = num;// spaceNum表示需要設(shè)置空白TextField的數(shù)量
jpl.setLayout(new GridLayout(9, 9));// JPanel布局
final int[][] cellAn = new int[9][9];// 數(shù)獨(dú)數(shù)組的答案
System.arraycopy(cells, 0, cellAn, 0, cells.length);// 答案從建立的數(shù)獨(dú)數(shù)組中Copy
for (int i = 0; i < 9; i++) {// 把答案從Console打印出來(lái)
for (int j = 0; j < 9; j++) {
System.out.print(cellAn[i][j]);
}
System.out.println();
} // 打印結(jié)束
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setSize(600, 600);
this.setResizable(false);
this.setTitle("黑馬-李德國(guó)-數(shù)獨(dú)游戲 9關(guān)");
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
txtGame[i][j] = new TextField();
// 設(shè)置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));// 設(shè)置字體大小
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("選擇數(shù)字");
// 取消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);
}
});
// 設(shè)置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++) {// 依據(jù)需要空白的TextField數(shù)量,隨機(jī)對(duì)TextField設(shè)置為空
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() {// 對(duì)空白的TextField添加監(jiān)聽(tīng),數(shù)值發(fā)生變化后進(jìn)行答案對(duì)比,如果全部答對(duì)在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("恭喜你過(guò)關(guān)"));
Button btn = new Button("進(jìn)入下一關(guān)");
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("恭喜 你已經(jīng)完成所有關(guān)卡!"));
}
jpl.updateUI();
System.out.println("good");
}
}
});
txtGame[ranD1][ranD2].setEnabled(true);
}
this.add(jpl);
this.setVisible(true);
}
}
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java實(shí)現(xiàn)藍(lán)橋杯數(shù)獨(dú)游戲的示例代碼
- java回溯算法解數(shù)獨(dú)問(wèn)題
- python實(shí)現(xiàn)數(shù)獨(dú)游戲 java簡(jiǎn)單實(shí)現(xiàn)數(shù)獨(dú)游戲
- Java基于二維數(shù)組實(shí)現(xiàn)的數(shù)獨(dú)問(wèn)題示例
- java版數(shù)獨(dú)游戲界面實(shí)現(xiàn)(二)
- java版數(shù)獨(dú)游戲核心算法(一)
- 簡(jiǎn)單實(shí)現(xiàn)java數(shù)獨(dú)游戲
- Java實(shí)現(xiàn)解數(shù)獨(dú)的小程序
- java使用回溯法求解數(shù)獨(dú)示例
- Java實(shí)現(xiàn)數(shù)獨(dú)小游戲
相關(guān)文章
Java Http多次請(qǐng)求復(fù)用同一連接示例詳解
這篇文章主要為大家介紹了Java Http多次請(qǐng)求復(fù)用同一連接示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Java簡(jiǎn)單數(shù)據(jù)加密方法DES實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了Java簡(jiǎn)單數(shù)據(jù)加密方法DES實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
java中char對(duì)應(yīng)的ASCII碼的轉(zhuǎn)化操作
這篇文章主要介紹了java中char對(duì)應(yīng)的ASCII碼的轉(zhuǎn)化操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
Java中的ArrayList.trimToSize()方法詳解
這篇文章主要介紹了Java中的ArrayList.trimToSize()方法詳解,前幾天看了Java?ArrayList,沒(méi)有明白trimToSize()這個(gè)方法是什么意思,所以看了一下源碼并且debug一下自己的一個(gè)例子,明白了其中的含義,需要的朋友可以參考下2023-11-11
基于Java的MathML轉(zhuǎn)圖片的方法(示例代碼)
最近接到一個(gè)新需求mathML轉(zhuǎn)圖片怎么實(shí)現(xiàn)呢?剛開(kāi)始還真是蒙圈了,不知道怎么實(shí)現(xiàn),今天小編記錄一種基于Java的MathML轉(zhuǎn)圖片的方法,感興趣的朋友一起看看吧2021-06-06
SpringBoot啟動(dòng)器Starters使用及原理解析
這篇文章主要介紹了SpringBoot啟動(dòng)器Starters使用及原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04

