1.制作五子棋游戏软件

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

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

package com.lovo.homework2;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage; import javax.swing.JFrame;
import javax.swing.JOptionPane; /**
* 类 : 我的五子棋窗体
*
* @author Abe
*/
public class MyFrameRenju extends JFrame {
private MyboardRenju board = new MyboardRenju();
private boolean isBlack = true;
private Image offImage = new BufferedImage(800, 800,
BufferedImage.TYPE_INT_RGB);// 双缓冲
private boolean isGameOver = false; /**
* 内部类:鼠标适配器
*
* @author Abe
*/
public class MyMouseAdapter extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) { // 重写点击鼠标的方法
if (!isGameOver) {
int x = e.getX();
int y = e.getY();
if (x > 25 && x < 775 && y > 25 && y < 775) {
int i = (x - 25) / 50;
int j = (y - 25) / 50;
if (board.move(i, j, isBlack)) {
repaint();
if (board.win(i, j, isBlack)) {
JOptionPane.showMessageDialog(null,
isBlack ? "黑方胜! " : "白方胜");
isGameOver = true;
}
isBlack = !isBlack;
}
}
}
}
} /**
* 构造器
*/
public MyFrameRenju() {
this.setTitle("五子棋");
this.setSize(800, 800);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.getContentPane().setBackground(new Color(180, 125, 12)); MyMouseAdapter l = new MyMouseAdapter();
this.addMouseListener(l); } /**
* 重写方法画出全部 newG均为双缓冲 去掉闪屏须要
*/
@Override
public void paint(Graphics g) {
Graphics newG = offImage.getGraphics();
super.paint(newG);
board.draw(newG);
g.drawImage(offImage, 0, 0, 800, 800, null);
} public static void main(String[] args) {
new MyFrameRenju().setVisible(true);
}
}

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

package com.lovo.homework2;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke; import javax.xml.bind.annotation.adapters.CollapsedStringAdapter; /**
* 类 : 五子棋棋盘
*
* @author Abe
*
*/
public class MyboardRenju {
private int[][] p = new int[15][15]; // 给每一个交点赋值 public void draw(Graphics g) {
g.setColor(Color.BLACK);
Graphics2D g2d = (Graphics2D) g; // 强转g为2D型 赋值给g2d
g2d.setStroke(new BasicStroke(5));
g.drawRect(50, 50, 700, 700);
g2d.setStroke(new BasicStroke(1));
g.fillOval(392, 392, 16, 16); // 画天元 星
g.fillOval(195, 195, 10, 10);
g.fillOval(195, 595, 10, 10);
g.fillOval(595, 195, 10, 10);
g.fillOval(595, 595, 10, 10); for (int i = 0; i < 750; i += 50) { // 画横纵坐标线
g.drawLine(50, 100 + i, 750, 100 + i);
g.drawLine(100 + i, 50, 100 + i, 750);
}
for (int i = 0; i < p.length; i++) { // 画出棋盘上的棋子
for (int j = 0; j < p.length; j++) {
if (p[i][j] != 0) {
g.setColor(p[i][j] == 1 ? Color.black : Color.WHITE);
g.fillOval(25 + i * 50, 25 + j * 50, 50, 50);
}
}
}
} /**
* 走棋
*/
public boolean move(int i, int j, boolean isBlack) {
if (p[i][j] == 0) {
p[i][j] = isBlack ? 1 : 2;
return true;
}
return false;
} /**
* 方法:推断胜负
*/
public boolean win(int i, int j, boolean isBlack) {
int currentColor = isBlack ? 1 : 2;
if (countH(i, j, currentColor) >= 5 || countV(i, j, currentColor) >= 5
|| countX1(i, j, currentColor) >= 5
|| countX2(i, j, currentColor) >= 5) {
return true;
}
return false;
} private int countH(int i, int j, int currentColor) {
int counter = 1;
int tempi = i;
while (--tempi >= 0 && p[tempi][j] == currentColor) {
counter++;
}
tempi = i;
while (++tempi <= p.length && p[tempi][j] == currentColor) {
counter++;
}
return counter;
} private int countV(int i, int j, int currentColor) {
int counter = 1;
int tempj = j;
while (--tempj >= 0 && p[i][tempj] == currentColor) {
counter++;
}
tempj = j;
while (++tempj <= p.length && p[i][tempj] == currentColor) {
counter++;
}
return counter;
} private int countX1(int i, int j, int currentColor) {
int counter = 1;
int tempi = i;
int tempj = j;
while (--tempj >= 0 && --tempi >= 0 && p[tempi][tempj] == currentColor) {
counter++;
}
tempi = i;
tempj = j;
while (++tempj <= p.length && ++tempi <= p.length
&& p[tempi][tempj] == currentColor) {
counter++;
}
return counter;
} private int countX2(int i, int j, int currentColor) {
int counter = 1;
int tempi = i;
int tempj = j;
while (--tempj >= 0 && ++tempi >= 0 && p[tempi][tempj] == currentColor) {
counter++;
}
tempi = i;
tempj = j;
while (++tempj <= p.length && --tempi <= p.length
&& p[tempi][tempj] == currentColor) {
counter++;
}
return counter;
}
}

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. python--网络编程之socket

    一 . 网络编程 CS架构 客户端服务端架构 服务端:提供服务的 客户端:享受服务的 BS架构:浏览器和服务端 网络通信流程: 集线器:将所有连接上它的电脑全部联通起来 交换机:升级版的集线器 网卡: ...

  2. teatime、

    Python之路,Day7 - 面向对象编程进阶   本节内容: 面向对象高级语法部分 经典类vs新式类 静态方法.类方法.属性方法 类的特殊方法 反射 异常处理 Socket开发基础 作业:开发一个 ...

  3. [转]Makefile中的wildcard/notdir/patsubst

    1.wildcard : 扩展通配符 2.notdir : 去除路径 3.patsubst :替换通配符 例子:建立一个测试目录,在测试目录下建立一个名为sub的子目录$ mkdir test$ cd ...

  4. Idea使用Tomcat乱码 tomcat 9.0 8.5.37乱码

    使用新版tomcat 如8.5.37,9.0.14的时候idea控制台输出乱码,很简单老版本的如8.5.31就不会乱码,使用比较工具比较一下发现如下变化, 关键的关键是\apache-tomcat-8 ...

  5. 【SaltStack】SaltStack研究心得

    基础篇 ------------------------------------------------------------------------------------------------ ...

  6. 关于Linux下使用expdp和impdp命令对Oracle数据库进行导入和导出操作

    说明:本次导入和导出采用expdp和impdp命令进行操作,这2个命令均需要在服务器端进行操作 http://www.cnblogs.com/huacw/p/3888807.html 一.    从O ...

  7. [uiautomator篇] 找父亲节点和其他兄弟节点

    https://testerhome.com/topics/1250 Appium [已解决] UiSelector 如何根据节点定位到父节点 / 兄弟节点? liqing380 · 发布于 2014 ...

  8. POJ 2092 Grandpa is Famous

    Grandpa is Famous Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 7153   Accepted: 3624 ...

  9. cell左右滑动展开更多按钮-MGSwipeTableCell

    MGSwipeTableCell是一个UITableViewCell的子类, 它实现了左,右滑动展开更多按钮用来实现一些相关操作就和QQ好友列表滑动展开的按钮一样,封装的很好,动画效果也处理很到位,废 ...

  10. Linux 下测试磁盘读写 I/O 速度的方法汇总

    在分布式异构存储系统中,我们经常会需要测量获取不同节点中硬盘/磁盘的读写 I/O 速度,下面是 Linux 系统下一些常用测试方法(之后不定期更新): 1.使用 hdparm 命令这是一个是用来获取A ...