欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

简单实现java数独游戏

程序员文章站 2023-12-13 09:42:22
本文实例为大家分享了java数独游戏的具体代码,供大家参考,具体内容如下 打算把javafx需要的组件装好以后直接用javafx的,但似乎eclipse的版本不对,安装了...

本文实例为大家分享了java数独游戏的具体代码,供大家参考,具体内容如下

打算把javafx需要的组件装好以后直接用javafx的,但似乎eclipse的版本不对,安装了也不能用...
数独代码是在之前寒假受命写的,学了一个月java的成果,现在看来有些不足但毕竟是第一个程序,就直接放上来,数独终盘的实现直接用了暴力,时间复杂度有点高,懒得改了直接放代码

终盘实现:

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];  //存储文本框中的数字 
 static int ans[][] = new int[9][9];  //存储输入后的两位数组 
 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);     //将菜单在容器内显示 
  panel.add(jmiexplain); 
  panel.add(jmimessage); 
  jpanel p1 = new jpanel(new gridlayout(9, 9, 5, 5));  //定义9行9列的网格布局 
  add(panel,borderlayout.north);   //将菜单放置在北面 
  add(p1,borderlayout.center);   //将数字放置在正中间 
  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);//将数字水平居中 
     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);   //将数字面板显示在容器里 
  jmiok.addactionlistener(new actionlistener(){//匿名创建事件监听器 
   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());  
     //将答案类型转换之后传给ans 
    } 
    catch(numberformatexception nfe) 
    { 
     joptionpane.showmessagedialog(null,"数据中包括非数字,请重新输入"); 
     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)  //判断每列是否有重复数字 
    return false; 
  } 
  for(j = 0; j < 9; j ++)     //判断每行是否有重复数字 
  { 
    
   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 ++)   //判断每个小九宫格内是否有重复数字 
  { 
   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])  //如果有重复的数字,返回false 
    { 
     return false; 
    } 
   } 
  } 
  return true;  //没有重复数字,返回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{  //事件监听器 
 public void actionperformed(actionevent e){ 
  joptionpane.showmessagedialog(null, "填入数字保证每行每列及每个小的九宫格内数字无重复","explain",joptionpane.information_message); 
 } 
} 
class messagelistenerclass implements actionlistener{ 
 public void actionperformed(actionevent e){ 
  joptionpane.showmessagedialog(null, "made by wyx","message",joptionpane.information_message); 
 } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: