连连看核心算法与基本思想(附全部项目代码链接与代码详细注释)

短命女 2024-04-24 14:35 135阅读 0赞

文章目录

  • 0.说明
  • 1.基本要求
  • 2.思路分析(加入核心代码)
    • 2.1 游戏初始化局面
    • 2.2 两点是否可连
    • 2.3 游戏是否结束
    • 2.4 判断死局
  • 3.注意事项与全部代码

0.说明

对于数据结构和算法,我并不是很精通(真的很一般),因此在这里只是做一个自己的简单分享,其实从这次数据结构课设中看出了自己存在了许多在这方面的不足。我的代码还存在一些不足,比如得到两个拐点最短路径的方法我并不是采用广度优先算法,而是使用的深度优先。顺便演示一下最终的效果:

image-20221203024433521

image-20221203024551866

image-20221203161357812

1.基本要求

  1. 生成游戏初始局面;
  2. 每次用户选择两个图形,如果图形能满足一定条件(如果两个图形一样,且这个两个图形直接存在少于 3 个弯的路径),则两个图形都能消掉。给定具有相同图形的任意两个格子,我们需要寻找这两个格子之间在转弯最少的情况下,经过格子数目最少的路径。如果这个最优路径的转弯数目少于 3,则这个两个格子可以消去;
  3. 判断游戏是否结束。如果所有图形全部消去,游戏结束;
  4. 判断死锁,当游戏玩家不可能消去任意两个图像的时候,游戏进入“死锁”状态。当游戏进入“死锁”状态,可以随机打乱局面,解除“死锁”。

2.思路分析(加入核心代码)

以下仅是我个人的思路

2.1 游戏初始化局面

  • 加载图片
  • 生成随机成对布局

2.2 两点是否可连

  • 先判断直连

    1. /**
    2. * 判断是否可以直连
    3. *
    4. * @param x1 当前位置点的横坐标
    5. * @param y1 当前位置点的纵坐标
    6. * @param x2 目标位置点的横坐标
    7. * @param y2 目标位置点的纵坐标
    8. * @return 是否可连
    9. */
    10. public boolean isLineLink(int x1, int y1, int x2, int y2) {
    11. if (x1 == x2) {
    12. int minY = Math.min(y1, y2) + 1;
    13. int maxY = Math.max(y1, y2);
    14. while (minY < maxY) {
    15. if (map[minY][x1] != 0) {
    16. return false;
    17. }
    18. minY++;
    19. }
    20. return true;
    21. } else if (y1 == y2) {
    22. int minX = Math.min(x1, x2) + 1;
    23. int maxX = Math.max(x1, x2);
    24. while (minX < maxX) {
    25. if (map[y2][minX] != 0) {
    26. return false;
    27. }
    28. minX++;
    29. }
    30. return true;
    31. } else {
    32. return false;
    33. }
    34. }
  • 再判断是否折一次可连

    1. /**
    2. * 判断是否可以通过一次折点连通
    3. *
    4. * @param x1 当前位置点的横坐标
    5. * @param y1 当前位置点的纵坐标
    6. * @param x2 目标位置点的横坐标
    7. * @param y2 目标位置点的纵坐标
    8. * @param g 画笔
    9. * @return 是否可连
    10. */
    11. public boolean isLinkByOne(int x1, int y1, int x2, int y2, Graphics g) {
    12. g.setColor(Color.RED);
    13. if (isLineLink(x1, y1, x1, y2) && map[y2][x1] == 0 && isLineLink(x1, y2, x2, y2)) {
    14. g.drawLine(x1 * 50 + 25, y1 * 50 + 25, x1 * 50 + 25, y2 * 50 + 25);
    15. g.drawLine(x1 * 50 + 25, y2 * 50 + 25, x2 * 50 + 25, y2 * 50 + 25);
    16. return true;
    17. } else if (isLineLink(x1, y1, x2, y1) && map[y1][x2] == 0 && isLineLink(x2, y1, x2, y2)) {
    18. g.drawLine(x1 * 50 + 25, y1 * 50 + 25, x2 * 50 + 25, y1 * 50 + 25);
    19. g.drawLine(x2 * 50 + 25, y1 * 50 + 25, x2 * 50 + 25, y2 * 50 + 25);
    20. return true;
    21. }
    22. return false;
    23. }
  • 最后判断图片是否可以两次可连 —> 深度优先搜索

    1. /**
    2. * 深度优先搜索
    3. * 判断是否可以通过两次折点连通 - 如果存在,在此基础上得到最短路径
    4. *
    5. * @param x1 当前位置点的横坐标
    6. * @param y1 当前位置点的纵坐标
    7. * @param x2 目标位置点的横坐标
    8. * @param y2 目标位置点的纵坐标
    9. * @param inflectionPointNum 当前折点的次数
    10. * @param direction 当前方向:无(-1) 右(0) 下(1) 左(2) 右(3)
    11. * @param step 当前走的步数
    12. */
    13. public void isLinkByTwo(int x1, int y1, int x2, int y2, int inflectionPointNum, int direction, int step) {
  1. //发现折点次数达到三次就返回,表示路径不行
  2. if (inflectionPointNum == 3) {
  3. return;
  4. }
  5. //记录该点位置到路径数组中
  6. tempPath[step][0] = y1;
  7. tempPath[step][1] = x1;
  8. //查看是否到达目标点
  9. if (x1 == x2 && y1 == y2) {
  10. //说明找到了更短的,就将这个更短路径放入到最短路径数组中存储
  11. if (step < minStep) {
  12. minStep = step;
  13. for (int i = 0; i <= minStep; i++) {
  14. minPath[i][0] = tempPath[i][0];
  15. minPath[i][1] = tempPath[i][1];
  16. }
  17. }
  18. return;
  19. }
  20. //标记当前点已经访问
  21. isVisited[y1][x1] = 1;
  22. int tx;
  23. int ty;
  24. //尝试不同的方向
  25. for (int i = 0; i < 4; i++) {
  26. tx = x1 + dx[i];
  27. ty = y1 + dy[i];
  28. /*
  29. 1.
  30. tx >= 0 && tx < LENGTH && ty >= 0 && ty < LENGTH 保证不越界
  31. map[ty][tx] == 0 保证是空白区
  32. isVisited[ty][tx] == 0 保证是未访问的
  33. 2.
  34. tx == x2 && ty == y2 说明是目标点
  35. */
  36. if (tx >= 0 && tx < LENGTH && ty >= 0 && ty < LENGTH && map[ty][tx] == 0 && isVisited[ty][tx] == 0 || tx == x2 && ty == y2) {
  37. //标记即将查找的点为 已访问
  38. isVisited[ty][tx] = 1;
  39. if (direction == -1) {
  40. //如果无方向,这是第一次深度搜索的状态
  41. isLinkByTwo(tx, ty, x2, y2, inflectionPointNum, i, step + 1);
  42. } else if (i != direction) {
  43. //如果即将走的点的方向与原来行走的点方向不一致
  44. isLinkByTwo(tx, ty, x2, y2, inflectionPointNum + 1, i, step + 1);
  45. } else {
  46. //如果方向一致:可以与第一个if的代码合并,但为了层次更为清晰,这里分开写
  47. isLinkByTwo(tx, ty, x2, y2, inflectionPointNum, i, step + 1);
  48. }
  49. //访问完毕,回溯回来就标记为未访问,给后面的点来查找
  50. isVisited[ty][tx] = 0;
  51. }
  52. }
  53. }
  54. > 广度优先搜索其实更好,但我有特别纠结却又不太好描述的问题导致我写到一般就没再继续用广度优先,总感觉对于 ”寻找这两个格子之间在转弯最少的情况下,经过格子数目最少的路径“ 有点问题,脑子绕不过来。

2.3 游戏是否结束

  • 用一个count全局变量来记录当前剩余的数目

2.4 判断死局

  • 对每个不为空的点进行深度优先搜索,如果该点返回true即找到有可连,就不再继续继续其它点的搜索
  • 判断一个地图是否为死局需要时间,而在这个过程中如果进行其它操作比如“重新开始游戏”就可能会弹出对话框上一张地图是死锁,而此时对于上一张是否为死锁是毫无意义的,因此我加入了一个地图版本号nowVersion,在每次更新地图时都会改变,值为当前系统时间 System.currentTimeMillis()

    /**

    1. * 深度优先搜索
    2. * 搜索有没有值为value并与最初点可连的点
    3. *
    4. * @param x1 当前点的横坐标
    5. * @param y1 当前点的纵坐标
    6. * @param value 最初点的值
    7. * @param inflectionPointNum 拐点个数
    8. * @param direction 当前方向
    9. * @param step 当前步数
    10. * @return 是否找到
    11. */
    12. public boolean search(int x1, int y1, int value, int inflectionPointNum, int direction, int step) {
    13. if (inflectionPointNum == 3) {
    14. return false;
    15. }
    16. /*
    17. 步数不为0是保证与刚开始的搜索不会冲突,否则刚已进入最初的搜索就会表明找到
    18. */
    19. if (value == runMap[y1][x1] && step != 0) {
    20. //说明找到了一条
    21. return true;
    22. }
    23. isVisitedForThread[y1][x1] = 1;
    24. int tx;
    25. int ty;
    26. boolean isFind = false;
    27. //尝试不同的方向
    28. for (int i = 0; i < 4 && !isFind; i++) {
    29. tx = x1 + dx[i];
    30. ty = y1 + dy[i];
    31. /*
    32. tx >= 0 && tx < LENGTH && ty >= 0 && ty < LENGTH && isVisitedForThread[ty][tx] == 0 不越界并且未被访问
    33. runMap[ty][tx] == 0 临时地图的该坐标为null则可走
    34. value == runMap[ty][tx] 临时地图的该坐标就为目标相等值则可走
    35. */
    36. if (tx >= 0 && tx < LENGTH && ty >= 0 && ty < LENGTH && isVisitedForThread[ty][tx] == 0 && (runMap[ty][tx] == 0 || value == runMap[ty][tx])) {
    37. isVisitedForThread[ty][tx] = 1;
    38. if (direction == -1) {
    39. isFind = search(tx, ty, value, inflectionPointNum, i, step + 1);
    40. } else if (i != direction) {
    41. isFind = search(tx, ty, value, inflectionPointNum + 1, i, step + 1);
    42. } else {
    43. isFind = search(tx, ty, value, inflectionPointNum, i, step + 1);
    44. }
    45. isVisitedForThread[ty][tx] = 0;
    46. }
    47. }
    48. return isFind;
    49. }

3.注意事项与全部代码

  • 我会把项目(包括代码,图片以及背景音乐代码)放在我的百度网盘:https://pan.baidu.com/s/1ykr53VP1zvfvk9VHG413yA?pwd=1314
  • 图片你可以自定义,但需要是 50*50 大小的图片

    image-20221203025127720

  • 代码注释比较详细,可以明确知道某段代码是在做什么

? 不包括音乐代码的全部代码

  1. package com.fox.link;
  2. import javax.imageio.ImageIO;
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.MouseAdapter;
  6. import java.awt.event.MouseEvent;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.util.Arrays;
  10. import java.util.Random;
  11. /**
  12. * @author 狐狸半面添
  13. * @create 2022-12-02 1:18
  14. */
  15. public class Game {
  16. private static GamePanel gamePanel;
  17. public static void main(String[] args) throws IOException {
  18. //画一个窗体
  19. JFrame gameFrame = new JFrame("连连看");
  20. //设置宽和高
  21. gameFrame.setSize(GamePanel.LENGTH * 51, GamePanel.LENGTH * 54);
  22. //设置一个菜单条
  23. JMenuBar menuBar = new JMenuBar();
  24. //设置一个菜单组件
  25. JMenu jMenu = new JMenu("设置");
  26. //设置菜单项
  27. JMenuItem restartItem = new JMenuItem("重新开始");
  28. //增加重新开始监听事件 --> 重新初始化地图
  29. restartItem.addActionListener(e-> {
  30. //初始化地图
  31. gamePanel.initMap();
  32. //刷新版本号
  33. gamePanel.nowVersion = System.currentTimeMillis();
  34. //刷新页面
  35. gamePanel.repaint();
  36. });
  37. //设置菜单项 - 退出
  38. JMenuItem exitItem = new JMenuItem("退出");
  39. exitItem.addActionListener(e-> {
  40. int option = JOptionPane.showConfirmDialog(gameFrame, "您确认退出吗?", "确认框", JOptionPane.YES_NO_OPTION);
  41. if (option == JOptionPane.YES_OPTION) {
  42. //选择了确定则退出程序
  43. System.exit(0);
  44. }
  45. });
  46. //将组件整合
  47. jMenu.add(restartItem);
  48. jMenu.add(exitItem);
  49. menuBar.add(jMenu);
  50. gameFrame.setJMenuBar(menuBar);
  51. //往窗口添加一个自定义容器
  52. gamePanel = new GamePanel(gameFrame);
  53. gameFrame.add(gamePanel);
  54. //开启线程监听当前是否死局
  55. new Thread(gamePanel).start();
  56. //设置窗口的图标/logo
  57. gameFrame.setIconImage(ImageIO.read(new File(Game.class.getResource("/").getPath()+"img/logo.png")));
  58. //展示窗体
  59. gameFrame.setVisible(true);
  60. //点击 x 即可退出程序
  61. gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  62. //设置窗口大小不可改变
  63. gameFrame.setResizable(false);
  64. //设置窗口位置到屏幕中间
  65. gameFrame.setLocationRelativeTo(null);
  66. }
  67. }
  68. class GamePanel extends JPanel implements Runnable {
  69. /**
  70. * 设置地图长度
  71. */
  72. public static final int LENGTH = 14;
  73. /**
  74. * 设置随机范围 --> 随机的图片数目
  75. */
  76. private final int RANDOM_NUM = 8;
  77. /**
  78. * 存储加载的RANDOM_NUM张图片
  79. */
  80. private Image[] images;
  81. /**
  82. * 地图
  83. */
  84. private int[][] map;
  85. /**
  86. * 临时地图去判断死局
  87. */
  88. private int[][] runMap;
  89. /**
  90. * 记录被选中的点的坐标
  91. */
  92. private Point selectedPoint;
  93. /**
  94. * 是否被选中,默认为 false
  95. */
  96. private boolean isSelected;
  97. /**
  98. * 保存当前地图的访问信息
  99. */
  100. private int[][] isVisited;
  101. /**
  102. * 保存临时地图的访问信息
  103. */
  104. private int[][] isVisitedForThread;
  105. /**
  106. * 保存访问路径
  107. * [i][0] 是横坐标
  108. * [i][1] 是纵坐标
  109. */
  110. private final int[][] tempPath = new int[100][2];
  111. /**
  112. * 保存最短的访问路径
  113. * [i][0] 是纵坐标
  114. * [i][1] 是横坐标
  115. */
  116. private final int[][] minPath = new int[100][2];
  117. /**
  118. * dx是保存四个方向在横坐标上的位置
  119. * 方向数组:对应 右 下 左 上
  120. */
  121. private final int[] dx = {
  122. 1, 0, -1, 0};
  123. /**
  124. * dy是保存四个方向在横坐标上的位置
  125. * 方向数组:对应 右 下 左 上
  126. */
  127. private final int[] dy = {
  128. 0, 1, 0, -1};
  129. /**
  130. * 记录最小步数
  131. */
  132. private int minStep;
  133. /**
  134. * 临时变量
  135. */
  136. private int temp;
  137. /**
  138. * 当前地图上剩余的图片数目
  139. */
  140. private int count;
  141. private final JFrame parentFrame;
  142. /**
  143. * 当前版本号
  144. * 判断一个地图是否为死局需要时间,而在这个过程中如果进行其它操作比如“重新开始游戏”就可能会显示上一张地图是死锁
  145. * 这是没有必要的,因此加个版本号进行判断
  146. */
  147. public long nowVersion;
  148. /**
  149. * 初始化画板
  150. *
  151. * @param parentFrame 父窗口
  152. * @throws IOException 异常
  153. */
  154. public GamePanel(JFrame parentFrame) throws IOException {
  155. //设置父节点
  156. this.parentFrame = parentFrame;
  157. //获取类路径
  158. String imgBasePath = this.getClass().getResource("/").getPath() + "img/";
  159. //加载图片存入数组
  160. images = new Image[RANDOM_NUM + 1];
  161. for (int i = 0; i <= RANDOM_NUM; i++) {
  162. images[i] = ImageIO.read(new File(imgBasePath + i + ".jpg"));
  163. }
  164. //初始化地图
  165. initMap();
  166. //创建选择点
  167. selectedPoint = new Point();
  168. //连连看监听
  169. addMouseListener(new MouseAdapter() {
  170. @Override
  171. public void mouseClicked(MouseEvent e) {
  172. //获取当前的点击坐标
  173. int x = e.getX() / 50;
  174. int y = e.getY() / 50;
  175. //如果超出真实地图范围则取消下一步操作
  176. if (x >= LENGTH || y >= LENGTH) {
  177. return;
  178. }
  179. Graphics g = getGraphics();
  180. g.setColor(Color.RED);
  181. //如果不是空,就可以 选中/连接 判断
  182. if (map[y][x] != 0) {
  183. //如果当前没有选中的图像
  184. if (!isSelected) {
  185. //就设置当前点击到的图像为选中图像
  186. isSelected = true;
  187. selectedPoint.x = x;
  188. selectedPoint.y = y;
  189. g.drawRect(50 * x, 50 * y, 50, 50);
  190. } else if (selectedPoint.x == x && selectedPoint.y == y) {
  191. //如果之前选中了图片,现在是重复点击,就取消选中图片的选中
  192. isSelected = false;
  193. repaint();
  194. } else {
  195. //进入else说明选中了两张不同位置的图片
  196. //如果两张图片不相同
  197. if (map[selectedPoint.y][selectedPoint.x] != map[y][x]) {
  198. //就取消已选中的图片的选中
  199. isSelected = false;
  200. repaint();
  201. } else {
  202. //进入else说明要去真正判断是否可连了
  203. //如果可以直连,即无折点
  204. if (isLineLink(x, y, selectedPoint.x, selectedPoint.y)) {
  205. //就画线连接
  206. g.drawRect(50 * x, 50 * y, 50, 50);
  207. g.drawLine(50 * x + 25, 50 * y + 25, selectedPoint.x * 50 + 25, selectedPoint.y * 50 + 25);
  208. try {
  209. //暂停1s,主要是为了能看清画线
  210. Thread.sleep(1000);
  211. } catch (InterruptedException ex) {
  212. ex.printStackTrace();
  213. }
  214. //将两个点的坐标标记为0即空白了
  215. map[y][x] = 0;
  216. map[selectedPoint.y][selectedPoint.x] = 0;
  217. //减少当前地图剩余图片的数量
  218. count -= 2;
  219. //修改版本号
  220. nowVersion = System.currentTimeMillis();
  221. } else if (isLinkByOne(x, y, selectedPoint.x, selectedPoint.y, g)) {
  222. //如果可以在只经过一个折点的情况下直连
  223. g.drawRect(50 * x, 50 * y, 50, 50);
  224. try {
  225. Thread.sleep(1000);
  226. } catch (InterruptedException ex) {
  227. throw new RuntimeException(ex);
  228. }
  229. map[y][x] = 0;
  230. map[selectedPoint.y][selectedPoint.x] = 0;
  231. count -= 2;
  232. nowVersion = System.currentTimeMillis();
  233. } else {
  234. //初始化深度优先搜索需要用到的访问情况数组与最短路径
  235. fillZeroAndSetMinPath();
  236. //进行深度优先搜索 --> 找到了基于两个折点的最短路径就会改变 minStep
  237. isLinkByTwo(x, y, selectedPoint.x, selectedPoint.y, 0, -1, 0);
  238. //如果不是 Integer.MAX_VALUE,说明找到了最大值
  239. if (minStep != Integer.MAX_VALUE) {
  240. int ky = minPath[0][0], kx = minPath[0][1];
  241. //进行路径的显示连接在窗口上
  242. for (int i = 1; i <= minStep; i++) {
  243. g.drawLine(50 * kx + 25, 50 * ky + 25, minPath[i][1] * 50 + 25, minPath[i][0] * 50 + 25);
  244. ky = minPath[i][0];
  245. kx = minPath[i][1];
  246. }
  247. try {
  248. Thread.sleep(1000);
  249. } catch (InterruptedException ex) {
  250. throw new RuntimeException(ex);
  251. }
  252. map[y][x] = 0;
  253. map[selectedPoint.y][selectedPoint.x] = 0;
  254. count -= 2;
  255. nowVersion = System.currentTimeMillis();
  256. }
  257. }
  258. //不管是否可连,都需要取消当前选中
  259. isSelected = false;
  260. //重绘画板
  261. repaint();
  262. //发现当前无剩余图片就弹出提醒:游戏结束
  263. if (count == 0) {
  264. JOptionPane.showMessageDialog(parentFrame, "游戏结束!", "success", JOptionPane.INFORMATION_MESSAGE);
  265. }
  266. }
  267. }
  268. }
  269. }
  270. });
  271. }
  272. /**
  273. * 绘制地图
  274. *
  275. * @param g the <code>Graphics</code> context in which to paint
  276. */
  277. @Override
  278. public void paint(Graphics g) {
  279. for (int i = 0; i < LENGTH; i++) {
  280. for (int j = 0; j < LENGTH; j++) {
  281. g.drawImage(images[map[i][j]], 50 * j, 50 * i, 50, 50, null);
  282. }
  283. }
  284. }
  285. /**
  286. * 判断是否可以直连
  287. *
  288. * @param x1 当前位置点的横坐标
  289. * @param y1 当前位置点的纵坐标
  290. * @param x2 目标位置点的横坐标
  291. * @param y2 目标位置点的纵坐标
  292. * @return 是否可连
  293. */
  294. public boolean isLineLink(int x1, int y1, int x2, int y2) {
  295. if (x1 == x2) {
  296. int minY = Math.min(y1, y2) + 1;
  297. int maxY = Math.max(y1, y2);
  298. while (minY < maxY) {
  299. if (map[minY][x1] != 0) {
  300. return false;
  301. }
  302. minY++;
  303. }
  304. return true;
  305. } else if (y1 == y2) {
  306. int minX = Math.min(x1, x2) + 1;
  307. int maxX = Math.max(x1, x2);
  308. while (minX < maxX) {
  309. if (map[y2][minX] != 0) {
  310. return false;
  311. }
  312. minX++;
  313. }
  314. return true;
  315. } else {
  316. return false;
  317. }
  318. }
  319. /**
  320. * 判断是否可以通过一次折点连通
  321. *
  322. * @param x1 当前位置点的横坐标
  323. * @param y1 当前位置点的纵坐标
  324. * @param x2 目标位置点的横坐标
  325. * @param y2 目标位置点的纵坐标
  326. * @param g 画笔
  327. * @return 是否可连
  328. */
  329. public boolean isLinkByOne(int x1, int y1, int x2, int y2, Graphics g) {
  330. g.setColor(Color.RED);
  331. if (isLineLink(x1, y1, x1, y2) && map[y2][x1] == 0 && isLineLink(x1, y2, x2, y2)) {
  332. g.drawLine(x1 * 50 + 25, y1 * 50 + 25, x1 * 50 + 25, y2 * 50 + 25);
  333. g.drawLine(x1 * 50 + 25, y2 * 50 + 25, x2 * 50 + 25, y2 * 50 + 25);
  334. return true;
  335. } else if (isLineLink(x1, y1, x2, y1) && map[y1][x2] == 0 && isLineLink(x2, y1, x2, y2)) {
  336. g.drawLine(x1 * 50 + 25, y1 * 50 + 25, x2 * 50 + 25, y1 * 50 + 25);
  337. g.drawLine(x2 * 50 + 25, y1 * 50 + 25, x2 * 50 + 25, y2 * 50 + 25);
  338. return true;
  339. }
  340. return false;
  341. }
  342. /**
  343. * 深度优先搜索
  344. * 判断是否可以通过两次折点连通 - 如果存在,在此基础上得到最短路径
  345. *
  346. * @param x1 当前位置点的横坐标
  347. * @param y1 当前位置点的纵坐标
  348. * @param x2 目标位置点的横坐标
  349. * @param y2 目标位置点的纵坐标
  350. * @param inflectionPointNum 当前折点的次数
  351. * @param direction 当前方向:无(-1) 右(0) 下(1) 左(2) 右(3)
  352. * @param step 当前走的步数
  353. */
  354. public void isLinkByTwo(int x1, int y1, int x2, int y2, int inflectionPointNum, int direction, int step) {
  355. //发现折点次数达到三次就返回,表示路径不行
  356. if (inflectionPointNum == 3) {
  357. return;
  358. }
  359. //记录该点位置到路径数组中
  360. tempPath[step][0] = y1;
  361. tempPath[step][1] = x1;
  362. //查看是否到达目标点
  363. if (x1 == x2 && y1 == y2) {
  364. //说明找到了更短的,就将这个更短路径放入到最短路径数组中存储
  365. if (step < minStep) {
  366. minStep = step;
  367. for (int i = 0; i <= minStep; i++) {
  368. minPath[i][0] = tempPath[i][0];
  369. minPath[i][1] = tempPath[i][1];
  370. }
  371. }
  372. return;
  373. }
  374. //标记当前点已经访问
  375. isVisited[y1][x1] = 1;
  376. int tx;
  377. int ty;
  378. //尝试不同的方向
  379. for (int i = 0; i < 4; i++) {
  380. tx = x1 + dx[i];
  381. ty = y1 + dy[i];
  382. /*
  383. 1.
  384. tx >= 0 && tx < LENGTH && ty >= 0 && ty < LENGTH 保证不越界
  385. map[ty][tx] == 0 保证是空白区
  386. isVisited[ty][tx] == 0 保证是未访问的
  387. 2.
  388. tx == x2 && ty == y2 说明是目标点
  389. */
  390. if (tx >= 0 && tx < LENGTH && ty >= 0 && ty < LENGTH && map[ty][tx] == 0 && isVisited[ty][tx] == 0 || tx == x2 && ty == y2) {
  391. //标记即将查找的点为 已访问
  392. isVisited[ty][tx] = 1;
  393. if (direction == -1) {
  394. //如果无方向,这是第一次深度搜索的状态
  395. isLinkByTwo(tx, ty, x2, y2, inflectionPointNum, i, step + 1);
  396. } else if (i != direction) {
  397. //如果即将走的点的方向与原来行走的点方向不一致
  398. isLinkByTwo(tx, ty, x2, y2, inflectionPointNum + 1, i, step + 1);
  399. } else {
  400. //如果方向一致:可以与第一个if的代码合并,但为了层次更为清晰,这里分开写
  401. isLinkByTwo(tx, ty, x2, y2, inflectionPointNum, i, step + 1);
  402. }
  403. //访问完毕,回溯回来就标记为未访问,给后面的点来查找
  404. isVisited[ty][tx] = 0;
  405. }
  406. }
  407. }
  408. /**
  409. * 初始化最短步数和访问数组(都标记为未访问)
  410. */
  411. public void fillZeroAndSetMinPath() {
  412. for (int i = 0; i < LENGTH; i++) {
  413. Arrays.fill(isVisited[i], 0);
  414. }
  415. minStep = Integer.MAX_VALUE;
  416. }
  417. /**
  418. * 初始化判断死局的临时地图的访问数组中的位置都为未访问
  419. */
  420. public void fillZeroForThread() {
  421. for (int i = 0; i < LENGTH; i++) {
  422. Arrays.fill(isVisitedForThread[i], 0);
  423. }
  424. }
  425. /**
  426. * 交换地图上的两个点的图片
  427. *
  428. * @param x1 第一个点的横坐标
  429. * @param y1 第一个点的纵坐标
  430. * @param x2 第二个点的横坐标
  431. * @param y2 第二个点的横坐标
  432. * @param map 地图
  433. */
  434. public void swap(int x1, int y1, int x2, int y2, int[][] map) {
  435. temp = map[y1][x1];
  436. map[y1][x1] = map[y2][x2];
  437. map[y2][x2] = temp;
  438. }
  439. /**
  440. * 初始化地图
  441. */
  442. public void initMap() {
  443. //初始化 10 * 10 的游戏界面,初始值为 0
  444. map = new int[LENGTH][LENGTH];
  445. runMap = new int[LENGTH][LENGTH];
  446. isVisited = new int[LENGTH][LENGTH];
  447. isVisitedForThread = new int[LENGTH][LENGTH];
  448. Random r = new Random();
  449. int randomNum;
  450. //随机数成对连续布局
  451. for (int i = 1; i <= LENGTH - 2; i++) {
  452. for (int j = 1; j <= LENGTH - 2; j++) {
  453. randomNum = r.nextInt(RANDOM_NUM) + 1;
  454. map[i][j] = randomNum;
  455. map[i][++j] = randomNum;
  456. }
  457. }
  458. //打乱图片
  459. shuffle(map);
  460. //重置当前剩余图片数量
  461. count = (LENGTH - 2) * (LENGTH - 2);
  462. //取消选中
  463. isSelected = false;
  464. //修改版本号
  465. nowVersion = System.currentTimeMillis();
  466. }
  467. /**
  468. * 用于监听地图是否死局
  469. */
  470. @Override
  471. public void run() {
  472. //死循环判断,除非程序退出
  473. while (true) {
  474. try {
  475. //每隔五秒判断一次地图状态
  476. Thread.sleep(5000);
  477. } catch (InterruptedException e) {
  478. throw new RuntimeException(e);
  479. }
  480. //获取到当前的版本号
  481. long version = nowVersion;
  482. //拷贝一份当前地图放入到临时地图
  483. for (int i = 1; i <= LENGTH - 2; i++) {
  484. for (int j = 1; j <= LENGTH - 2; j++) {
  485. runMap[i][j] = map[i][j];
  486. }
  487. }
  488. //查看是否有可连点
  489. boolean isFind = pointSearch();
  490. //如果没有找到并且地图上还有点并且版本号还是原来的说明是原地图,就显示死局
  491. if (!isFind && count != 0 && version == nowVersion) {
  492. JOptionPane.showMessageDialog(parentFrame, "当前死局,将进行洗牌", "提醒", JOptionPane.PLAIN_MESSAGE);
  493. //进行洗牌,直到有可连点
  494. while (!isFind&& version == nowVersion) {
  495. //洗牌
  496. shuffle(map);
  497. //获取临时地图
  498. for (int i = 1; i <= LENGTH - 2; i++) {
  499. for (int j = 1; j <= LENGTH - 2; j++) {
  500. runMap[i][j] = map[i][j];
  501. }
  502. }
  503. isFind = pointSearch();
  504. }
  505. //洗牌完毕,重绘地图
  506. if(version == nowVersion) {
  507. repaint();
  508. nowVersion = System.currentTimeMillis();
  509. }
  510. }
  511. }
  512. }
  513. /**
  514. * 对 map 地图进行洗牌,随机交换打乱位置
  515. * @param map 地图
  516. */
  517. public void shuffle(int[][] map) {
  518. int tx, ty;
  519. int num = 0;
  520. Random r = new Random();
  521. while (num++ < LENGTH) {
  522. for (int y = 1; y <= LENGTH - 2; y++) {
  523. for (int x = 1; x <= LENGTH - 2; x++) {
  524. if(map[y][x]!=0) {
  525. tx = r.nextInt(LENGTH - 2) + 1;
  526. ty = r.nextInt(LENGTH - 2) + 1;
  527. if(map[ty][tx]!=0) {
  528. swap(tx, ty, x, y, map);
  529. }
  530. }
  531. }
  532. }
  533. }
  534. }
  535. /**
  536. * 搜索是否存在点可连
  537. *
  538. * @return 为true说明存在,false不存在
  539. */
  540. public boolean pointSearch() {
  541. boolean isFind = false;
  542. for (int i = 1; i <= LENGTH - 2; i++) {
  543. for (int j = 1; j <= LENGTH - 2; j++) {
  544. if (runMap[i][j] != 0) {
  545. fillZeroForThread();
  546. isFind = search(j, i, runMap[i][j], 0, -1, 0);
  547. }
  548. if (isFind) {
  549. return true;
  550. }
  551. }
  552. }
  553. return false;
  554. }
  555. /**
  556. * 深度优先搜索
  557. * 搜索有没有值为value并与最初点可连的点
  558. *
  559. * @param x1 当前点的横坐标
  560. * @param y1 当前点的纵坐标
  561. * @param value 最初点的值
  562. * @param inflectionPointNum 拐点个数
  563. * @param direction 当前方向
  564. * @param step 当前步数
  565. * @return 是否找到
  566. */
  567. public boolean search(int x1, int y1, int value, int inflectionPointNum, int direction, int step) {
  568. if (inflectionPointNum == 3) {
  569. return false;
  570. }
  571. /*
  572. 步数不为0是保证与刚开始的搜索不会冲突,否则刚已进入最初的搜索就会表明找到
  573. */
  574. if (value == runMap[y1][x1] && step != 0) {
  575. //说明找到了一条
  576. return true;
  577. }
  578. isVisitedForThread[y1][x1] = 1;
  579. int tx;
  580. int ty;
  581. boolean isFind = false;
  582. //尝试不同的方向
  583. for (int i = 0; i < 4 && !isFind; i++) {
  584. tx = x1 + dx[i];
  585. ty = y1 + dy[i];
  586. /*
  587. tx >= 0 && tx < LENGTH && ty >= 0 && ty < LENGTH && isVisitedForThread[ty][tx] == 0 不越界并且未被访问
  588. runMap[ty][tx] == 0 临时地图的该坐标为null则可走
  589. value == runMap[ty][tx] 临时地图的该坐标就为目标相等值则可走
  590. */
  591. if (tx >= 0 && tx < LENGTH && ty >= 0 && ty < LENGTH && isVisitedForThread[ty][tx] == 0 && (runMap[ty][tx] == 0 || value == runMap[ty][tx])) {
  592. isVisitedForThread[ty][tx] = 1;
  593. if (direction == -1) {
  594. isFind = search(tx, ty, value, inflectionPointNum, i, step + 1);
  595. } else if (i != direction) {
  596. isFind = search(tx, ty, value, inflectionPointNum + 1, i, step + 1);
  597. } else {
  598. isFind = search(tx, ty, value, inflectionPointNum, i, step + 1);
  599. }
  600. isVisitedForThread[ty][tx] = 0;
  601. }
  602. }
  603. return isFind;
  604. }
  605. }

发表评论

表情:
评论列表 (有 0 条评论,135人围观)

还没有评论,来说两句吧...

相关阅读

    相关 javascript 代码注释规范示例

    文件注释 文件注释位于文件的最前面,应包括文件的以下信息:概要说明及版本(必须)项目地址(开源组件必须)版权声明(必须)开源协议(开源组件必须)版本号(必须)修改时间(必须

    相关 连连

    Problem Description “连连看”相信很多人都玩过。没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子。如果某两个相同的棋子,可以通过