版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 “Java程序设计”综合实验报告一、前言1、项目背景通过五子棋这个课程设计。可以让我们更加熟悉Java程序设计这门课程,加强对Eclipse这个软件的使用,加深对Java语言的掌握,提高编程水平。同时培养能力,为将来的工作打下坚实的基础。2、 目标和意义 目标:锻炼我们的能力,提高组中每一个人对Java语言的认识,培养编程兴趣。让每一个人都能参与进来,提高团队合作意识。 意义:通过编写综合类的程序,运用已经学过的知识,和自主学习一些新的知识,提高了学习能力,掌握了一些自主学习的方法。3、 开发分工及进度安排二、功能分析1、主要模块 本项目主要分为3个大的模块,分别为整体布局;界面绘制;与对战
2、的算法。由于游戏规则的设置,这里的游戏模式分为人机对战和人人对战。黑白双方依次落子,由黑子先下,当任意一方在棋盘上形成横向,竖向,斜向连续五个相同颜色的棋子的一方获胜。主要功能实现在2种模式下五子棋的游戏。实现通过鼠标的点击位置放置棋子,达到下棋的目的。实现游戏玩家对游戏模式的自主选择。实现对在每种游戏模式下的黑子先手下棋的规定,先达到5子即为胜利。三、关键功能的设计与实现1、数据结构与算法数据结构:项目中主要数据结构为二维数组。用于存储棋盘上棋子的信息,和保存棋型表。主要算法:(一)iswin()函数:用来判断输赢,通过鼠标事件所得到的点或者电脑下的点的坐标,来扫描该点八个方向的相邻的相同棋
3、子数,上下,左右,斜左上下,斜右上下为四组,任意一组等于5即为胜利,由于本程序没有考虑禁手原则,只考虑了民间规则,所以大于5也为胜利。public int iswin1(int x, int y, int heqi) int k, s1, s2, s3, s4, s5, s6, s7, s8;s1 = 0;s2 = 0;s3 = 0;s4 = 0;s5 = 0;s6 = 0;s7 = 0;s8 = 0;if (heqi = 256)return -1;for (k = 1; k < 5; k+) if (y + k < 16 && qipanqizixy + k =
4、 qipanqizixy)s1+;elsebreak;for (k = 1; k < 5; k+) if (y - k > -1 && qipanqizixy - k = qipanqizixy)s2+;elsebreak;for (k = 1; k < 5; k+) if (x + k < 16 && y + k < 16&& qipanqizix + ky + k = qipanqizixy)s3+;elsebreak;for (k = 1; k < 5; k+) if (x - k > -1 &am
5、p;& y - k > -1&& qipanqizix - ky - k = qipanqizixy)s4+;elsebreak;for (k = 1; k < 5; k+) if (x + k < 16 && qipanqizix + ky = qipanqizixy)s5+;elsebreak;for (k = 1; k < 5; k+) if (x - k > -1 && qipanqizix - ky = qipanqizixy)s6+;elsebreak;for (k = 1; k < 5;
6、k+) if (x - k > -1 && y + k < 16&& qipanqizix - ky + k = qipanqizixy)s7+;elsebreak;for (k = 1; k < 5; k+) if (x + k < 16 && y - k > -1&& qipanqizix + ky - k = qipanqizixy)s8+;elsebreak;if (s1 + s2 >= 4 | s3 + s4 >= 4 | s5 + s6 >= 4 | s7 + s8 &g
7、t;= 4) return 1; elsereturn 0;(二)人机对战通过对整个棋盘上每一个点的扫描,获得了电脑和玩家的棋型表,表中数据为该点的权值。根据权值来考虑下一步应该下在哪里。棋型表中的权值通过引用下表的值来计算。public void renji() int i, j, max, jx = 0, jy = 0, k = 0;max = -10;q = getGraphics();if (gameover = false) if (isblack = true) if (qipanqiziy1x1 = -1) Point linshi = new Point(x1, y1, Col
8、or.black);pointii = new Point(linshi, q);isblack = false;qipanqiziy1x1 = 0;/ 0代表黑棋,1代表白棋,-1代表空if (iswin1(y1, x1, ii) = 1) String msg = String.format("恭喜,你赢了!");JOptionPane.showMessageDialog(this, msg);gameover = true;if (iswin1(y1, x1, ii) = -1) String msg = String.format("和棋!");
9、JOptionPane.showMessageDialog(this, msg);gameover = true;ii+;if (isblack = false) qixing();for (i = 0; i < 16; i+)for (j = 0; j < 16; j+) if (qixingPCij != 0 && max < qixingPCij) max = qixingPCij;jx = i;jy = j;for (i = 0; i < 16; i+)for (j = 0; j < 16; j+) if (qixingPlayerij !
10、= 0 && max < qixingPlayerij) max = qixingPlayerij;jx = i;jy = j;Point linshi1 = new Point(jy, jx, Color.white);pointii = new Point(linshi1, q);qipanqizijxjy = 1;isblack=true;if (iswin1(jx, jy, ii) = 1) String msg = String.format("哦,你输啦!");JOptionPane.showMessageDialog(this, msg)
11、;gameover = true;if (iswin1(jx, jy, ii) = -1) String msg = String.format("和棋!");JOptionPane.showMessageDialog(this, msg);gameover = true;ii+;public void qixing() int i, j;for (i = 0; i < 16; i+)for (j = 0; j < 16; j+) qixingPCij = 0;qixingPlayerij = 0;for (i = 0; i < 16; i+) for (
12、j = 0; j < 16; j+) if (qipanqiziij = -1) / 此处没有棋子qixingPCij = heng(i, j, 1, 0) + shu(i, j, 1, 0)+ zuoxie(i, j, 1, 0) + youxie(i, j, 1, 0);qixingPlayerij = heng(i, j, 0, 1) + shu(i, j, 0, 1)+ zuoxie(i, j, 0, 1) + youxie(i, j, 0, 1);public int heng(int hang, int lie, int num1, int num2) / num1当前棋子颜
13、色,num2对方棋子颜色int i;int left = new int4;int right = new int4;for (i = 1; i < 5; i+)if (lie - i > -1 && qipanqizihanglie - i = num1)lefti - 1 = num1;else if (lie - i > -1 && qipanqizihanglie - i = -1)/ 空lefti - 1 = -1;else if (lie - i > -1 && qipanqizihanglie - i = n
14、um2)/ 不空,为对方棋子lefti - 1 = num2;else if (lie - i <= -1)/ 边界,越界置-2lefti - 1 = -2;for (i = 1; i < 5; i+)if (lie + i < 16 && qipanqizihanglie + i = num1)righti - 1 = num1;else if (lie + i < 16 && qipanqizihanglie + i = -1)/ 空righti - 1 = -1;else if (lie + i < 16 &&
15、 qipanqizihanglie + i = num2)/ 不空,为对方棋子righti - 1 = num2;else if (lie + i >= 16)/ 边界righti - 1 = -2;return jieguo(left, right, num1, num2);public int shu(int hang, int lie, int num1, int num2) int i;int left = new int4;int right = new int4;for (i = 1; i < 5; i+)if (hang - i > -1 &&
16、qipanqizihang - ilie = num1)lefti - 1 = num1;else if (hang - i > -1 && qipanqizihang - ilie = -1)/ 空lefti - 1 = -1;else if (hang - i > -1 && qipanqizihang - ilie = num2)/ 不空,为对方棋子lefti - 1 = num2;else if (hang - i <= -1)/ 边界lefti - 1 = -2;for (i = 1; i < 5; i+)if (hang +
17、i < 16 && qipanqizihang + ilie = num1)righti - 1 = num1;else if (hang + i < 16 && qipanqizihang + ilie = -1)/ 空righti - 1 = -1;else if (hang + i < 16 && qipanqizihang + ilie = num2)/ 不空,为对方棋子righti - 1 = num2;else if (hang + i >= 16)/ 边界righti - 1 = -2;return jieg
18、uo(left, right, num1, num2);public int zuoxie(int hang, int lie, int num1, int num2) / /int i;int left = new int4;int right = new int4;for (i = 1; i < 5; i+)if (hang + i < 16 && lie - i > -1&& qipanqizihang + ilie - i = num1)lefti - 1 = num1;else if (hang + i < 16 &&a
19、mp; lie - i > -1&& qipanqizihang + ilie - i = -1)/ 空lefti - 1 = -1;else if (hang + i < 16 && lie - i > -1&& qipanqizihang + ilie - i = num2)/ 不空,为对方棋子lefti - 1 = num2;else if (lie - i <= -1 | hang + i >= 16)/ 边界lefti - 1 = -2;for (i = 1; i < 5; i+)if (hang -
20、 i > -1 && lie + i < 16&& qipanqizihang - ilie + i = num1)righti - 1 = num1;else if (hang - i > -1 && lie + i < 16&& qipanqizihang - ilie + i = -1)/ 空righti - 1 = -1;else if (hang - i > -1 && lie + i < 16&& qipanqizihang - ilie + i =
21、num2)/ 不空,为对方棋子righti - 1 = num2;else if (lie + i >= 16 | hang - i <= -1)/ 边界righti - 1 = -2;return jieguo(left, right, num1, num2);public int youxie(int hang, int lie, int num1, int num2) / int i;int left = new int4;int right = new int4;for (i = 1; i < 5; i+)if (hang - i > -1 &&
22、 lie - i > -1&& qipanqizihang - ilie - i = num1)lefti - 1 = num1;else if (hang - i > -1 && lie - i > -1&& qipanqizihang - ilie - i = -1)/ 空lefti - 1 = -1;else if (hang - i > -1 && lie - i > -1&& qipanqizihang - ilie - i = num2)/ 不空,为对方棋子lefti -
23、1 = num2;else if (lie - i <= -1 | hang - i <= -1)/ 边界lefti - 1 = -2;for (i = 1; i < 5; i+)if (hang + i < 16 && lie + i < 16&& qipanqizihang + ilie + i = num1)righti - 1 = num1;else if (hang + i < 16 && lie + i < 16&& qipanqizihang + ilie + i = -1)
24、/ 空righti - 1 = -1;else if (hang + i < 16 && lie + i < 16&& qipanqizihang + ilie + i = num2)/ 不空,为对方棋子righti - 1 = num2;else if (lie + i >= 16 | hang + i >= 16)/ 边界righti - 1 = -2;return jieguo(left, right, num1, num2);public int jieguo(int left, int right, int num1, int
25、num2) int i, j;int qixing = new int9;qixing0 = left3;qixing1 = left2;qixing2 = left1;qixing3 = left0;qixing4 = num1;qixing5 = right0;qixing6 = right1;qixing7 = right2;qixing8 = right3;for (i = 0; i <= 4; i+) if (qixingi = num1 && qixingi + 1 = num1&& qixingi + 2 = num1 &&
26、qixingi + 3 = num1&& qixingi + 4 = num1)return 3000000;for (i = 0; i <= 3; i+) if (qixingi = -1 && qixingi + 1 = num1&& qixingi + 2 = num1 && qixingi + 3 = num1&& qixingi + 4 = num1 && qixingi + 5 = -1) return 300000;/ ?AAAA?for (i = 0; i <= 4; i
27、+) if (qixingi = num1 && qixingi + 1 = num1&& qixingi + 2 = num1 && qixingi + 3 = num1&& qixingi + 4 = -1)return 2500;/ AAAA?for (i = 0; i <= 4; i+) if (qixingi = -1 && qixingi + 1 = num1&& qixingi + 2 = num1 && qixingi + 3 = num1&&
28、qixingi + 4 = num1)return 2500;/ ?AAAAfor (i = 0; i <= 4; i+) if (qixingi = num1 && qixingi + 1 = num1&& qixingi + 2 = num1 && qixingi + 3 = -1&& qixingi + 4 = num1) return 3000;/ AAA?Afor (i = 0; i <= 4; i+) if (qixingi = num1 && qixingi + 1 = -1&&a
29、mp; qixingi + 2 = num1 && qixingi + 3 = num1&& qixingi + 4 = num1) return 3000;/ A?AAAfor (i = 0; i <= 4; i+) if (qixingi = num1 && qixingi + 1 = num1&& qixingi + 2 = -1 && qixingi + 3 = num1&& qixingi + 4 = num1) return 2600;/ AA?AAfor (i = 0; i &l
30、t;= 2; i+) if (qixingi = -1 && qixingi + 1 = -1 && qixingi + 2 = num1&& qixingi + 3 = num1 && qixingi + 4 = num1&& qixingi + 5 = -1 && qixingi + 6 = -1) return 3000;/ ?AAA?for (i = 0; i <= 4; i+) if (qixingi + 0 = num1 && qixingi + 1 = num1&
31、amp;& qixingi + 2 = num1 && qixingi + 3 = -1&& qixingi + 4 = -1)return 500;/ AAA?for (i = 0; i <= 4; i+) if (qixingi + 0 = -1 && qixingi + 1 = -1&& qixingi + 2 = num1 && qixingi + 3 = num1&& qixingi + 4 = num1)return 500;/ ?AAAfor (i = 0; i <
32、= 3; i+) if (qixingi = -1 && qixingi + 1 = num1 && qixingi + 2 = -1&& qixingi + 3 = num1 && qixingi + 4 = num1&& qixing5 = -1) return 800;/ ?A?AA?for (i = 0; i <= 3; i+) if (qixingi = -1 && qixingi + 1 = num1&& qixingi + 2 = num1 &&
33、qixingi + 3 = -1&& qixingi + 4 = num1 && qixingi + 5 = -1) return 800;/ ?AA?A?for (i = 0; i <= 4; i+) if (qixingi = num1 && qixingi + 1 = -1 && qixingi + 2 = -1&& qixingi + 3 = num1 && qixingi + 4 = num1) return 600;/ A?AAfor (i = 0; i <= 4; i+)
34、if (qixingi = num1 && qixingi + 1 = num1&& qixingi + 2 = -1 && qixingi + 3 = -1&& qixingi + 4 = num1) return 600;/ AA?Afor (i = 0; i <= 4; i+) if (qixingi = num1 && qixingi + 1 = -1&& qixingi + 2 = num1 && qixingi + 3 = -1&& qixingi
35、+ 4 = num1) return 550;/ A?A?Afor (i = 0; i <= 1; i+) if (qixingi = -1 && qixingi + 1 = -1 && qixingi + 2 = -1&& qixingi + 3 = num1 && qixingi + 4 = num1&& qixingi + 5 = -1 && qixingi + 6 = -1&& qixingi + 7 = -1) return 650;/ ?AA?for (i = 0;
36、 i <= 4; i+) if (qixingi = num1 && qixingi + 1 = num1&& qixingi + 2 = -1 && qixingi + 3 = -1&& qixingi + 4 = -1) return 150;/ AA?for (i = 0; i <= 4; i+) if (qixingi = -1 && qixingi + 1 = -1 && qixingi + 2 = -1&& qixingi + 3 = num1 &&a
37、mp; qixingi + 4 = num1) return 150;/ ?AAfor (i = 0; i <= 2; i+) if (qixingi = -1 && qixingi + 1 = -1 && qixingi + 2 = num1&& qixingi + 3 = -1 && qixingi + 4 = num1&& qixingi + 5 = -1 && qixingi + 6 = -1) return 250;/ ?A?A?for (i = 0; i <= 3; i+)
38、if (qixingi = -1 && qixingi + 1 = num1 && qixingi + 2 = -1&& qixingi + 3 = -1 && qixingi + 4 = num1&& qixingi + 5 = -1) return 200;/ ?A?A?return 10;2、 整体代码主类(Chessboard):实现了程序界面的生成和事件监听,继承了JFrame,作为面板。加入了两个容器,一个是棋盘,一个是容器包含两个按钮。实现了两个按钮的功能-调用棋盘类的函数去重绘并且选择算法。pack
39、age 绘制图形1;import ;import ;import javax.swing.*;import .*;import java.awt.event.*;import java.awt.*;public class Chessboard extends JFrame implements ActionListener private JButton jbt1 = new JButton("人人对战");private JButton jbt2 = new JButton("人机对战");/private JButton jbt3 = new JB
40、utton("悔棋");/private JFrame Iswin = new JFrame();qipan zhegeqipan = new qipan();public Chessboard() Container container = getContentPane();container.setLayout(new BorderLayout();JPanel p1 = new JPanel();p1.setLayout(new GridLayout(1, 2);p1.add(jbt1);p1.add(jbt2);/p1.add(jbt3);container.add
41、(zhegeqipan, BorderLayout.CENTER);container.add(p1, BorderLayout.SOUTH);jbt1.addActionListener(this);jbt2.addActionListener(this);/jbt3.addActionListener(this);public void actionPerformed(ActionEvent e) / Graphics/ g=zhegeqipan.getGraphics();if (e.getActionCommand() = "人人对战") / zhegeqipan.
42、paintComponent(g);zhegeqipan.huifu();zhegeqipan.repaint();zhegeqipan.renji = false;System.out.println("renrenn");if (e.getActionCommand() = "人机对战") zhegeqipan.huifu();zhegeqipan.repaint();zhegeqipan.renji = true;System.out.println("renjin");/if (e.getActionCommand() = &
43、quot;悔棋")/System.out.println("悔棋n");/ System.out.println("The"+e.getActionCommand()+"button is"+"clicked atn"/*+new/ java.util.Date(e.getWhen()*/);public static void main(String args) Chessboard frame = new Chessboard();frame.setVisible(true);frame.setRes
44、izable(false);frame.setTitle("五子棋");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(600, 650); 棋子类(point):定义了棋子的属性,包括棋子的坐标,半径,颜色,和绘制黑白3D棋子的函数。package 绘制图形1;import java.awt.Graphics;import java.awt.Color;import javax.swing.*;import .*;import .*;import java.awt.*;import ja
45、va.awt.geom.Ellipse2D;import java.awt.Graphics2D;import java.awt.RadialGradientPaint;import java.awt.RenderingHints;public class Point int pointx, pointy, pointr;Color pointcolor;boolean islive = false;public Point(int x, int y, Color color) pointx = x;pointy = y;pointcolor = color;public Point(Poin
46、t point, Graphics g) / z=30黑,z=130+白pointx = point.pointx;pointy = point.pointy;pointcolor = point.pointcolor;if (pointx > -1 && pointx < 16 && pointy > -1 && pointy < 16&& pointcolor = Color.black/* |point.islive=false */)Graphics2D g2d = (Graphics2D) g;R
47、adialGradientPaint paint = new RadialGradientPaint(pointx + 1) * 35 + 10, (pointy + 1) * 35 + 10, 30,new float 0f, 1f , new Color Color.WHITE,Color.BLACK );g2d.setPaint(paint);g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);g2d.setRenderingHint(RenderingHints.
48、KEY_ALPHA_INTERPOLATION,RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT);Ellipse2D e = new Ellipse2D.Float(pointx * 35 + 15,pointy * 35 + 15, 35, 35);g2d.fill(e);if (pointx > -1 && pointx < 16 && pointy > -1 && pointy < 16&& pointcolor = Color.white/* |po
49、int.islive=false */)Graphics2D g2d = (Graphics2D) g;RadialGradientPaint paint = new RadialGradientPaint(pointx + 1) * 35 + 10, (pointy + 1) * 35 + 10, 130,new float 0f, 1f , new Color Color.WHITE,Color.BLACK );g2d.setPaint(paint);g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VA
50、LUE_ANTIALIAS_ON);g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT);Ellipse2D e = new Ellipse2D.Float(pointx * 35 + 15,pointy * 35 + 15, 35, 35);g2d.fill(e);public int getX() return pointx;public int getY() return pointy;public Color getCol
51、or() return pointcolor;棋盘类(qipan):包含了程序的核心算法,鼠标监听和棋盘的属性和绘制。包括了判断输赢的函数和人机对战的函数。package 绘制图形1;import java.awt.Graphics;import java.awt.Color;import java.awt.Cursor;import javax.swing.*;import javax.swing.event.*;import java.awt.event.*;import java.awt.geom.Ellipse2D;import java.awt.Graphics2D;import java.awt.RadialGradientPaint;import java.awt.RenderingHints;public class qipan extends JPanel implements MouseMotionListener, MouseListener Point point = new Point16 * 16;public int qipanqizi = new int1616;public int qixingPC = new int1616;public int qixingPlayer = new int
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 原料供应商合同
- 鸡场疫病防控合同
- 2025届黑龙江省牡丹江市爱民区牡丹江一中物理高三上期末经典模拟试题含解析
- 陕西省西北工业大学附中2025届高一物理第一学期期中复习检测模拟试题含解析
- 河北省安平中学2025届物理高二第一学期期中质量跟踪监视试题含解析
- 内部通信装置产业运行及前景预测报告
- 国家开放大学机考合同法
- 2024年地面测控管理设备项目提案报告
- 2024年产业互联网项目提案报告
- 加工承揽合同简单版样板
- 大型展会对城市会展业发展影响文献综述会展专业
- 乡镇结核病防治工作职责
- 机组启动试运行工作报告
- 礼仪队工作计划三篇
- 互补输出级介绍
- 中波广播发送系统概述
- (完整版)管道代号对照
- 县疾控中心中层干部竞聘上岗实施方案
- 急性心肌梗死精美PPt完整版
- 市森林消防(防汛)专业队管理制度森林防火扑火队管理制度.doc
- 物业日常巡查记录表.doc
评论
0/150
提交评论