import java.io.*; import java.util.*; /** * @param 导师:袁超 * @param 作者:刘跃勇 * @param 日期:2010/4/23 */ public class Link { public static void main(String[] argv) throws IOException { gameRun(); } /** * @param 无 * @param ---------------------------------------------------------------- * @param 函数内部变量说明: * @param in * 记录键盘输入 * @param num * 记录配对的对数 */ public static byte gameRun() throws NumberFormatException, IOException { map = createMap((byte) 6, (byte) 8, (byte) 4); printMap(map); System.out.println(); swap(map); BufferedReader in = new BufferedReader(new InputStreamReader(System.in));// 记录键盘输入 byte num = (byte) (map.length * map[0].length / 2);// 记录配对的对数 while (0 < num) { printMap(map); System.out.print("行(先):"); fRow = (byte) Integer.parseInt(in.readLine()); System.out.print("列(先):"); fCol = (byte) Integer.parseInt(in.readLine()); System.out.print("你选择了:"); System.out.println(map[fRow][fCol]); System.out.print("行(后):"); bRow = (byte) Integer.parseInt(in.readLine()); System.out.print("列(后):"); bCol = (byte) Integer.parseInt(in.readLine()); System.out.print("你选择了:"); System.out.println(map[bRow][bCol]); if (map[fRow][fCol] == map[bRow][bCol]) { if (updateX(fRow, fCol, bRow, bCol) || updateY(fRow, fCol, bRow, bCol) || updateOne(fRow, fCol, bRow, bCol) || updateTwo(fRow, fCol, bRow, bCol)) { map[fRow][fCol] = -1; map[bRow][bCol] = -1; num--; } } } System.out.println("Win"); return 0; } /** * 函数功能:两次选择在同一行且不需要变向时的判断 * * @return 地图需要变动返回true,否则返回false */ public static boolean updateX(byte fRow, byte fCol, byte bRow, byte bCol) { if (fRow != bRow) { return false;// 本函数只考虑同行且不需要变向时的情况,不同行即返回false } if (fCol == bCol) { return false;// 两次选择一样,不能删除,即地图不需要变动 } if (fCol < bCol) { for (byte i = (byte) (fCol + 1); i < bCol; i++) { if (-1 != map[fRow][i]) { return false; } } } else { for (byte i = (byte) (bCol + 1); i < fCol; i++) { if (-1 != map[bRow][i]) { return false; } } } return true; } /** * 函数功能:两次选择在同一列且不需要变向时的判断 * * @return 地图需要变动返回true,否则返回false */ public static boolean updateY(byte fRow, byte fCol, byte bRow, byte bCol) { if (fCol != bCol) { return false;// 本函数只考虑同列且不需要变向时的情况,不同列即返回false } if (fRow == bRow) { return false;// 两次选择一样,不能删除,即地图不需要变动 } if (fRow < bRow) { for (byte i = (byte) (fRow + 1); i < bRow; i++) { if (-1 != map[i][fCol]) { return false; } } } else { for (byte i = (byte) (bRow + 1); i < fRow; i++) { if (-1 != map[i][bCol]) { return false; } } } return true; } /** * 函数功能:只需一次变向的判断 * * @return 地图需要变动返回true,否则返回false * * @param 先左上先左下先右上先右下四种情况是可以合并的 */ public static boolean updateOne(byte fRow, byte fCol, byte bRow, byte bCol) { if (fRow == bRow || fCol == bCol) { return false;// 同行或同列有专门的判断函数,本函数不再考虑 } if (-1 == map[fRow][bCol] || -1 == map[bRow][fCol])// 判断两个结点是否已清空,也就是图片已经消掉了 { if (updateX(fRow, fCol, fRow, bCol) && updateY(fRow, bCol, bRow, bCol)) { return true; } if (updateY(fRow, fCol, bRow, fCol) && updateX(bRow, fCol, bRow, bCol)) { return true; } } return false; } /** * 函数功能:需两次变向的判断 * * @return 地图需要变动返回true,否则返回false */ public static boolean updateTwo(byte fRow, byte fCol, byte bRow, byte bCol) { byte m = (byte) map.length; byte n = (byte) map[0].length; /** 两次选择都在边界 */ if ((0 == fRow && 0 == bRow) || (m == fRow && m == bRow) || (0 == fCol && 0 == bCol) || (n == fCol && n == bCol)) { return true; } /** 只有一次选择在边界 */ if (0 == fRow && 0 != bRow) { if (updateY(bRow, bCol, (byte) 0, bCol)) { return true; } } if (0 == bRow && 0 != fRow) { if (updateY(fRow, fCol, (byte) 0, fCol)) { return true; } } if (m == fRow && m != bRow) { if (updateY(bRow, bCol, m, bCol)) { return true; } } if (m == bRow && m != fRow) { if (updateY(fRow, fCol, m, fCol)) { return true; } } if (0 == fCol && 0 != bCol) { if (updateX(bRow, bCol, bRow, (byte) 0)) { return true; } } if (0 == bCol && 0 != fCol) { if (updateX(fRow, fCol, fRow, (byte) 0)) { return true; } } if (n == fCol && n != bCol) { if (updateX(bRow, bCol, bRow, n)) { return true; } } if (n == bCol && n != fCol) { if (updateX(fRow, fCol, fRow, n)) { return true; } } /** 两次选择都不在边界 */ if ((updateX(fRow, fCol, fRow, (byte) 0) && updateX(bRow, bCol, bRow, (byte) 0)) || (updateX(fRow, fCol, fRow, n) && updateX(bRow, bCol, bRow, n))) { return true; } if ((updateY(fRow, fCol, (byte) 0, fCol) && updateY(bRow, bCol, (byte) 0, bCol)) || (updateY(fRow, fCol, m, fCol) && updateY(bRow, bCol, m, bCol))) { return true; } for (byte i = 0; i < m; i++) { for (byte j = 0; j < n; j++) { if (updateOne(fRow, fCol, i, j) && (updateX(i, j, bRow, bCol) || updateY(i, j, bRow, bCol))) { return true; } if (updateOne(bRow, bCol, i, j) && (updateX(i, j, fRow, fCol) || updateY(i, j, fRow, fCol))) { return true; } } } return false; } /** * @param m * 将要创建地图的行 * @param n * 将要创建地图的列 * @param type * 地图中不同图片种类的数量 * @return 返回一个二维的整型数组用于表示地图 * @param 注意: * @param 前两个参数至少要有一个偶数,为了连连看能够配对. * @param 不在这里添加参数m,n的奇偶校验是为了写成通用方法. */ public static byte[][] createMap(byte m, byte n, byte type) { byte[][] map = new byte[m][n]; for (byte i = 0; i < m; i++) { for (byte j = 0; j < n; j++) { map[i][j] = (byte) (j % type); } } return map; } /** * @param map * @param 函数功能:输出地图 */ public static void printMap(byte[][] map) { byte m = (byte) map.length; byte n = (byte) map[0].length; for (byte i = 0; i < m; i++) { for (byte j = 0; j < n; j++) { if (-1 == map[i][j]) { System.out.print(" "); } else { System.out.print(map[i][j]); } System.out.print(" "); } System.out.println(); } } /** * @param map * @param 函数功能:打乱地图 * @param x,y随机坐标 */ public static void swap(byte[][] map) { byte m = (byte) map.length; byte n = (byte) map[0].length; byte x, y;// 记录坐标 for (byte i = 0; i < m; i++) { for (byte j = 0; j < n; j++) { x = (byte) rand.nextInt(m); y = (byte) rand.nextInt(n); /** * 当 i==x && j==y 的时候,异或交换的结果是0,会丢失原数,所以要加个判断 * 当然也可以不用异或,再定义个变量来交换 */ if (i != x || j != y) { map[i][j] ^= map[x][y]; map[x][y] ^= map[i][j]; map[i][j] ^= map[x][y]; } } } } static Random rand = new Random(); /** 游戏地图 */ static byte[][] map; /** 先选择的横坐标 */ static byte fCol; /** 先选择的纵坐标 */ static byte fRow; /** 后选择的横坐标 */ static byte bCol; /** 后选择的纵坐标 */ static byte bRow; }