iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >简单实现java数独游戏
  • 381
分享到

简单实现java数独游戏

java数独游戏ava 2023-05-30 17:05:58 381人浏览 安东尼
摘要

本文实例为大家分享了java数独游戏的具体代码,供大家参考,具体内容如下打算把javaFx需要的组件装好以后直接用javaFx的,但似乎eclipse的版本不对,安装了也不能用...数独代码是在之前寒假受命写的,学了一个月java的成果,现

本文实例为大家分享了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;  }   } 

--结束END--

本文标题: 简单实现java数独游戏

本文链接: https://www.lsjlt.com/news/220022.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • c++中if elseif使用规则
    c++ 中 if-else if 语句的使用规则为:语法:if (条件1) { // 执行代码块 1} else if (条件 2) { // 执行代码块 2}// ...else ...
    99+
    2024-05-14
    c++
  • c++中的继承怎么写
    继承是一种允许类从现有类派生并访问其成员的强大机制。在 c++ 中,继承类型包括:单继承:一个子类从一个基类继承。多继承:一个子类从多个基类继承。层次继承:多个子类从同一个基类继承。多层...
    99+
    2024-05-14
    c++
  • c++中如何使用类和对象掌握目标
    在 c++ 中创建类和对象:使用 class 关键字定义类,包含数据成员和方法。使用对象名称和类名称创建对象。访问权限包括:公有、受保护和私有。数据成员是类的变量,每个对象拥有自己的副本...
    99+
    2024-05-14
    c++
  • c++中优先级是什么意思
    c++ 中的优先级规则:优先级高的操作符先执行,相同优先级的从左到右执行,括号可改变执行顺序。操作符优先级表包含从最高到最低的优先级列表,其中赋值运算符具有最低优先级。通过了解优先级,可...
    99+
    2024-05-14
    c++
  • c++中a+是什么意思
    c++ 中的 a+ 运算符表示自增运算符,用于将变量递增 1 并将结果存储在同一变量中。语法为 a++,用法包括循环和计数器。它可与后置递增运算符 ++a 交换使用,后者在表达式求值后递...
    99+
    2024-05-14
    c++
  • c++中a.b什么意思
    c++kquote>“a.b”表示对象“a”的成员“b”,用于访问对象成员,可用“对象名.成员名”的语法。它还可以用于访问嵌套成员,如“对象名.嵌套成员名.成员名”的语法。 c++...
    99+
    2024-05-14
    c++
  • C++ 并发编程库的优缺点
    c++++ 提供了多种并发编程库,满足不同场景下的需求。线程库 (std::thread) 易于使用但开销大;异步库 (std::async) 可异步执行任务,但 api 复杂;协程库 ...
    99+
    2024-05-14
    c++ 并发编程
  • 如何在 Golang 中备份数据库?
    在 golang 中备份数据库对于保护数据至关重要。可以使用标准库中的 database/sql 包,或第三方包如 github.com/go-sql-driver/mysql。具体步骤...
    99+
    2024-05-14
    golang 数据库备份 mysql git 标准库
  • 如何在 Golang 中优雅地处理错误?
    在 go 中,优雅处理错误包括:使用 error 类型;使用 errors 包函数和类型;自定义错误类型;遵循错误处理模式,包括关闭资源、检查错误、打印错误信息和处理或返回错误。 在 ...
    99+
    2024-05-14
    golang 错误处理
  • 如何构建 Golang RESTful API,并使用中间件进行身份验证?
    本文介绍了如何构建 golang restful api。首先,通过导入必要的库、定义数据模型和创建路由来构建 restful api。其次,使用 go-chi/chigot 和 go-...
    99+
    2024-05-14
    golang git
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作