簡單實現(xiàn)java數(shù)獨游戲-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)2013年至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站設(shè)計、成都做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元崇陽做網(wǎng)站,已為上家服務(wù),為崇陽各地企業(yè)和個人服務(wù),聯(lián)系電話:13518219792

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


終盤實現(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; 
 } 
  
} 

分享標(biāo)題:簡單實現(xiàn)java數(shù)獨游戲-創(chuàng)新互聯(lián)
網(wǎng)頁URL:http://bm7419.com/article28/hcpcp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、云服務(wù)器品牌網(wǎng)站設(shè)計、面包屑導(dǎo)航、搜索引擎優(yōu)化、標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化