游戏棋 闲暇空余和朋友、陪自己玩耍的居家出行利器

释放双眼,带上耳机,听听看~!
00:00
00:00
首先,我们要做的就是打开你的(或者其他的java编译软件,当然这些都是废话),之后我们就要开始制作五子棋的第一步:制作一个界面,制作界面的方法相信不用我多说,还不会制作界面的朋友可以去看我以前的文章。

花了一个多星期才终于完善了我所有的五子棋。接下来给大家分享一款可以和朋友一起玩,闲暇时间陪你一起玩的居家旅行工具——五子棋。 (对了,我分享的不是游戏,而是游戏的制作方法)

一、制作接口

首先我们要做的就是打开你的(或者其他java编译软件,当然这些都是废话),然后我们开始制作五子棋的第一步:制作界面,制作方法接口相信没必要我多说,不知道怎么做接口的朋友可以看我之前的文章。代码直接附在这里:

  1. public void showUI() {
  2. // 窗体对象,JFrame默认是边框布局
  3. JFrame jf = new JFrame();
  4. jf.setTitle("五子棋1.0");
  5. jf.setSize(800, 700);
  6. // 设置退出进程的方法
  7. jf.setDefaultCloseOperation(3);
  8. // 设置居中显示
  9. jf.setLocationRelativeTo(null);
  10. GamePanel gp = new GamePanel(chessArray);
  11. //设置棋盘背景色
  12. gp.setBackground(new Color(139,139,122));
  13. jf.add(gp,BorderLayout.CENTER);
  14. //设置东边的属性
  15. JPanel jp=new JPanel();
  16. jp.setBackground(new Color(245,245,245));
  17. jf.add(jp,BorderLayout.EAST);
  18. //设置东边流式布局的属性
  19. java.awt.FlowLayout fl=new java.awt.FlowLayout(5,5,60);
  20. jp.setLayout(fl);
  21. Dimension di=new Dimension(120,0);
  22. jp.setPreferredSize(di);
  23. //设置东边的按钮及按钮的属性
  24. javax.swing.JButton jbu1=new javax.swing.JButton("开始游戏");
  25. Dimension di1=new Dimension(100,60);
  26. jbu1.setPreferredSize(di1);
  27. jp.add(jbu1);
  28. javax.swing.JButton jbu2=new javax.swing.JButton("人人对战");
  29. Dimension di2=new Dimension(100,60);
  30. jbu2.setPreferredSize(di2);
  31. jp.add(jbu2);
  32. javax.swing.JButton jbu3=new javax.swing.JButton("人机对战");
  33. Dimension di3=new Dimension(100,60);
  34. jbu3.setPreferredSize(di3);
  35. jp.add(jbu3);
  36. javax.swing.JButton jbu4=new javax.swing.JButton("悔棋");
  37. Dimension di4=new Dimension(100,60);
  38. jbu4.setPreferredSize(di4);
  39. jp.add(jbu4);
  40. javax.swing.JButton jbu5=new javax.swing.JButton("重新开始");
  41. Dimension di5=new Dimension(100,60);
  42. jbu5.setPreferredSize(di5);
  43. jp.add(jbu5);
  44. jf.setVisible(true);

顺便说一句,我们这里使用的不再是我们之前使用的流式布局。这里使用的布局就是布局,(即边框布局)。我们通常使用的默认是边框布局。评论也写了。虽然不是流式布局,但是我们可以将边框布局的block设置成我们想要的布局。我这里设置的是流布局。详情见代码。

二、创建按钮和监听器

界面完成后,我们要做的就是创建我们需要的按钮和按钮的监听器:

  1. //设置按钮的动作监听器
  2. jbu1.addActionListener(mouse);
  3. jbu2.addActionListener(mouse);
  4. jbu3.addActionListener(mouse);
  5. jbu4.addActionListener(mouse);
  6. jbu5.addActionListener(mouse);

然后在另一个类中创建一个监听方法:

  1. public void mouseClicked(MouseEvent e) {
  2. x1 = e.getX();
  3. y1 = e.getY();
  4. if (z == 1) {
  5. rr();
  6. }
  7. if (z == 2) {
  8. rj();
  9. gameWin(xx, yy);
  10. try {
  11. Thread.sleep(350);
  12. }catch(Exception ef) {}
  13. a.ai();
  14. gameWin(a.getmaxi(), a.getmaxj());
  15. }
  16. }
  17. // 对设置的按钮进行功能实现
  18. public int z;
  19. public void actionPerformed(ActionEvent e) {
  20. System.out.println("str" + e.getActionCommand());
  21. if (e.getActionCommand().equals("开始游戏")) {
  22. j.addMouseListener(this);
  23. }
  24. if (e.getActionCommand().equals("人人对战")) {
  25. z = 1;
  26. }
  27. if (e.getActionCommand().equals("人机对战")) {
  28. a = new AI(g, chessArray, j);
  29. z = 2;
  30. }
  31. if (e.getActionCommand().equals("悔棋")) {
  32. System.out.println("2");
  33. for (int i = 0; i < LINE; i++) {
  34. for (int j = 0; j < LINE; j++) {
  35. chessArray[xx][yy] = 0;
  36. count = 0;
  37. }
  38. }
  39. j.repaint();
  40. }
  41. if (e.getActionCommand().equals("重新开始")) {
  42. System.out.println("1");
  43. // 调用repaint清空所有的棋子并使每个下过的点可以再下
  44. for (int i = 0; i < LINE; i++) {
  45. for (int j = 0; j < LINE; j++) {
  46. chessArray[i][j] = 0;
  47. count = 0;
  48. }
  49. }
  50. j.repaint();
  51. }
  52. }

这里可以清楚的看到我设置的按钮和按钮方法的实现。因为我比较懒,先一起贴吧。之后游戏棋,我们将讨论按钮的功能实现。现在我只是告诉你创建一个监听器。 .

三、所有人对所有人

完成按键的监控,接下来我们将实现五子棋中最简单的可以让人娱乐​​的部分,也就是大家部分(和自己下棋,或者和朋友在同一台电脑下棋)。要完成大家的部分,我们首先要考虑的是如何搭建棋盘,如何让棋子落在棋盘线的交点上,如何让黑白交替出现。要做到这一点,我们首先要确定棋盘的宽度、高度和棋盘之间的间隔以及棋子的大小都是由你自己设定的。当然,为了让棋盘更加匀称美观,这些数据都是我亲测过的,大家也可以试试:

棋盘:

  1. for(int i=0;i<LINE;i++){
  2. g.drawLine(X, Y+i*SIZE, (LINE-1)*SIZE+X, Y+i*SIZE);
  3. g.drawLine(X+i*SIZE, Y, X+i*SIZE, (LINE-1)*SIZE+Y);
  4. }

控制棋盘的交点和黑白交替:

  1. if ((x1 - X) % SIZE > SIZE / 2) {
  2. xx = (x1 - X) / SIZE + 1;
  3. } else {
  4. xx = (x1 - X) / SIZE;
  5. }
  6. if ((y1 - Y) % SIZE > SIZE / 2) {
  7. yy = (y1 - Y) / SIZE + 1;
  8. } else {
  9. yy = (y1 - Y) / SIZE;
  10. }
  11. // 控制棋子的黑白色交替出现
  12. if (chessArray[xx][yy] == 0) {
  13. if (count == 1) {
  14. g.setColor(Color.WHITE);
  15. chessArray[xx][yy] = 1;
  16. count--;
  17. } else {
  18. g.setColor(Color.BLACK);
  19. chessArray[xx][yy] = -1;
  20. count++;
  21. }
  22. g.fillOval(xx * SIZE + X - CHESS / 2, yy * SIZE + Y - CHESS / 2,CHESS, CHESS);
  23. }

做完这件事后,还要考虑输赢的情况。这也是大家最复杂的部分,未来人类和机器也会用到。整个五子棋的思路和精髓其实在于五子的判断:

  1. // 判断黑棋和白棋的获胜方式
  2. // 行
  3. public int checkrow(int x, int y) {
  4. int count1 = 0;// 存放连续的相同颜色的棋子数
  5. for (int i = x + 1; i < 15; i++) {
  6. if (chessArray[Math.abs(i)][y] == chessArray[x][y]) {
  7. count1++;
  8. System.out.println(count1);
  9. } else {
  10. break;
  11. }
  12. }
  13. for (int i = x; i >= 0; i--) {
  14. if (chessArray[Math.abs(i)][y] == chessArray[x][y]) {
  15. count1++;
  16. } else {
  17. break;
  18. }
  19. }
  20. return count1;
  21. }
  22. // 列
  23. public int checkcolumn(int x, int y) {
  24. int count1 = 0;// 存放连续的相同颜色的棋子数
  25. for (int i = y + 1; i < 15; i++) {
  26. if (chessArray[x][Math.abs(i)] == chessArray[x][y]) {
  27. count1++;
  28. } else {
  29. break;
  30. }
  31. }
  32. for (int i = y; i >= 0; i--) {
  33. if (chessArray[x][Math.abs(i)] == chessArray[x][y]) {
  34. count1++;
  35. } else {
  36. break;
  37. }
  38. }
  39. return count1;
  40. }
  41. // 斜
  42. public int checkinclined1(int x, int y) {
  43. int count1 = 0;// 存放连续的相同颜色的棋子数
  44. for (int i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--) {
  45. if (chessArray[i][j] == chessArray[x][y]) {
  46. count1++;
  47. System.out.println(count1);
  48. } else {
  49. break;
  50. }
  51. }
  52. for (int i = x + 1, j = y + 1; i < LINE && j < LINE; i++, j++) {
  53. if (chessArray[i][j] == chessArray[x][y]) {
  54. count1++;
  55. } else {
  56. break;
  57. }
  58. }
  59. return count1;
  60. }
  61. public int checkinclined2(int x, int y) {
  62. int count1 = 0;
  63. for (int i = x - 1, j = y + 1; i >= 0 && j < LINE; i--, j++) {
  64. if (chessArray[i][j] == chessArray[x][y]) {
  65. count1++;
  66. } else {
  67. break;
  68. }
  69. }
  70. for (int i = x + 1, j = y - 1; i < LINE && j >= 0; i++, j--) {
  71. if (chessArray[i][j] == chessArray[x][y]) {
  72. count1++;
  73. } else {
  74. break;
  75. }
  76. }
  77. return count1;
  78. }
  79. public void gameWin(int x,int y){
  80. if(checkinclined1(x, y)>=4||checkinclined2(x,y)>=4||checkcolumn(x,y)>=5||checkrow(x,y)>=5){
  81. if(chessArray[x][y] == -1){
  82. hwin();
  83. }else if(chessArray[x][y] == 1){
  84. bwin();
  85. }
  86. }
  87. }

这是判断五子棋胜负的一路。

完成大家判断走法、黑白交替、输赢的判断。相信大家已经对如何制作步步高供大家玩有一个基本的想法了。

四、人机大战

在完成了大家战斗的功能之后,接下来要做的就是我们简单的人工智能AI了。 AI在我们现在的生活中非常有名。相信大家一定对五子棋的AI很感兴趣吧,虽然五子棋的AI听起来很棒,但是并没有大家想象的那么复杂。我在这里没有使用任何特别强大的方法。我只是使用了一个哈希表并使用权重来让 AI 判断。达到我们简单人工智能的目的。首先我们需要创建一个权重表来存储不同棋子的权重,这里黑色是-1,白色是1:

  1. HashMap<String, Integer> hm = new HashMap<String, Integer>();
  2. public AI(Graphics g, int[][] chessArray, JPanel j) {
  3. this.g = g;
  4. this.chessArray = chessArray;
  5. this.j = j; // 设置不同情况时的权值
  6. hm.put("-1", 20);
  7. hm.put("-1-1", 400);
  8. hm.put("-1-1-1", 420);
  9. hm.put("-1-1-1-1", 3000);
  10. hm.put("-11", 4);
  11. hm.put("-1-11", 40);
  12. hm.put("-1-1-11", 400);
  13. hm.put("-1-1-1-11", 10000);
  14. hm.put("1-1-1-1-1", 10000);
  15. hm.put("1", 8);
  16. hm.put("11", 80);
  17. hm.put("111", 1000);
  18. hm.put("1111", 5000);
  19. hm.put("1111-1", 5000);
  20. hm.put("1111-1-1", 5000);
  21. hm.put("1-1", 6);
  22. hm.put("11-1", 60);
  23. hm.put("111-1", 600);
  24. hm.put("-11-1", 5);
  25. hm.put("-111-1", 5);
  26. hm.put("1-1-11", 5);
  27. hm.put("1-11", 5);
  28. }

这样,一个简单的权重表就完成了。事实上,这个重量表并不是很准确。可能有很多数值没有设置好,但作为一个简单的AI,还是可以和普通人一起玩的。建立哈希表并设置权重后,我们需要将需要的值存储在权重中。这里我们需要int类型,所以我们有如下代码来存储哈希表需要的数据:

  1. public void ai() {
  2. for (int i = 0; i < LINE; i++) {
  3. for (int j = 0; j < LINE; j++) {
  4. if (chessArray[i][j] == 0) {
  5. String code = "";
  6. int color = 0;
  7. // 向右
  8. for (int k = i + 1; k < 15; k++) {
  9. if (chessArray[Math.abs(k)][j] == 0) {
  10. break;
  11. } else {
  12. if (color == 0) { // 右边第一颗棋子
  13. color = chessArray[Math.abs(k)][j]; // 保存颜色
  14. code += chessArray[Math.abs(k)][j]; // 保存棋子相连情况
  15. } else if (chessArray[Math.abs(k)][j] == color) {
  16. code += chessArray[Math.abs(k)][j]; // 保存棋子相连情况
  17. } else {
  18. code += chessArray[Math.abs(k)][j]; // 保存棋子相连情况
  19. break;
  20. }
  21. }
  22. if (chessArray[i][j] != 0) {
  23. chessValue[i][j] = 0;
  24. }
  25. }
  26. Integer value = hm.get(code);
  27. if (value != null) {
  28. chessValue[i][j] += value;
  29. }
  30. if (value != null) {
  31. 游戏棋 闲暇空余和朋友、陪自己玩耍的居家出行利器

    游戏棋 闲暇空余和朋友、陪自己玩耍的居家出行利器

  32. chessValue[i][j] += value;
  33. }
  34. // 向左
  35. code = "";
  36. color = 0;
  37. for (int k = i - 1; k >= 0; k--) {
  38. if (chessArray[Math.abs(k)][j] == 0) {
  39. break;
  40. } else {
  41. if (color == 0) { // 左边第一颗棋子
  42. color = chessArray[Math.abs(k)][j]; // 保存颜色
  43. code += chessArray[Math.abs(k)][j]; // 保存棋子相连情况
  44. } else if (chessArray[Math.abs(k)][j] == color) {
  45. code += chessArray[Math.abs(k)][j]; // 保存棋子相连情况
  46. } else {
  47. code += chessArray[Math.abs(k)][j]; // 保存棋子相连情况
  48. break;
  49. }
  50. }
  51. }
  52. value = hm.get(code);
  53. if (value != null) {
  54. chessValue[i][j] += value;
  55. }
  56. if (chessArray[i][j] != 0) {
  57. chessValue[i][j] = 0;
  58. }
  59. // 向上
  60. code = "";
  61. color = 0;
  62. for (int k = j - 1; k >= 0; k--) {
  63. if (chessArray[i][Math.abs(k)] == 0) {
  64. break;
  65. } else {
  66. if (color == 0) { // 左边第一颗棋子
  67. color = chessArray[i][Math.abs(k)]; // 保存颜色
  68. code += chessArray[i][Math.abs(k)]; // 保存棋子相连情况
  69. } else if (chessArray[i][Math.abs(k)] == color) {
  70. code += chessArray[i][Math.abs(k)]; // 保存棋子相连情况
  71. } else {
  72. code += chessArray[i][Math.abs(k)]; // 保存棋子相连情况
  73. break;
  74. }
  75. }
  76. }
  77. value = hm.get(code);
  78. if (value != null) {
  79. chessValue[i][j] += value;
  80. }
  81. if (chessArray[i][j] != 0) {
  82. chessValue[i][j] = 0;
  83. }
  84. // 向下
  85. code = "";
  86. color = 0;
  87. for (int k = j + 1; k < LINE; k++) {
  88. if (chessArray[i][Math.abs(k)] == 0) {
  89. break;
  90. } else {
  91. if (color == 0) { // 左边第一颗棋子
  92. color = chessArray[i][Math.abs(k)]; // 保存颜色
  93. code += chessArray[i][Math.abs(k)]; // 保存棋子相连情况
  94. } else if (chessArray[i][Math.abs(k)] == color) {
  95. code += chessArray[i][Math.abs(k)]; // 保存棋子相连情况
  96. } else {
  97. code += chessArray[i][Math.abs(k)]; // 保存棋子相连情况
  98. break;
  99. }
  100. }
  101. }
  102. value = hm.get(code);
  103. if (value != null) {
  104. chessValue[i][j] += value;
  105. }
  106. if (chessArray[i][j] != 0) {
  107. chessValue[i][j] = 0;
  108. }
  109. // 右下
  110. code = "";
  111. color = 0;
  112. for (int k = i + 1, l = j + 1; k < LINE || l < LINE; k++, l++) {
  113. if (chessArray[Math.abs(k)][Math.abs(l)] == 0) {
  114. break;
  115. } else {
  116. if (color == 0) { // 左边第一颗棋子
  117. color = chessArray[Math.abs(k)][Math.abs(l)]; // 保存颜色
  118. code += chessArray[Math.abs(k)][Math.abs(l)]; // 保存棋子相连情况
  119. } else if (chessArray[Math.abs(k)][Math.abs(l)] == color) {
  120. code += chessArray[Math.abs(k)][Math.abs(l)]; // 保存棋子相连情况
  121. } else {
  122. code += chessArray[Math.abs(k)][Math.abs(l)]; // 保存棋子相连情况
  123. break;
  124. }
  125. }
  126. }
  127. value = hm.get(code);
  128. if (value != null) {
  129. chessValue[i][j] += value;
  130. }
  131. if (chessArray[i][j] != 0) {
  132. chessValue[i][j] = 0;
  133. }
  134. // 右上
  135. code = "";
  136. color = 0;
  137. for (int k = i + 1, l = j - 1; k < LINE || l >= 0; k++, l--) {
  138. if (chessArray[Math.abs(k)][Math.abs(l)] == 0) {
  139. break;
  140. } else {
  141. if (color == 0) { // 左边第一颗棋子
  142. color = chessArray[Math.abs(k)][Math.abs(l)]; // 保存颜色
  143. code += chessArray[Math.abs(k)][Math.abs(l)]; // 保存棋子相连情况
  144. } else if (chessArray[Math.abs(k)][Math.abs(l)] == color) {
  145. code += chessArray[Math.abs(k)][Math.abs(l)]; // 保存棋子相连情况
  146. } else {
  147. code += chessArray[Math.abs(k)][Math.abs(l)]; // 保存棋子相连情况
  148. break;
  149. }
  150. }
  151. }
  152. value = hm.get(code);
  153. if (value != null) {
  154. chessValue[i][j] += value;
  155. }
  156. if (chessArray[i][j] != 0) {
  157. chessValue[i][j] = 0;
  158. }
  159. // 左上
  160. code = "";
  161. color = 0;
  162. for (int k = i - 1, l = j - 1; k >= 0 || l >= 0; k--, l--) {
  163. if (chessArray[Math.abs(k)][Math.abs(l)] == 0) {
  164. break;
  165. } else {
  166. if (color == 0) { // 左边第一颗棋子
  167. color = chessArray[Math.abs(k)][Math.abs(l)]; // 保存颜色
  168. code += chessArray[Math.abs(k)][Math.abs(l)]; // 保存棋子相连情况
  169. } else if (chessArray[Math.abs(k)][Math.abs(l)] == color) {
  170. code += chessArray[Math.abs(k)][Math.abs(l)]; // 保存棋子相连情况
  171. } else {
  172. code += chessArray[Math.abs(k)][Math.abs(l)]; // 保存棋子相连情况
  173. break;
  174. }
  175. }
  176. }
  177. value = hm.get(code);
  178. if (value != null) {
  179. chessValue[i][j] += value;
  180. }
  181. if (chessArray[i][j] != 0) {
  182. chessValue[i][j] = 0;
  183. }
  184. // 左下
  185. code = "";
  186. color = 0;
  187. for (int k = i - 1, l = j + 1; k >= 0 || l < LINE; k--, l++) {
  188. if (chessArray[Math.abs(k)][Math.abs(l)] == 0) {
  189. break;
  190. } else {
  191. if (color == 0) { // 左边第一颗棋子
  192. color = chessArray[Math.abs(k)][Math.abs(l)]; // 保存颜色
  193. code += chessArray[Math.abs(k)][Math.abs(l)]; // 保存棋子相连情况
  194. } else if (chessArray[Math.abs(k)][Math.abs(l)] == color) {
  195. code += chessArray[Math.abs(k)][Math.abs(l)]; // 保存棋子相连情况
  196. } else {
  197. code += chessArray[Math.abs(k)][Math.abs(l)]; // 保存棋子相连情况
  198. break;
  199. }
  200. }
  201. }
  202. value = hm.get(code);
  203. if (value != null) {
  204. chessValue[i][j] += value;
  205. }
  206. if (chessArray[i][j] != 0) {
  207. chessValue[i][j] = 0;
  208. }
  209. }
  210. }
  211. }
  212. for (int j = 0; j < LINE; j++) {
  213. for (int i = 0; i < LINE; i++) {
  214. System.out.print(chessValue[i][j] + " ");
  215. }
  216. System.out.println();
  217. }
  218. for (int j = 0; j < LINE; j++) {
  219. for (int i = 0; i < LINE; i++) {
  220. System.out.print(chessArray[i][j] + " ");
  221. }
  222. System.out.println();
  223. }

其实这个方法和之前判断五子连线的方法思路是一样的,所以我会说五子连线的思路其实就是五子棋的精髓。 (设置最后两个for循环,检查权重错误和颜色错误。)五子棋步法判断完成后,就要控制五子棋计算机的步法了。这里需要注意的是:每次我们下完棋和电脑下完棋后,都要判断输赢。这里我们需要用我上面写的语句来判断输赢,而且每走一步之后,我们都要将每一个点的权重清零。移动的点是彩色的,不能放在下面,代码如下:

  1. // 判断权值最大的位置,并在该位置下棋。
  2. int maxv = 0;
  3. for (int i = 0; i < LINE; i++) {
  4. for (int j = 0; j < LINE; j++) {
  5. if (maxv < chessValue[i][j]) {
  6. maxv = chessValue[i][j];
  7. maxi = i;
  8. maxj = j;
  9. }
  10. }
  11. }
  12. //画白棋
  13. g.setColor(Color.WHITE);
  14. g.fillOval(maxi * SIZE + X - CHESS / 2, maxj * SIZE + Y - CHESS / 2,CHESS, CHESS);
  15. chessArray[maxi][maxj] = 1;
  16. System.out.println("x" + maxi + "y" + maxj);
  17. for (int i = 0; i < LINE; i++) {
  18. for (int j = 0; j < LINE; j++) {
  19. chessValue[i][j] = 0;
  20. }

完成这些方法后,我们只需要调用监听类中的方法就可以了。

五、完成所有按钮功能并重绘

完成以上所有步骤后,我们就来到了我们步步高的最后一步,也就是步步高按钮功能的实现。我的五子棋按钮不多游戏棋,只有五个,每个都有不同的功能。 ,如果要实现这5个按钮的功能,可以先看看有没有想法:

其实看起来并不难,对吧?我们来看看:其实我们在开始游戏的时候,是想让鼠标在我们的棋盘上停止工作,所以我把面板的监听器放在了按钮的监听器里面。这样就达到了效果。人和人机要调用不同的方法,但是需要注意的是,每次调用一个下棋的方法,都要判断一次胜负,然后后悔重新开始。事实上,原理是相似的。 ,就是调用()方法重绘棋盘中的棋子,从而达到完美忏悔重启的目的,代码:

  1. public int z;
  2. public void actionPerformed(ActionEvent e) {
  3. System.out.println("str" + e.getActionCommand());
  4. if (e.getActionCommand().equals("开始游戏")) {
  5. j.addMouseListener(this);
  6. }
  7. if (e.getActionCommand().equals("人人对战")) {
  8. z = 1;
  9. }
  10. if (e.getActionCommand().equals("人机对战")) {
  11. a = new AI(g, chessArray, j);
  12. z = 2;
  13. }
  14. if (e.getActionCommand().equals("悔棋")) {
  15. System.out.println("2");
  16. for (int i = 0; i < LINE; i++) {
  17. for (int j = 0; j < LINE; j++) {
  18. chessArray[xx][yy] = 0;
  19. count = 0;
  20. }
  21. }
  22. j.repaint();
  23. }
  24. if (e.getActionCommand().equals("重新开始")) {
  25. System.out.println("1");
  26. // 调用repaint清空所有的棋子并使每个下过的点可以再下
  27. for (int i = 0; i < LINE; i++) {
  28. for (int j = 0; j < LINE; j++) {
  29. chessArray[i][j] = 0;
  30. count = 0;
  31. }
  32. }
  33. j.repaint();
  34. }

完成按钮功能的实现,然后重绘。其实之前我已经告诉过你,重绘其实就是调用paint方法,然后重绘我们要重绘的东西。这里我们要重绘的东西是棋盘和棋子,所以我们要重新创建一个类并在里面重绘:

  1. public class GamePanel extends JPanel implements Config{
  2. private int[][] chessArray;
  3. public GamePanel(int[][] chessArray){
  4. this.chessArray = chessArray;
  5. }
  6. public void paint(Graphics g){
  7. super.paint(g);
  8. //重绘棋盘
  9. //硬编码
  10. for(int i=0;i<LINE;i++){
  11. g.drawLine(X, Y+i*SIZE, (LINE-1)*SIZE+X, Y+i*SIZE);
  12. g.drawLine(X+i*SIZE, Y, X+i*SIZE, (LINE-1)*SIZE+Y);
  13. }
  14. paintChess(g);
  15. }
  16. /**
  17. * 重绘棋子
  18. * @param g
  19. */
  20. public void paintChess(Graphics g){
  21. for(int i=0;i<chessArray.length;i++){
  22. for(int j=0;j<chessArray[i].length;j++){
  23. if(chessArray[i][j] == 1){
  24. g.setColor(Color.WHITE);
  25. g.fillOval(i * SIZE + X - CHESS / 2, j * SIZE + Y - CHESS / 2, CHESS, CHESS);
  26. }else if(chessArray[i][j] == -1){
  27. g.setColor(Color.BLACK);
  28. g.fillOval(i * SIZE + X - CHESS / 2, j * SIZE + Y - CHESS / 2, CHESS, CHESS);
  29. }
  30. }
  31. }
  32. }
  33. }

顺便说一句,你可能会很困惑。我在每个类中都调用了一些大写的常量,比如LINE、X、Y等。其实这在java编程中是很重要的东西,它让我们改变编码更容易,我们称之为“硬编码”,在其实硬编码就是重新定义一个接口,把我们要使用的数据存储在接口中,这样以后需要修改代码的时候,会减少很多。工作量很大,我这里为每个类都继承了这样一个接口:

  1. public interface Config {
  2. public static final int X = 50;
  3. public static final int Y = 40;
  4. public static final int LINE = 15;
  5. public static final int SIZE = 40;
  6. public static final int CHESS = 30;
  7. }

写到这里,我们的五子棋已经完成。相信大家已经了解了五子棋的制作方法,也有了自己制作一些小游戏的想法。如果您对我的五子棋有任何疑问或意见,可以评论与我讨论,我们下期再见! !

0 条回复 A文章作者 M管理员
欢迎您,新朋友,感谢参与互动!
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
私信列表
搜索