Java创新实习课程设计
报告
软件系统测试报告下载sgs报告如何下载关于路面塌陷情况报告535n,sgs报告怎么下载竣工报告下载
题 目: 简易五子棋游戏开发
院(系):
年级专业:
学生姓名:
学生学号:
指导老师:
实习时间:
成绩评定: 设计报告(40%)
程序演示(20%)
回答问题(40%)
总成绩:
目 录
1. 摘 要 -----------------------------------------------------2
2. 概要设计-------------------------------------------------- 2
3. 详细设计-------------------------------------------------- 2
4. 测试分析-------------------------------------------------- 9
5. 参考文献--------------------------------------------------9
一、 摘 要
五子棋游戏是常见的经典游戏,在19*19的棋盘方格上进行对弈,由于时间与基础知识的掌握程度限制,现初步完成简易五子棋的实现。本程序完全有面向对象的JAVA语言编写而成,单击“开始游戏按钮”将弹出确认对话框,当点击对话框上的“确认”按钮,游戏正式开始;“认输”按钮表示当对弈双方有一方认输时,结束比赛并且输出结果;“关于”按钮的功能可以由制作者自己添加,主要表示游戏的版本等级;“退出”按钮单击时推出对弈比赛。
二、 概要设计
(一) 自定义类说明
类名:ChessBoardFrame
作用:显示五子棋游戏的主界面在整个屏幕中的位置与大小
继承的父类:JFrame
实现接口:无
public class ChessBoardFrame extends JFrame
{
}//自定义一个继承于Jframe的类用于获取五子棋背景图的大小,在整个屏幕上的显示位置
类名:ChessBoardPanel
作用:实现五子棋的各个功能,通过面板继承来进行背景图片载入、通过画直线的成员函数画出棋盘、通过判断函数对鼠标的判断以保证正确落子、在判断函数中还包括了对输与赢的判断、加上清屏函数、每次重新开始时进行清屏。在此类中主要使用了JAVA本身所具有的一些功能,如:异常抛出、鼠标适配器、鼠标单击时间的捕捉、窗口重绘、颜色填充等功能。
继承的父类:JPanel
实现接口:无
public class ChessBoardPanel extends JPanel
{
}//完成五子棋的主要功能
(二) 程序流程图
x >= 10 && x <= 370 && y >= 50 && y <= 410
开始游戏
确认
i = (x - 10) / 20
j = (y - 50) / 20
(x - 10) % 20 > 10
(y - 50) % 20 > 10
i = i + 1;
j = j + 1;
arr[i][j] == 0;
arr[i][j]= chess;
游戏结束
三、 详细设计
一、ChessBoardFrame类
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class ChessBoardFrame extends JFrame {
public ChessBoardFrame() {
// 设置标题
this.setTitle("五子棋");
int width = 500;
int height = 456;
// 窗口起点坐标
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension dimension = kit.getScreenSize();
double screenWidth = dimension.getWidth();
double screenHeight = dimension.getHeight();
int x = (int) (screenWidth - width) / 2;
int y = (int) (screenHeight - height) / 2;
// 设置窗口大小和位置
this.setBounds(x, y, width, height);
// 设置窗口大小不可改变
this.setResizable(false);
// 设置默认关闭选项
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 将面板放于窗口中
// 注意:这句话在ChessBoardFrame的构造方法中
this.add(new ChessBoardPanel());
this.setVisible(true);
}
public static void main(String[] args) {
new ChessBoardFrame();
}
}
二、ChessBoardPanel类
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class ChessBoardPanel extends JPanel {
// 保存棋子,1表示黑棋,-1表示白棋,0表示没有棋
int[][] arr = new int[19][19];
// 是否开始游戏
boolean start;
// 棋子
int chess = 1;
public ChessBoardPanel() {
this.addMouseListener(ma);
this.addMouseMotionListener(mma);
}
MouseMotionAdapter mma = new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if ((x >= 400 && x <= 470 && y >= 50 && y <= 80)
|| (x >= 400 && x <= 470 && y >= 250 && y <= 280)
|| (x >= 400 && x <= 470 && y >= 300 && y <= 330)
|| (x >= 400 && x <= 470 && y >= 350 && y <= 380)) {
setCursor(new Cursor(Cursor.HAND_CURSOR));
} else {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
};
// 棋盘:10,50;370,410
// 开始游戏:400,50;470,80
// 认输:400,250;470,280
// 关于:400,300;470,330
// 退出:400,350;470,380
MouseAdapter ma = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (start == true) {
if (x >= 10 && x <= 370 && y >= 50 && y <= 410) {
int i = (x - 10) / 20;
int j = (y - 50) / 20;
if ((x - 10) % 20 > 10) {
i = i + 1;
}
if ((y - 50) % 20 > 10) {
j = j + 1;
}
if (arr[i][j] == 0) {
arr[i][j] = chess;
}
// 判断输赢
if (isWin(chess) == true) {
if (chess == 1) {
JOptionPane.showMessageDialog(null, "黑棋获胜");
} else {
JOptionPane.showMessageDialog(null, "白棋获胜");
}
start = false;
}
// 改棋子颜色
chess = chess * (-1);
}
}
// 开始游戏
if (x >= 400 && x <= 470 && y >= 50 && y <= 80) {
// 清空数组,清盘
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
arr[i][j] = 0;
}
}
start = true;
JOptionPane.showMessageDialog(null, "游戏开始!");
}
// 认输
if (x >= 400 && x <= 470 && y >= 250 && y <= 280) {
int option = JOptionPane.showConfirmDialog(null, "确定要认输吗?",
"确认框", JOptionPane.YES_NO_OPTION);
if (option == 0) {
if (chess == 1) {
JOptionPane.showMessageDialog(null, "黑棋认输,白棋胜");
} else {
JOptionPane.showMessageDialog(null, "白棋认输,黑棋胜");
}
start = false;
}
}
// 关于
if (x >= 400 && x <= 470 && y >= 300 && y <= 330) {
JOptionPane.showMessageDialog(null, "简易五子棋1.0版");
}
// 退出
if (x >= 400 && x <= 470 && y >= 350 && y <= 380) {
int option = JOptionPane.showConfirmDialog(null, "确定要退出吗?",
"对话框", JOptionPane.YES_NO_OPTION);
if (option == 0) {
// 退出
System.exit(0);
}
}
}
};
@Override
public void paint(Graphics g) {
super.paint(g);
// 构造背景图的Image对象
Image img = null;
try {
img = ImageIO.read(new File(System.getProperty("user.dir")
+ "/background.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
// 绘制背景图
g.drawImage(img, 0, 0, this);
// 绘制棋盘
// 横向
for (int i = 0; i < 19; i++) {
g.drawLine(10, 50 + i * 20, 370, 50 + i * 20);
}
// 纵向
for (int i = 0; i < 19; i++) {
g.drawLine(10 + i * 20, 50, 10 + i * 20, 410);
}
// 遍历数组
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
if (arr[i][j] != 0) {
if (arr[i][j] == 1) {
g.setColor(Color.BLACK);
g.fillOval(3 + i * 20, 43 + j * 20, 14, 14);
g.drawOval(3 + i * 20, 43 + j * 20, 14, 14);
} else {
g.setColor(Color.WHITE);
g.fillOval(3 + i * 20, 43 + j * 20, 14, 14);
g.setColor(Color.BLACK);
g.drawOval(3 + i * 20, 43 + j * 20, 14, 14);
}
}
}
}
// 让窗口重绘
this.repaint();
}
// 判断输赢
// 横向
public boolean isH(int chess) {
for (int i = 0; i < 19; i++) {
for (int j = 2; j < 17; j++) {
if (arr[i][j - 2] == chess && arr[i][j - 1] == chess
&& arr[i][j] == chess && arr[i][j + 1] == chess
&& arr[i][j + 2] == chess) {
return true;
}
}
}
return false;
}
// 纵向
public boolean isV(int chess) {
for (int i = 2; i < 17; i++) {
for (int j = 0; j < 19; j++) {
if (arr[i - 2][j] == chess && arr[i - 1][j] == chess
&& arr[i][j] == chess && arr[i + 1][j] == chess
&& arr[i + 2][j] == chess) {
return true;
}
}
}
return false;
}
// 东南
public boolean isEastSouth(int chess) {
for (int i = 2; i < 17; i++) {
for (int j = 2; j < 17; j++) {
if (arr[i - 2][j - 2] == chess && arr[i - 1][j - 1] == chess
&& arr[i][j] == chess && arr[i + 1][j + 1] == chess
&& arr[i + 2][j + 2] == chess) {
return true;
}
}
}
return false;
}
// 东北
public boolean isEastNorth(int chess) {
for (int i = 2; i < 17; i++) {
for (int j = 2; j < 17; j++) {
if (arr[i - 2][j + 2] == chess && arr[i - 1][j + 1] == chess
&& arr[i][j] == chess && arr[i + 1][j - 1] == chess
&& arr[i + 2][j - 2] == chess) {
return true;
}
}
}
return false;
}
public boolean isWin(int chess) {
if ((isH(chess) == true) || (isV(chess) == true)
|| (isEastSouth(chess) == true) || (isEastNorth(chess) == true)) {
return true;
}
return false;
}
}
四、 测试分析
(1)程序运行情况:
程序正常的响应按钮事件,鼠标点击的范围严格受到g.getwidth()与g.getheiht()的限定,当在加载图片的时候可能由于图片在不同的机器上的存储位置的不同而需要进行异常处理,当用户将图片存储在不同的地方时,通过得到用户的计算机在所存位置处的图片而进行异常处理。
(2)程序异常处理:
当所存储的背景图片与程序中的不符合时就抛出一个任意字符作为异常对象,然后使用try-catch去捕捉这个异常,异常的处理如下:
try {
img = ImageIO.read(new File(System.getProperty("user.dir")+ "/background.jpg"));
}
catch (IOException e) {e.printStackTrace();
} //如果出现异常则初始化效果如同clear键
五、 参考文献
1.、《Java自学手册》 明日科技主编 人民邮电出版社