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

通过文件来输入一个二维数组,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);
}
}
}
}
05-11 11:27