《最新“Java程序设计”——“五子棋”综合实验报告.docx》由会员分享,可在线阅读,更多相关《最新“Java程序设计”——“五子棋”综合实验报告.docx(72页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-date“Java程序设计”“五子棋”综合实验报告_ “Java程序设计”综合实验报告-一、前言1、项目背景通过五子棋这个课程设计。可以让我们更加熟悉Java程序设计这门课程,加强对Eclipse这个软件的使用,加深对Java语言的掌握,提高编程水平。同时培养能力,为将来的工作打下坚实的基础。2、 目标和意义 目标:锻炼我们的能力,提高组中每一个人对Java语言的认识,培养编
2、程兴趣。让每一个人都能参与进来,提高团队合作意识。 意义:通过编写综合类的程序,运用已经学过的知识,和自主学习一些新的知识,提高了学习能力,掌握了一些自主学习的方法。3、 开发分工及进度安排二、功能分析1、主要模块 本项目主要分为3个大的模块,分别为整体布局;界面绘制;与对战的算法。由于游戏规则的设置,这里的游戏模式分为人机对战和人人对战。黑白双方依次落子,由黑子先下,当任意一方在棋盘上形成横向,竖向,斜向连续五个相同颜色的棋子的一方获胜。主要功能实现在2种模式下五子棋的游戏。实现通过鼠标的点击位置放置棋子,达到下棋的目的。实现游戏玩家对游戏模式的自主选择。实现对在每种游戏模式下的黑子先手下棋
3、的规定,先达到5子即为胜利。三、关键功能的设计与实现1、数据结构与算法数据结构:项目中主要数据结构为二维数组。用于存储棋盘上棋子的信息,和保存棋型表。主要算法:(一)iswin()函数:用来判断输赢,通过鼠标事件所得到的点或者电脑下的点的坐标,来扫描该点八个方向的相邻的相同棋子数,上下,左右,斜左上下,斜右上下为四组,任意一组等于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;s
4、3 = 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 = qipanqizixy)s1+;elsebreak;for (k = 1; 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+;elsebre
5、ak;for (k = 1; k -1 & 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 -1 & qipanqizix - ky = qipanqizixy)s6+;elsebreak;for (k = 1; k -1 & y + k 16& qipanqizix - ky + k = qipanqizixy)s7+;elsebreak;
6、for (k = 1; k 5; k+) if (x + k -1& qipanqizix + ky - k = qipanqizixy)s8+;elsebreak;if (s1 + s2 = 4 | s3 + s4 = 4 | s5 + s6 = 4 | s7 + s8 = 4) return 1; elsereturn 0;(二)人机对战通过对整个棋盘上每一个点的扫描,获得了电脑和玩家的棋型表,表中数据为该点的权值。根据权值来考虑下一步应该下在哪里。棋型表中的权值通过引用下表的值来计算。public void renji() int i, j, max, jx = 0, jy = 0, k
7、 = 0;max = -10;q = getGraphics();if (gameover = false) if (isblack = true) if (qipanqiziy1x1 = -1) Point linshi = new Point(x1, y1, Color.black);pointii = new Point(linshi, q);isblack = false;qipanqiziy1x1 = 0;/ 0代表黑棋,1代表白棋,-1代表空if (iswin1(y1, x1, ii) = 1) String msg = String.format(恭喜,你赢了!);JOption
8、Pane.showMessageDialog(this, msg);gameover = true;if (iswin1(y1, x1, ii) = -1) String msg = String.format(和棋!);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;
9、jx = i;jy = j;for (i = 0; i 16; i+)for (j = 0; j 16; j+) if (qixingPlayerij != 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.form
10、at(哦,你输啦!);JOptionPane.showMessageDialog(this, msg);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
11、 (i = 0; i 16; i+) for (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, in
12、t num2) / num1当前棋子颜色,num2对方棋子颜色int i;int left = new int4;int right = new int4;for (i = 1; 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 = num2)/ 不空,为对方棋子lefti - 1 = num2;else if (lie -
13、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)/ 边界righti - 1 = -2;return jieguo(left, right, num1, num2);public int shu(int hang, int lie, int
14、 num1, int num2) int i;int left = new int4;int right = new int4;for (i = 1; i -1 & 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)/ 边界
15、lefti - 1 = -2;for (i = 1; i 5; i+)if (hang + i 16 & qipanqizihang + ilie = num1)righti - 1 = num1;else if (hang + i 16 & qipanqizihang + ilie = -1)/ 空righti - 1 = -1;else if (hang + i = 16)/ 边界righti - 1 = -2;return jieguo(left, right, num1, num2);public int zuoxie(int hang, int lie, int num1, int
16、num2) / /int i;int left = new int4;int right = new int4;for (i = 1; i 5; i+)if (hang + i -1& qipanqizihang + ilie - i = num1)lefti - 1 = num1;else if (hang + i -1& qipanqizihang + ilie - i = -1)/ 空lefti - 1 = -1;else if (hang + i -1& qipanqizihang + ilie - i = num2)/ 不空,为对方棋子lefti - 1 = num2;else if
17、 (lie - i = 16)/ 边界lefti - 1 = -2;for (i = 1; i -1 & lie + i -1 & lie + i -1 & 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 -1 & lie - i
18、-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 - 1 = num2;else if (lie - i = -1 | hang - i = -1)/ 边界lefti - 1 = -2;for (i = 1; i
19、 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)/ 空righti - 1 = -1;else if (hang + i 16 & lie + i = 16 | hang + i = 16)/ 边界righti - 1 = -2;return jieguo(left, right, num1, num2);public int jieguo(
20、int left, int right, int num1, int 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 = num
21、1 & 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+) if (qixingi = num1 & qixingi + 1 = num1& qixingi + 2 =
22、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& qixingi + 4 = num1)return 2500;/ ?AAAAfor (i = 0; i = 4; i+) if (qixingi = num1 & qixingi + 1 = num1& qixingi + 2 = num1 & qixingi + 3
23、 = -1& qixingi + 4 = num1) return 3000;/ AAA?Afor (i = 0; i = 4; i+) if (qixingi = num1 & qixingi + 1 = -1& 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 +
24、 4 = num1) return 2600;/ AA?AAfor (i = 0; i = 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& qixingi + 2 = num1 & qix
25、ingi + 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 = 3; i+) if (qixingi = -1 & qixingi + 1 = num1 & qixingi + 2 = -1& qixingi + 3 = num1 & qixingi
26、+ 4 = num1& qixing5 = -1) return 800;/ ?A?AA?for (i = 0; i = 3; i+) if (qixingi = -1 & qixingi + 1 = num1& qixingi + 2 = num1 & 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 = n
27、um1 & qixingi + 4 = num1) return 600;/ A?AAfor (i = 0; i = 4; i+) 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 + 4 = num1
28、) 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; i = 4; i+) if (qixingi = num1 & qixingi + 1 = num1& qixingi + 2 = -1 & qixin
29、gi + 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 & 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 = num
30、1& qixingi + 5 = -1 & qixingi + 6 = -1) return 250;/ ?A?A?for (i = 0; i = 3; i+) 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,作为面板。加入了两个容器,一个是棋盘,一个是容器包含两个
31、按钮。实现了两个按钮的功能-调用棋盘类的函数去重绘并且选择算法。package 绘制图形1;import java.awt.Graphics;import java.awt.Color;import javax.swing.*;import javax.swing.event.*;import java.awt.event.*;import java.awt.*;public class Chessboard extends JFrame implements ActionListener private JButton jbt1 = new JButton(人人对战);private JBu
32、tton jbt2 = new JButton(人机对战);/private JButton jbt3 = new JButton(悔棋);/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.ad
33、d(jbt1);p1.add(jbt2);/p1.add(jbt3);container.add(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.getA
34、ctionCommand() = 人人对战) / zhegeqipan.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() = 悔棋)/System.out.println(悔棋n);/ System.out.println(The+e.getActionCommand()+button is+clicked atn/*+new/