Java制作五子棋人机对战

电玩女神 2023-02-28 08:59 119阅读 0赞

目录

简介

游戏思路

源码

效果视频

代码片段


简介

Hello,I’m Shendi

继上次写完象棋后又花了一天时间写了个五子棋,比较简单

拥有大概如下功能

  1. 五子棋的基本功能
  2. 棋盘大小的自由设置
  3. 胜利棋数的自由设置
  4. 人机对战

游戏思路

五子棋相比其他棋牌游戏比较简单

我们判断胜负只需要在玩家/人机下棋处循环遍历

  • 上下是不是连起来到了指定点数
  • 左右…
  • 左斜
  • 右斜

源码

github: https://github.com/1711680493/Application#gobang


效果视频

Java制作五子棋/连珠棋+简单AI


代码片段

棋盘,棋子绘制代码

  1. /**
  2. * 重新绘制棋盘.
  3. * @author Shendi <a href='tencent://AddContact/?fromId=45&fromSubId=1&subcmd=all&uin=1711680493'>QQ</a>
  4. * @param chessBoardNum 棋盘大小
  5. */
  6. public void repaint(int chessBoardNum) {
  7. BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
  8. Graphics g = img.getGraphics();
  9. size = 380 / chessBoardNum;
  10. first = size >> 1;
  11. // 棋子大小初始化
  12. Game.getGame().getChesses().forEach((chess) -> chess.setSize(size,size));
  13. // 将棋盘上所有位置初始化
  14. positions.clear();
  15. for (int i = 0;i < chessBoardNum;i++) {
  16. positions.put(i, first + size * i);
  17. }
  18. // 绘制棋盘
  19. for (int i = 1;i < chessBoardNum;i++) {
  20. // 横线
  21. g.drawLine(size, size * i, 380, size * i);
  22. // 竖线
  23. g.drawLine(size * i, size, size * i, 380);
  24. }
  25. g.drawLine(size, 380 , 380, 380);
  26. g.drawLine(380, size, 380, 380);
  27. g.dispose();
  28. background.setIcon(new ImageIcon(img));
  29. invalidate();
  30. repaint();
  31. // 白色黑色棋子图
  32. int imgPos = (size - size / 2) >> 1;
  33. int imgSize = size >> 1;
  34. BufferedImage whiteChessImg = new BufferedImage(size,size,BufferedImage.TYPE_INT_ARGB);
  35. g = whiteChessImg.getGraphics();
  36. g.setColor(Color.WHITE);
  37. g.fillRoundRect(imgPos, imgPos, imgSize, imgSize, 180, 180);
  38. g.dispose();
  39. BufferedImage blackChessImg = new BufferedImage(size,size,BufferedImage.TYPE_INT_ARGB);
  40. g = blackChessImg.getGraphics();
  41. g.setColor(Color.BLACK);
  42. g.fillRoundRect(imgPos, imgPos, imgSize, imgSize, 180, 180);
  43. g.dispose();
  44. whiteChessIcon = new ImageIcon(whiteChessImg);
  45. blackChessIcon = new ImageIcon(blackChessImg);
  46. }

判断游戏逻辑代码

  1. /**
  2. * 执行下棋操作.
  3. * @author Shendi <a href='tencent://AddContact/?fromId=45&fromSubId=1&subcmd=all&uin=1711680493'>QQ</a>
  4. * @param chess 棋子
  5. * @param whiteBlack 白棋还是黑棋.
  6. */
  7. public void exec(JLabel chess,boolean whiteBlack) {
  8. // 指定位置是否有棋子,有不做操作
  9. int x = chessBoard.getScenePos(chess.getX());
  10. int y = chessBoard.getScenePos(chess.getY());
  11. if (scene[y][x] != 0) { return; }
  12. // 下棋
  13. int team = whiteBlack ? 1 : 2;
  14. scene[y][x] = team;
  15. // for (int[] xScene : scene) System.out.println(Arrays.toString(xScene));
  16. if (whiteBlack) chess.setIcon(chessBoard.whiteChessIcon);
  17. else chess.setIcon(chessBoard.blackChessIcon);
  18. // 判断胜负,横竖斜有指定数量棋子则胜利,对方失败
  19. int h = 1,v = 1,ld = 1,rd = 1;
  20. // 横,左右
  21. int posX,posY;
  22. for (int i = 1;i < chessBoardNum;i++) {
  23. posX = x - i;
  24. if (posX < 0) break;
  25. if (scene[y][posX] == team) h++; else break;
  26. }
  27. for (int i = 1;i < chessBoardNum;i++) {
  28. posX = x + i;
  29. if (posX >= chessBoardNum) break;
  30. if (scene[y][posX] == team) h++; else break;
  31. }
  32. // 竖,上下
  33. for (int i = 1;i < chessBoardNum;i++) {
  34. posY = y - i;
  35. if (posY < 0) break;
  36. if (scene[posY][x] == team) v++; else break;
  37. }
  38. for (int i = 1;i < chessBoardNum;i++) {
  39. posY = y + i;
  40. if (posY >= chessBoardNum) break;
  41. if (scene[posY][x] == team) v++; else break;
  42. }
  43. // 左斜,上左下右 \
  44. for (int i = 1;i < chessBoardNum;i++) {
  45. posX = x - i;
  46. posY = y - i;
  47. if (posX < 0 || posY < 0) break;
  48. if (scene[posY][posX] == team) ld++; else break;
  49. }
  50. for (int i = 1;i < chessBoardNum;i++) {
  51. posX = x + i;
  52. posY = y + i;
  53. if (posX >= chessBoardNum || posY >= chessBoardNum) break;
  54. if (scene[posY][posX] == team) ld++; else break;
  55. }
  56. // 右斜,上右下左 /
  57. for (int i = 1;i < chessBoardNum;i++) {
  58. posX = x + i;
  59. posY = y - i;
  60. if (posX >= chessBoardNum || posY < 0) break;
  61. if (scene[posY][posX] == team) rd++; else break;
  62. }
  63. for (int i = 1;i < chessBoardNum;i++) {
  64. posX = x - i;
  65. posY = y + i;
  66. if (posX < 0 || posY >= chessBoardNum) break;
  67. if (scene[posY][posX] == team) rd++; else break;
  68. }
  69. if (h >= vectoryNum || v >= vectoryNum || ld >= vectoryNum || rd >= vectoryNum) {
  70. stop(whiteBlack);
  71. return;
  72. }
  73. // 当前角色下棋完成,换一边
  74. currentTeam = !currentTeam;
  75. if (!currentTeam) AI.getAI().play();
  76. }

发表评论

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

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

相关阅读

    相关 五子棋人机设计

    一、人机对战算法概述 人机对战属于一种弱人工智能算法,其核心是:当玩家落下一枚棋子后,计算出这枚棋子构成的所有棋型,找出威胁程度最大的棋型,并破解其产生的威胁。 五子棋中所