兴趣来了,写了个简单的数独游戏计算程序,未做算法优化。

通过文件来输入一个二维数组,9行,每行9个数组,数独游戏中需要填空的地方用0来表示。结果也是打印二维数组。

 import java.io.File;
import java.util.List;
//代表数独中的一个单元格位置
public class Cell { // 所在行
public int row;
// 所在列
public int colum;
// 值
public int value; public static int rowMax = 9; public static int columMax = 9; public static Cell[][] pan = new Cell[rowMax][columMax]; // 初期化数独游戏
public static void init() {
// 数独盘上的值输入下面文件中,共9行,每行9个数字
// 0表示需要计算的空着的区域,
// eg:
// -----------------------
// 008309100
// 900060004
// 007504800
// 036000540
// 001000600
// 042000970
// 005907300
// 600010008
// 004608200
// -------------------------
File f = new File("conf/sd.txt");
List<List<Integer>> list = SDUtil.initQiPan(f);
for (int i = 0; i < rowMax; i++) {
for (int j = 0; j < columMax; j++) {
pan[i][j] = new Cell(i, j);
pan[i][j].value = list.get(i).get(j);
}
}
} // 取得下一个需要计算的位置
public Cell getNext() {
Cell next = null;
int row = 0;
int colum = 0;
if (this.row + 1 < rowMax) {
row = this.row + 1;
colum = this.colum;
} else if (this.colum + 1 < columMax) {
row = 0;
colum = this.colum + 1;
} else {
return null;
}
next = pan[row][colum];
return next;
} private Cell(int row, int colum) {
this.row = row;
this.colum = colum;
} }
 import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List; public class SDUtil { //把配置文件转换成二维列表
public static List<List<Integer>> initQiPan(File f) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
try {
BufferedReader br = new BufferedReader(new FileReader(f));
String s;
while ((s = br.readLine()) != null) {
s = s.trim();
char[] car = s.toCharArray();
List<Integer> l = new ArrayList<Integer>();
for (int i = 0; i < 9; i++) {
l.add(Integer.parseInt("" + car[i]));
}
list.add(l);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
 import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; public class ShuDu { static List<String> allNum = Arrays.asList("1", "2", "3", "4", "5", "6",
"7", "8", "9"); public static void main(String[] args) {
begin(); } public static void begin() {
//初期化,数独中原有的数字装入
Cell.init();
//第一个位置取得
Cell beginCell = Cell.pan[0][0];
//计算
insertCell(beginCell);
} //打印结果
public static void printOkValue() {
for (int i = 0; i < Cell.rowMax; i++) {
for (int j = 0; j < Cell.columMax; j++) {
System.out.print(Cell.pan[i][j].value + " ");
}
System.out.println();
}
} //计算并插入正确的值(主要逻辑方法)
public static boolean insertCell(Cell cell) {
if (cell.value == 0) {
List<String> canList = getCanInList(cell.row, cell.colum);
if (canList.size() == 0) {
return false;
}
for (String can : canList) {
cell.value = Integer.parseInt(can);
Cell nextCell = cell.getNext();
if (nextCell != null) {
boolean b = insertCell(nextCell);
if (b) {
return true;
}
} else {
printOkValue();
System.exit(0);
;
}
}
cell.value = 0;
} else {
Cell nextCell = cell.getNext();
if (nextCell != null) {
boolean b = insertCell(nextCell);
if (b) {
return true;
}
} else {
printOkValue();
System.exit(0);
;
}
} return false;
} //取得所在位置的所有可能数字列表
public static List<String> getCanInList(int row, int colum) {
List<String> canList = new ArrayList<String>();
canList.addAll(allNum);
lineValidate(canList, row, colum, Cell.columMax);
columValidate(canList, row, colum, Cell.rowMax);
blockValidate(canList, row, colum);
return canList;
} //行验证
public static void lineValidate(List<String> set, int row, int colum,
int max) {
for (int i = 0; i < max; i++) {
String value = Cell.pan[row][i].value + "";
if (value.equals("0")) {
continue;
}
set.remove(value);
}
} //列验证
public static void columValidate(List<String> set, int row, int colum,
int max) {
for (int i = 0; i < max; i++) {
String value = Cell.pan[i][colum].value + "";
if (value.equals("0")) {
continue;
}
set.remove(value);
}
} //所求位置所在的9个块验证
public static void blockValidate(List<String> canList, int row, int colum) {
int blockRow = row / 3 * 3;
int blockColum = colum / 3 * 3;
for (int i = 0 + blockRow; i < 3 + blockRow; i++) {
for (int j = 0 + blockColum; j < 3 + blockColum; j++) {
String value = Cell.pan[i][j].value + "";
if (value.equals("0")) {
continue;
}
canList.remove(value);
}
}
}
}

最新文章

  1. shell脚本批量收集linux服务器的硬件信息快速实现
  2. 新建 ASP.NET Core Web API 项目 -- RESTFul 风格 Hello World!
  3. Redis配置文件参数说明
  4. Ajax提交后台中文乱码问题
  5. Java通过jedis操作redis缓存
  6. C++多线程编程入门之经典实例
  7. BZOJ 3165 Segment
  8. 李洪强漫谈iOS开发[C语言-037]-if else 语句
  9. delphi 基础书籍推荐
  10. AngularJs学习笔记3-服务及过滤器
  11. android-学习1 配置环境
  12. 使用react-router实现单页面应用路由
  13. 移动开发day2_css预处理器_flex布局
  14. intellij IDEA导入java源码
  15. 如何备份和恢复你的TFS服务器(二)
  16. VB.Net 经典画圆方法
  17. postgresql-int,bigint,numeric效率测试
  18. Siki_Unity_4-4_丛林战争_Socket/TCP网络游戏开发
  19. Java 9的日期时间格式化趋近Unicode区域设置标准
  20. RabbitMQ(4) 未路由的消息、TTL和死信

热门文章

  1. Introducing the Filter Types
  2. DotNetCore 微服务上传附件
  3. Centos7安装和配置NFS
  4. SpringMVC完成文件上传的基本步骤
  5. 关于DRY原则
  6. 验证二叉查找树(LintCode)
  7. CSU - 1334 -好老师(STL-map用法)
  8. HDU5742 It&#39;s All In The Mind 数学思维题
  9. [Contest20171005]Maze
  10. 【暴力】hdu6121 Build a tree