1.制作五子棋游戏软件

由于老师已经基本做完了。重做的时候快了非常多……可是还是感觉思维非常混乱…… 哪边先哪边后,哪个方法在哪边好之类的问题 太纠结了……

先是窗体 内部类:鼠标适配器  窗体的构造器  画图

  1. package com.lovo.homework2;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Image;
  6. import java.awt.event.MouseAdapter;
  7. import java.awt.event.MouseEvent;
  8. import java.awt.image.BufferedImage;
  9.  
  10. import javax.swing.JFrame;
  11. import javax.swing.JOptionPane;
  12.  
  13. /**
  14. * 类 : 我的五子棋窗体
  15. *
  16. * @author Abe
  17. */
  18. public class MyFrameRenju extends JFrame {
  19. private MyboardRenju board = new MyboardRenju();
  20. private boolean isBlack = true;
  21. private Image offImage = new BufferedImage(800, 800,
  22. BufferedImage.TYPE_INT_RGB);// 双缓冲
  23. private boolean isGameOver = false;
  24.  
  25. /**
  26. * 内部类:鼠标适配器
  27. *
  28. * @author Abe
  29. */
  30. public class MyMouseAdapter extends MouseAdapter {
  31. @Override
  32. public void mousePressed(MouseEvent e) { // 重写点击鼠标的方法
  33. if (!isGameOver) {
  34. int x = e.getX();
  35. int y = e.getY();
  36. if (x > 25 && x < 775 && y > 25 && y < 775) {
  37. int i = (x - 25) / 50;
  38. int j = (y - 25) / 50;
  39. if (board.move(i, j, isBlack)) {
  40. repaint();
  41. if (board.win(i, j, isBlack)) {
  42. JOptionPane.showMessageDialog(null,
  43. isBlack ?
  44.  
  45. "黑方胜!
  46.  
  47. " : "白方胜");
  48. isGameOver = true;
  49. }
  50. isBlack = !isBlack;
  51. }
  52. }
  53. }
  54. }
  55. }
  56.  
  57. /**
  58. * 构造器
  59. */
  60. public MyFrameRenju() {
  61. this.setTitle("五子棋");
  62. this.setSize(800, 800);
  63. this.setResizable(false);
  64. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  65. this.setLocationRelativeTo(null);
  66. this.setLayout(null);
  67. this.getContentPane().setBackground(new Color(180, 125, 12));
  68.  
  69. MyMouseAdapter l = new MyMouseAdapter();
  70. this.addMouseListener(l);
  71.  
  72. }
  73.  
  74. /**
  75. * 重写方法画出全部 newG均为双缓冲 去掉闪屏须要
  76. */
  77. @Override
  78. public void paint(Graphics g) {
  79. Graphics newG = offImage.getGraphics();
  80. super.paint(newG);
  81. board.draw(newG);
  82. g.drawImage(offImage, 0, 0, 800, 800, null);
  83. }
  84.  
  85. public static void main(String[] args) {
  86. new MyFrameRenju().setVisible(true);
  87. }
  88. }

然后是位置面板,绘制棋子。走棋。推断胜负

  1. package com.lovo.homework2;
  2.  
  3. import java.awt.BasicStroke;
  4. import java.awt.Color;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.Shape;
  8. import java.awt.Stroke;
  9.  
  10. import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
  11.  
  12. /**
  13. * 类 : 五子棋棋盘
  14. *
  15. * @author Abe
  16. *
  17. */
  18. public class MyboardRenju {
  19. private int[][] p = new int[15][15]; // 给每一个交点赋值
  20.  
  21. public void draw(Graphics g) {
  22. g.setColor(Color.BLACK);
  23. Graphics2D g2d = (Graphics2D) g; // 强转g为2D型 赋值给g2d
  24. g2d.setStroke(new BasicStroke(5));
  25. g.drawRect(50, 50, 700, 700);
  26. g2d.setStroke(new BasicStroke(1));
  27. g.fillOval(392, 392, 16, 16); // 画天元 星
  28. g.fillOval(195, 195, 10, 10);
  29. g.fillOval(195, 595, 10, 10);
  30. g.fillOval(595, 195, 10, 10);
  31. g.fillOval(595, 595, 10, 10);
  32.  
  33. for (int i = 0; i < 750; i += 50) { // 画横纵坐标线
  34. g.drawLine(50, 100 + i, 750, 100 + i);
  35. g.drawLine(100 + i, 50, 100 + i, 750);
  36. }
  37. for (int i = 0; i < p.length; i++) { // 画出棋盘上的棋子
  38. for (int j = 0; j < p.length; j++) {
  39. if (p[i][j] != 0) {
  40. g.setColor(p[i][j] == 1 ? Color.black : Color.WHITE);
  41. g.fillOval(25 + i * 50, 25 + j * 50, 50, 50);
  42. }
  43. }
  44. }
  45. }
  46.  
  47. /**
  48. * 走棋
  49. */
  50. public boolean move(int i, int j, boolean isBlack) {
  51. if (p[i][j] == 0) {
  52. p[i][j] = isBlack ?
  53.  
  54. 1 : 2;
  55. return true;
  56. }
  57. return false;
  58. }
  59.  
  60. /**
  61. * 方法:推断胜负
  62. */
  63. public boolean win(int i, int j, boolean isBlack) {
  64. int currentColor = isBlack ? 1 : 2;
  65. if (countH(i, j, currentColor) >= 5 || countV(i, j, currentColor) >= 5
  66. || countX1(i, j, currentColor) >= 5
  67. || countX2(i, j, currentColor) >= 5) {
  68. return true;
  69. }
  70. return false;
  71. }
  72.  
  73. private int countH(int i, int j, int currentColor) {
  74. int counter = 1;
  75. int tempi = i;
  76. while (--tempi >= 0 && p[tempi][j] == currentColor) {
  77. counter++;
  78. }
  79. tempi = i;
  80. while (++tempi <= p.length && p[tempi][j] == currentColor) {
  81. counter++;
  82. }
  83. return counter;
  84. }
  85.  
  86. private int countV(int i, int j, int currentColor) {
  87. int counter = 1;
  88. int tempj = j;
  89. while (--tempj >= 0 && p[i][tempj] == currentColor) {
  90. counter++;
  91. }
  92. tempj = j;
  93. while (++tempj <= p.length && p[i][tempj] == currentColor) {
  94. counter++;
  95. }
  96. return counter;
  97. }
  98.  
  99. private int countX1(int i, int j, int currentColor) {
  100. int counter = 1;
  101. int tempi = i;
  102. int tempj = j;
  103. while (--tempj >= 0 && --tempi >= 0 && p[tempi][tempj] == currentColor) {
  104. counter++;
  105. }
  106. tempi = i;
  107. tempj = j;
  108. while (++tempj <= p.length && ++tempi <= p.length
  109. && p[tempi][tempj] == currentColor) {
  110. counter++;
  111. }
  112. return counter;
  113. }
  114.  
  115. private int countX2(int i, int j, int currentColor) {
  116. int counter = 1;
  117. int tempi = i;
  118. int tempj = j;
  119. while (--tempj >= 0 && ++tempi >= 0 && p[tempi][tempj] == currentColor) {
  120. counter++;
  121. }
  122. tempi = i;
  123. tempj = j;
  124. while (++tempj <= p.length && --tempi <= p.length
  125. && p[tempi][tempj] == currentColor) {
  126. counter++;
  127. }
  128. return counter;
  129. }
  130. }

JAVA程序设计(12.3)---- 监听器0基础应用:五子棋的更多相关文章

  1. JAVA程序设计(11)-----面对对象0基础设计 麻将 创建麻将牌 然后洗牌 发牌~ 恩 就这样

    zzzzZZZZ 1.開始还想贴图的 实在太懒了-- 这是一张麻将 package com.lovo; import java.awt.Graphics; import java.awt.Image; ...

  2. Java 入门课程视频实战-0基础 上线了,猜拳游戏,ATM实战,欢迎围观

    Java 入门课程视频实战-0基础 已经上传完了.欢迎小伙伴们过来围观 直接进入: http://edu.csdn.net/course/detail/196 课程文件夹例如以下: 1 初识Java  ...

  3. 《Java程序设计》第三章-基础语法

    20145221<Java程序设计>第三章-基础语法 总结 教材学习内容总结 类型.变量与运算符 类型 Java可区分为基本类型(Primitive Type)和类类型(Class Typ ...

  4. Java程序设计的DOS命令基础

    Java程序设计的DOS命令基础 用户使用操作系统和软件有两种方式:命令行界面(Command Line Interface,CLI)和图形界面(Graphical User Interface,GU ...

  5. 20155304 2016-2017-2 《Java程序设计》第三周学习总结

    20155304 2016-2017-2 <Java程序设计>第三周学习总结 教材学习内容总结 第四章 类与对象 定义: 对象(Object):存在的具体实体,具有明确的状态和行为. 类( ...

  6. 20145221 《Java程序设计》第二周学习总结

    20145221 <Java程序设计>第二周学习总结 教材学习内容总结 第二周内容已在假期完成,详见博客: <Java程序设计>第三章-基础语法 代码调试中的问题和解决过程 第 ...

  7. 【转】WF4.0 (基础篇)

    转自:http://www.cnblogs.com/foundation/category/215023.html 作者:WXWinter  ——  兰竹菊梅★春夏秋冬☆ —— wxwinter@16 ...

  8. 20145219 《Java程序设计》实验四 Android开发基础设计实验报告

    20145219 <Java程序设计>实验四 Android开发基础设计实验报告 实验内容 安装Andriod Studio并配置软件 使用Andriod Studio软件实现Hello ...

  9. 201521123082 《Java程序设计》第12周学习总结

    201521123082 <Java程序设计>第12周学习总结 标签(空格分隔): java 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. An ...

随机推荐

  1. Web框架之Django_06 模型层了解(F查询、Q查询、事务、update和save、only和defer、choice属性、bulk_create)

    摘要: F查询 Q查询 事务 一.F查询 在上面所有的例子中,我们构造的过滤器都只是将字段值与某个我们自己设定的常量做比较.如果我们要对两个字段的值做比较,那该怎么做呢?Django 提供 F() 来 ...

  2. 我的Python分析成长之路8

    Numpy数值计算基础 Numpy:是Numerical Python的简称,它是目前Python数值计算中最为基础的工具包,Numpy是用于数值科学计算的基础模块,不但能够完成科学计算的任而且能够用 ...

  3. (转)Objective-C语言--属性和实例变量

    本文转自http://blog.csdn.net/addychen/article/details/39525681 使用Objective-C一段时间了,一直没有弄清楚在Objective-C中属性 ...

  4. 解决img标签上下出现间隙的方法

    图片与父元素下边缘有 2px 的间隙,并不是因为空格.多个 inline-block 元素之间的间隙才是因为空格. 任何不是块级元素的可见元素都是内联元素,其表现的特性是“行布局”形式.----< ...

  5. luogu2050 [NOI2012]美食节

    修车加强版 边跑边加,有个师傅做到第 i 个(相对他自己而言),就给他加到 i+1 个. #include <iostream> #include <cstring> #inc ...

  6. ci $this->load->database()

    http://pengbotao.cn/codeigniter-database.html

  7. Leetcode 392.判断子序列

    判断子序列 给定字符串 s 和 t ,判断 s 是否为 t 的子序列. 你可以认为 s 和 t 中仅包含英文小写字母.字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 ...

  8. BZOJ 3747: [POI2015]Kinoman 【线段树】

    Description 共有m部电影,编号为1~m,第i部电影的好看值为w[i]. 在n天之中(从1~n编号)每天会放映一部电影,第i天放映的是第f[i]部. 你可以选择l,r(1<=l< ...

  9. 【kmp+最小循环节】poj 2406 Power Strings

    http://poj.org/problem?id=2406 [题意] 给定字符串s,s=a^n,a是s的子串,求n最大是多少 [思路] kmp中的next数组求最小循环节的应用 例如 ababab ...

  10. 【bzoj1193】[HNOI2006]马步距离

    [HNOI2006]马步距离 Description Input 只包含4个整数,它们彼此用空格隔开,分别为xp,yp,xs,ys.并且它们的都小于10000000. Output 含一个整数,表示从 ...