最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

簡單實(shí)現(xiàn)java數(shù)獨(dú)游戲

 更新時間:2017年12月18日 11:43:14   作者:Wyx_  
這篇文章主要教大家如何簡單實(shí)現(xiàn)java數(shù)獨(dú)游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java數(shù)獨(dú)游戲的具體代碼,供大家參考,具體內(nèi)容如下

打算把javaFx需要的組件裝好以后直接用javaFx的,但似乎eclipse的版本不對,安裝了也不能用...
數(shù)獨(dú)代碼是在之前寒假受命寫的,學(xué)了一個月java的成果,現(xiàn)在看來有些不足但畢竟是第一個程序,就直接放上來,數(shù)獨(dú)終盤的實(shí)現(xiàn)直接用了暴力,時間復(fù)雜度有點(diǎn)高,懶得改了直接放代碼

終盤實(shí)現(xiàn):

import java.util.Random; 
 
public class SudokuPuzzleGenerator { 
 private Random random = new Random(); 
  
 private static final int MAX_CALL_RANDOM_ARRAY_TIMES = 220; 
  
 private int currentTimes = 0; 
 
 public int[][] generatePuzzleMatrix() { 
 
  int[][] randomMatrix = new int[9][9]; 
 
  for (int row = 0; row < 9; row++) { 
   if (row == 0) { 
    currentTimes = 0; 
    randomMatrix[row] = buildRandomArray(); 
 
   } else { 
    int[] tempRandomArray = buildRandomArray(); 
 
    for (int col = 0; col < 9; col++) { 
     if (currentTimes < MAX_CALL_RANDOM_ARRAY_TIMES) { 
      if (!isCandidateNmbFound(randomMatrix, tempRandomArray, row, col)) { 
        
       resetValuesInRowToZero(randomMatrix,row); 
       row -= 1; 
       col = 8; 
       tempRandomArray = buildRandomArray(); 
      } 
     } else {  
      row = -1; 
      col = 8; 
      resetValuesToZeros(randomMatrix); 
      currentTimes = 0; 
     } 
    } 
   } 
  } 
  return randomMatrix; 
 } 
  
 private void resetValuesInRowToZero(int[][] matrix, int row) 
 { 
  for (int j = 0; j < 9; j++) { 
   matrix[row][j] = 0; 
  } 
   
 } 
 
 private void resetValuesToZeros(int[][] matrix) { 
  for (int row = 0; row < 9; row++) { 
   for (int col = 0; col < 9; col++) { 
    matrix[row][col] = 0; 
   } 
  } 
 } 
 
 private boolean isCandidateNmbFound(int[][] randomMatrix, int[] randomArray, int row, int col) { 
  for (int i = 0; i < 9; i++) { 
   randomMatrix[row][col] = randomArray[i]; 
   if (noConflict(randomMatrix, row, col)) { 
    return true; 
   } 
  } 
  return false; 
 } 
 
 private boolean noConflict(int[][] candidateMatrix, int row, int col) { 
  return noConflictInRow(candidateMatrix, row, col)&&noConflictInColumn(candidateMatrix, row, col) && noConflictInBlock(candidateMatrix, row, col); 
 } 
 
 private boolean noConflictInRow(int[][] candidateMatrix, int row, int col) { 
   
  int currentValue = candidateMatrix[row][col]; 
 
  for (int colNum = 0; colNum < col; colNum++) { 
   if (currentValue == candidateMatrix[row][colNum]) { 
    return false; 
   } 
  } 
 
  return true; 
 } 
 
 private boolean noConflictInColumn(int[][] candidateMatrix, int row, int col) { 
 
  int currentValue = candidateMatrix[row][col]; 
 
  for (int rowNum = 0; rowNum < row; rowNum++) { 
   if (currentValue == candidateMatrix[rowNum][col]) { 
    return false; 
   } 
  } 
 
  return true; 
 } 
 
 private boolean noConflictInBlock(int[][] candidateMatrix, int row, int col) { 
 
  int baseRow = row / 3 * 3; 
  int baseCol = col / 3 * 3; 
 
  for (int rowNum = 0; rowNum < 8; rowNum++) { 
   if (candidateMatrix[baseRow + rowNum / 3][baseCol + rowNum % 3] == 0) { 
    continue; 
   } 
   for (int colNum = rowNum + 1; colNum < 9; colNum++) { 
    if (candidateMatrix[baseRow + rowNum / 3][baseCol + rowNum % 3] == candidateMatrix[baseRow 
      + colNum / 3][baseCol + colNum % 3]) { 
     return false; 
    } 
   } 
  } 
  return true; 
 
 }  
 private int[] buildRandomArray() { 
  currentTimes++; 
  int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 
  int randomInt = 0;  
  for (int i = 0; i < 20; i++) { 
   randomInt = random.nextInt(8) + 1; 
   int temp = array[0]; 
   array[0] = array[randomInt]; 
   array[randomInt] = temp; 
  } 
 
  return array; 
 } 
  
 public int getCurrentTimes() { 
  return currentTimes; 
 } 
  
 public void setCurrentTimes(int currentTimes) { 
  this.currentTimes = currentTimes; 
 } 
  
} 

界面及判斷:
用swing寫的

import javax.swing.*;  
import java.awt.*; 
import java.awt.event.*; 
import java.util.Random; 
 
public class ShuD extends JFrame{ 
 private static final long serialVersionUID = 5952689219411916553L; //序列化字段 
 private static JTextField a[][] = new JTextField[9][9];  //存儲文本框中的數(shù)字 
 static int ans[][] = new int[9][9];  //存儲輸入后的兩位數(shù)組 
 SudokuPuzzleGenerator example = new SudokuPuzzleGenerator(); 
 public int right[][] = example.generatePuzzleMatrix(); 
 public int rightans[][]; 
 private int[][] Wk(int a[][]){    //挖空 
  Random r = new Random(); 
  int a1, a2; 
  a1 = r.nextInt(9); 
  a2 = r.nextInt(9); 
  for(int i = 0; i < 100; i++) 
  { 
   a[a1][a2] = 0; 
   a1 = r.nextInt(9); 
   a2 = r.nextInt(9); 
  } 
  return a; 
 } 
 public ShuD(){ 
  Container c = getContentPane(); 
  c.setLayout(new BorderLayout(2, 1));  //邊框布局 
  JMenuItem jmiOk = new JMenuItem("提交");  //定義菜單 
  JMenuItem jmiExplain = new JMenuItem("詳情"); 
  JMenuItem jmiMessage = new JMenuItem("信息"); 
   
  JPanel panel = new JPanel();  //定義一個容器 
  panel.add(jmiOk);     //將菜單在容器內(nèi)顯示 
  panel.add(jmiExplain); 
  panel.add(jmiMessage); 
  JPanel p1 = new JPanel(new GridLayout(9, 9, 5, 5));  //定義9行9列的網(wǎng)格布局 
  add(panel,BorderLayout.NORTH);   //將菜單放置在北面 
  add(p1,BorderLayout.CENTER);   //將數(shù)字放置在正中間 
  rightans = Wk(right); 
  for(int k = 0;k<9; k ++) 
  { 
   for(int n=0;n<9;n++) 
   { 
    if(rightans[k][n] != 0) 
    { 
     a[k][n] = new JTextField("" + rightans[k][n]); 
     a[k][n].setHorizontalAlignment(JTextField.CENTER);//將數(shù)字水平居中 
     a[k][n].setEditable(false);   //只可顯示不可修改 
     p1.add(a[k][n]);     //添加文本框 
    } 
    else 
    { 
     a[k][n] = new JTextField();  
     a[k][n].setHorizontalAlignment(JTextField.CENTER); 
     p1.add(a[k][n]); 
    } 
   } 
  } 
  add(p1);   //將數(shù)字面板顯示在容器里 
  jmiOk.addActionListener(new ActionListener(){//匿名創(chuàng)建事件監(jiān)聽器 
   public void actionPerformed(ActionEvent e) 
   { 
    if(gettext() == 1) 
    { 
     if(judge() == true) 
     { 
      JOptionPane.showMessageDialog(null, "Your answer is right!","Result",JOptionPane.INFORMATION_MESSAGE); 
     } 
     else 
     { 
      JOptionPane.showMessageDialog(null, "Your answer is wrong!","Result",JOptionPane.INFORMATION_MESSAGE); 
     } 
    } 
   } 
  }); 
  explainListenerClass listener2 = new explainListenerClass(); 
  jmiExplain.addActionListener(listener2); 
  messageListenerClass listener3 = new messageListenerClass(); 
  jmiMessage.addActionListener(listener3); 
 } 
  
 static int gettext()   //獲取文本框的文字 
 { 
  int i,j; 
  for(i = 0; i < 9; i++) 
  { 
   for(j = 0; j < 9 ; j ++) 
   { 
    ans[i][j] = 0; 
   } 
  } 
  for(int k = 0;k < 9; k++) 
  { 
   for(int n = 0;n < 9; n++) 
   { 
    try   //異常處理 
    { 
     ans[k][n] = Integer.parseInt(a[k][n].getText());  
     //將答案類型轉(zhuǎn)換之后傳給ans 
    } 
    catch(NumberFormatException nfe) 
    { 
     JOptionPane.showMessageDialog(null,"數(shù)據(jù)中包括非數(shù)字,請重新輸入"); 
     return 0; 
    } 
   } 
  } 
  return 1; 
 } 
 public static boolean judge()   //判斷輸入的答案是否正確 
 { 
  int i,j,k; 
  int [][]answer = ans;    
   
  for(i = 0; i < 9; i ++) 
  { 
   if(judge9(answer[i]) == false)  //判斷每列是否有重復(fù)數(shù)字 
    return false; 
  } 
  for(j = 0; j < 9; j ++)     //判斷每行是否有重復(fù)數(shù)字 
  { 
    
   int[] newAnswerColumn = new int[9]; 
   for(i = 0; i < 9; i ++) 
   { 
    newAnswerColumn[i] = answer[i][j]; 
   } 
   if(judge9(newAnswerColumn) == false) 
    return false; 
  } 
  for(i = 0; i < 3; i ++)   //判斷每個小九宮格內(nèi)是否有重復(fù)數(shù)字 
  { 
   for(j = 0; j < 3; j ++) 
   { 
    k = 0; 
    int[] newAnswer = new int[9]; 
    for(int m = i * 3; m < i * 3 + 3; m ++) 
    { 
     for(int n = j * 3; n < j * 3 + 3; n ++) 
     { 
      newAnswer[k] = answer[m][n]; 
      k++; 
     } 
    } 
    if(judge9(newAnswer) == false) 
    { 
     return false; 
    }   
   } 
  } 
  return true; 
 } 
 public static boolean judge9(int[] answer) 
 { 
  int i,j; 
  for(i = 0; i < 9; i ++) 
  { 
   for(j = 0; j < 9; j ++) 
   { 
    if(i == j) 
     continue; 
    if(answer[i] == answer[j])  //如果有重復(fù)的數(shù)字,返回false 
    { 
     return false; 
    } 
   } 
  } 
  return true;  //沒有重復(fù)數(shù)字,返回true 
 } 
  
 public static void main(String[] args) { 
  JFrame frame = new ShuD(); 
  frame.setTitle("SuDoku"); 
  frame.setSize(600,900); 
  frame.setLocationRelativeTo(null);  
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  frame.setVisible(true); 
 } 
} 
class explainListenerClass implements ActionListener{  //事件監(jiān)聽器 
 public void actionPerformed(ActionEvent e){ 
  JOptionPane.showMessageDialog(null, "填入數(shù)字保證每行每列及每個小的九宮格內(nèi)數(shù)字無重復(fù)","Explain",JOptionPane.INFORMATION_MESSAGE); 
 } 
} 
class messageListenerClass implements ActionListener{ 
 public void actionPerformed(ActionEvent e){ 
  JOptionPane.showMessageDialog(null, "made by wyx","Message",JOptionPane.INFORMATION_MESSAGE); 
 } 
} 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • idea同步y(tǒng)api插件的詳細(xì)步驟

    idea同步y(tǒng)api插件的詳細(xì)步驟

    yapi是一個很好的接口文檔維護(hù)工具,其swagger功能,可將接口信息同步到y(tǒng)api平臺上,這篇文章主要介紹了idea同步y(tǒng)api插件,需要的朋友可以參考下
    2024-04-04
  • spring實(shí)現(xiàn)靜態(tài)注入(類或者屬性)操作示例

    spring實(shí)現(xiàn)靜態(tài)注入(類或者屬性)操作示例

    這篇文章主要為大家介紹了spring實(shí)現(xiàn)靜態(tài)注入(類或者屬性)操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • springboot自定義Starter過程解析

    springboot自定義Starter過程解析

    這篇文章主要介紹了springboot自定義Starter過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • IDEA創(chuàng)建自定義模板圖文教程

    IDEA創(chuàng)建自定義模板圖文教程

    我們每次在使用IntelliJ IDEA 時總會有一些文件是一直被創(chuàng)建的,今天我們就來學(xué)習(xí)一下IntelliJ IDEA 的自定義模板功能,文中有詳細(xì)的圖文介紹,需要的朋友可以參考下
    2021-05-05
  • Spring Boot 統(tǒng)一數(shù)據(jù)返回格式的解決方案

    Spring Boot 統(tǒng)一數(shù)據(jù)返回格式的解決方案

    統(tǒng)?的數(shù)據(jù)返回格式使? @ControllerAdvice 和 ResponseBodyAdvice 的?式實(shí)現(xiàn),下面給大家分享Spring Boot 統(tǒng)一數(shù)據(jù)返回格式的解決方案,感興趣的朋友一起看看吧
    2024-03-03
  • 淺談兩個jar包中包含完全相同的包名和類名的加載問題

    淺談兩個jar包中包含完全相同的包名和類名的加載問題

    下面小編就為大家?guī)硪黄獪\談兩個jar包中包含完全相同的包名和類名的加載問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Java并發(fā)之不可思議的死循環(huán)詳解

    Java并發(fā)之不可思議的死循環(huán)詳解

    下面小編就為大家?guī)硪黄狫ava并發(fā)之不可思議的死循環(huán)詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • java編程實(shí)現(xiàn)基于UDP協(xié)議傳輸數(shù)據(jù)的方法

    java編程實(shí)現(xiàn)基于UDP協(xié)議傳輸數(shù)據(jù)的方法

    這篇文章主要介紹了java編程實(shí)現(xiàn)基于UDP協(xié)議傳輸數(shù)據(jù)的方法,較為詳細(xì)的分析了UDP協(xié)議的原理及Java編程實(shí)現(xiàn)數(shù)據(jù)傳輸客戶端與服務(wù)器端的相關(guān)技巧,需要的朋友可以參考下
    2015-11-11
  • Mybatis動態(tài)SQL實(shí)例詳解

    Mybatis動態(tài)SQL實(shí)例詳解

    這篇文章主要給大家介紹了關(guān)于Mybatis動態(tài)SQL的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • java interface的兩個經(jīng)典用法

    java interface的兩個經(jīng)典用法

    這篇文章主要為大家詳細(xì)介紹了java interface的兩個經(jīng)典用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09

最新評論

仁寿县| 武山县| 个旧市| 呼玛县| 湄潭县| 杭锦旗| 聂荣县| 吉隆县| 松桃| 天长市| 东兰县| 新源县| 方正县| 菏泽市| 揭西县| 渝北区| 万年县| 潮安县| 土默特右旗| 辽宁省| 米脂县| 南召县| 宝丰县| 离岛区| 武功县| 扶余县| 龙里县| 神池县| 巴马| 银川市| 雅江县| 磴口县| 益阳市| 郓城县| 铜川市| 钦州市| 慈溪市| 林西县| 龙岩市| 平塘县| 宜君县|