晚上闲来无事,刚装了一个ylmf的os,里面有个小游戏,九宫格,以前知道这个游戏没玩过,LP想玩玩就陪她看看,每个格子都用同样的方法计算,就写了一段代码来计算,呵呵,只能计算最简单的,复杂一点的还没研究。好了废话少说直接上代码,没有注释请大家原谅:
(解释下,把九宫格写成一个字符串,没有填写数字的为O,每一行用逗号隔开,每行中数字用空格隔开)
package com.jgg; public class CalcJGG { /** * @param args */ public static void main(String[] args) { JGG jgg = new JGG(); jgg.init("7 6 8 3 2 5 1 4 9,3 4 1 0 9 0 2 5 6,2 9 5 1 6 4 0 8 0,9 1 2 0 5 6 4 3 0,6 8 3 0 0 0 0 2 0,4 5 7 2 0 0 0 6 0,5 7 4 6 3 9 8 1 2,1 3 9 5 8 2 6 7 4,8 2 6 4 7 1 0 9 0"); jgg.out(); jgg.calc(); System.out.println("计算结果:"); jgg.out(); } } package com.jgg; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class JGG { private int[][] jgg; private boolean isOut = false; private boolean isNotCalc = false; public JGG_bak() { jgg = new int[9][9]; } public void init(String str) { String[] arrStr = str.split(","); for (int i = 0; i < arrStr.length; i++) { String[] tmpArr = arrStr[i].split(" "); for (int j = 0; j < tmpArr.length; j++) { jgg[i][j] = Integer.parseInt(tmpArr[j]); } } } public void calc() { while (!isWin()) { isNotCalc = true; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (jgg[i][j] == 0) { List<Integer> rows = row(i); if (isFind(rows, i, j)) { continue; } List<Integer> cols = column(j); if (isFind(cols, i, j)) { continue; } List<Integer> colRow = colRow(i, j); if (isFind(colRow, i, j)) { continue; } List<Integer> tmpList = compare(rows, cols); if (isFind(tmpList, i, j)) { continue; } List<Integer> result = compare(tmpList, colRow); isFind(result, i, j); } } } if (isNotCalc) { System.out.println("对不起,不能计算!"); break; } } } public List<Integer> row(int i) { List<Integer> tmpList = new ArrayList<Integer>(); int[] tmpInt = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; for (int j = 0; j < 9; j++) { if (jgg[i][j] != 0) { for (int k = 0; k < tmpInt.length; k++) { if (tmpInt[k] == jgg[i][j]) { tmpInt[k] = 0; break; } } } } for (int j = 0; j < tmpInt.length; j++) { if (tmpInt[j] != 0) { tmpList.add(tmpInt[j]); } } return tmpList; } public List<Integer> column(int j) { List<Integer> tmpList = new ArrayList<Integer>(); int[] tmpInt = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; for (int i = 0; i < 9; i++) { if (jgg[i][j] != 0) { for (int k = 0; k < tmpInt.length; k++) { if (tmpInt[k] == jgg[i][j]) { tmpInt[k] = 0; break; } } } } for (int i = 0; i < tmpInt.length; i++) { if (tmpInt[i] != 0) { tmpList.add(tmpInt[i]); } } return tmpList; } public List<Integer> colRow(int i, int j) { List<Integer> tmpList = new ArrayList<Integer>(); int[] tmpInt = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int startRow = (i / 3) * 3; int startCol = (j / 3) * 3; for (int k = startRow; k < (startRow + 3); k++) { for (int m = startCol; m < (startCol + 3); m++) { if (jgg[k][m] != 0) { for (int index = 0; index < tmpInt.length; index++) { if (tmpInt[index] == jgg[k][m]) { tmpInt[index] = 0; break; } } } } } for (int m = 0; m < tmpInt.length; m++) { if (tmpInt[m] != 0) { tmpList.add(tmpInt[m]); } } return tmpList; } public List<Integer> compare(List<Integer> list1, List<Integer> list2) { List<Integer> tmpList = new ArrayList<Integer>(); for (Iterator iter = list1.iterator(); iter.hasNext();) { int tmp = (Integer)iter.next(); for (Iterator iter1 = list2.iterator(); iter1.hasNext();) { int tmp2 = (Integer)iter1.next(); if (tmp == tmp2) { tmpList.add(tmp); break; } } } return tmpList; } public boolean isWin(){ for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (jgg[i][j] == 0) { return false; } } } return true; } public void out(){ for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { System.out.print(jgg[i][j] + " "); } System.out.println(); } } public boolean isFind(List<Integer> list, int i, int j){ if (list.size() == 1) { jgg[i][j] = list.get(0); isNotCalc = false; if (isOut) { System.out.println("添加第" + (i + 1) + "行,第" + (j + 1) + "列 为【" + jgg[i][j] + "】"); } return true; } return false; } public boolean isOut() { return isOut; } public void setOut(boolean isOut) { this.isOut = isOut; } }