java程序计算数独游戏

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

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

 1 import java.io.File;
 2 import java.util.List;
 3 //代表数独中的一个单元格位置
 4 public class Cell {
 5 
 6     // 所在行
 7     public int row;
 8     // 所在列
 9     public int colum;
10     //
11     public int value;
12 
13     public static int rowMax = 9;
14 
15     public static int columMax = 9;
16 
17     public static Cell[][] pan = new Cell[rowMax][columMax];
18 
19     // 初期化数独游戏
20     public static void init() {
21         // 数独盘上的值输入下面文件中,共9行,每行9个数字
22         // 0表示需要计算的空着的区域,
23         // eg:
24         // -----------------------
25         // 008309100
26         // 900060004
27         // 007504800
28         // 036000540
29         // 001000600
30         // 042000970
31         // 005907300
32         // 600010008
33         // 004608200
34         // -------------------------
35         File f = new File("conf/sd.txt");
36         List<List<Integer>> list = SDUtil.initQiPan(f);
37         for (int i = 0; i < rowMax; i++) {
38             for (int j = 0; j < columMax; j++) {
39                 pan[i][j] = new Cell(i, j);
40                 pan[i][j].value = list.get(i).get(j);
41             }
42         }
43     }
44 
45     // 取得下一个需要计算的位置
46     public Cell getNext() {
47         Cell next = null;
48         int row = 0;
49         int colum = 0;
50         if (this.row + 1 < rowMax) {
51             row = this.row + 1;
52             colum = this.colum;
53         } else if (this.colum + 1 < columMax) {
54             row = 0;
55             colum = this.colum + 1;
56         } else {
57             return null;
58         }
59         next = pan[row][colum];
60         return next;
61     }
62 
63     private Cell(int row, int colum) {
64         this.row = row;
65         this.colum = colum;
66     }
67 
68 }
 1 import java.io.BufferedReader;
 2 import java.io.File;
 3 import java.io.FileReader;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6 
 7 public class SDUtil {
 8 
 9     //把配置文件转换成二维列表
10     public static List<List<Integer>> initQiPan(File f) {
11         List<List<Integer>> list = new ArrayList<List<Integer>>();
12         try {
13             BufferedReader br = new BufferedReader(new FileReader(f));
14             String s;
15             while ((s = br.readLine()) != null) {
16                 s = s.trim();
17                 char[] car = s.toCharArray();
18                 List<Integer> l = new ArrayList<Integer>();
19                 for (int i = 0; i < 9; i++) {
20                     l.add(Integer.parseInt("" + car[i]));
21                 }
22                 list.add(l);
23             }
24             br.close();
25         } catch (Exception e) {
26             e.printStackTrace();
27         }
28         return list;
29     }
30 }
  1 import java.util.ArrayList;
  2 import java.util.Arrays;
  3 import java.util.List;
  4 
  5 public class ShuDu {
  6 
  7     static List<String> allNum = Arrays.asList("1", "2", "3", "4", "5", "6",
  8             "7", "8", "9");
  9 
 10     public static void main(String[] args) {
 11         begin();
 12 
 13     }
 14 
 15     public static void begin() {
 16         //初期化,数独中原有的数字装入
 17         Cell.init();
 18         //第一个位置取得
 19         Cell beginCell = Cell.pan[0][0];
 20         //计算
 21         insertCell(beginCell);
 22     }
 23 
 24     //打印结果
 25     public static void printOkValue() {
 26         for (int i = 0; i < Cell.rowMax; i++) {
 27             for (int j = 0; j < Cell.columMax; j++) {
 28                 System.out.print(Cell.pan[i][j].value + "    ");
 29             }
 30             System.out.println();
 31         }
 32     }
 33 
 34     //计算并插入正确的值(主要逻辑方法)
 35     public static boolean insertCell(Cell cell) {
 36         if (cell.value == 0) {
 37             List<String> canList = getCanInList(cell.row, cell.colum);
 38             if (canList.size() == 0) {
 39                 return false;
 40             }
 41             for (String can : canList) {
 42                 cell.value = Integer.parseInt(can);
 43                 Cell nextCell = cell.getNext();
 44                 if (nextCell != null) {
 45                     boolean b = insertCell(nextCell);
 46                     if (b) {
 47                         return true;
 48                     }
 49                 } else {
 50                     printOkValue();
 51                     System.exit(0);
 52                     ;
 53                 }
 54             }
 55             cell.value = 0;
 56         } else {
 57             Cell nextCell = cell.getNext();
 58             if (nextCell != null) {
 59                 boolean b = insertCell(nextCell);
 60                 if (b) {
 61                     return true;
 62                 }
 63             } else {
 64                 printOkValue();
 65                 System.exit(0);
 66                 ;
 67             }
 68         }
 69 
 70         return false;
 71     }
 72 
 73     //取得所在位置的所有可能数字列表
 74     public static List<String> getCanInList(int row, int colum) {
 75         List<String> canList = new ArrayList<String>();
 76         canList.addAll(allNum);
 77         lineValidate(canList, row, colum, Cell.columMax);
 78         columValidate(canList, row, colum, Cell.rowMax);
 79         blockValidate(canList, row, colum);
 80         return canList;
 81     }
 82 
 83     //行验证
 84     public static void lineValidate(List<String> set, int row, int colum,
 85             int max) {
 86         for (int i = 0; i < max; i++) {
 87             String value = Cell.pan[row][i].value + "";
 88             if (value.equals("0")) {
 89                 continue;
 90             }
 91             set.remove(value);
 92         }
 93     }
 94 
 95     //列验证
 96     public static void columValidate(List<String> set, int row, int colum,
 97             int max) {
 98         for (int i = 0; i < max; i++) {
 99             String value = Cell.pan[i][colum].value + "";
100             if (value.equals("0")) {
101                 continue;
102             }
103             set.remove(value);
104         }
105     }
106 
107     //所求位置所在的9个块验证
108     public static void blockValidate(List<String> canList, int row, int colum) {
109         int blockRow = row / 3 * 3;
110         int blockColum = colum / 3 * 3;
111         for (int i = 0 + blockRow; i < 3 + blockRow; i++) {
112             for (int j = 0 + blockColum; j < 3 + blockColum; j++) {
113                 String value = Cell.pan[i][j].value + "";
114                 if (value.equals("0")) {
115                     continue;
116                 }
117                 canList.remove(value);
118             }
119         }
120     }
121 }
原文地址:https://www.cnblogs.com/zwm512327/p/3767345.html