天津科技大学Java课程设计(共13页)_第1页
天津科技大学Java课程设计(共13页)_第2页
天津科技大学Java课程设计(共13页)_第3页
天津科技大学Java课程设计(共13页)_第4页
天津科技大学Java课程设计(共13页)_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上高级语言程序设计期末作业题目五子棋 学 号:王琪姓 名:指导老师:程岚岚时间:2013年12月161、 开发工具:MyEclipse 2、 程序的功能介绍系统实现五子棋人人对战。3、 系统分析与设计(一)五子棋基本规则 1.五子棋行棋时,黑棋先下第一子,由天元开始,后白棋在黑棋周围的交叉点的落子。接着黑方再以天元中心的25个交叉点的范围内落盘面的第三子,之后黑白双方相互顺序子。    2.最先在棋盘线交点横向,纵向,斜向形成已方的五个棋子连续的一方为胜。    3.出现禁手对方指出后将判负,其中包括两个或两个

2、以上的活三,活四,长连均称之为禁手。    4.禁手只对黑棋有效,白棋无禁手。    5.黑方禁手形成时,白方需立即指出,黑方将被判负。若白方未发现黑方禁手存在而继续应子,其后指出黑方禁手不能判黑方负。    6.黑方连五与禁手同时形成,因黑方已连成五即黑方已获胜,故禁手规则失效。    7.在对局中,在盘上落下的子又拿起来,此动作称为拔子,若拔子将被判为负。    8.在对局中棋子掉落在棋盘上将被判负。若推子或蹭子,以盘面第一落点为准。用手将棋

3、子推正不算违犯规则。    9.在对局中,一方自行中止比赛如:中途退场,将被判负。    10.在对局中对方宣布认输,本局获胜。    11.超过比赛规定所用时间限制,将被判为负:正式比赛期间,迟到时间超过比赛容许时间将被判负。    12.如下至最终一子仍不分胜负则定为平局。13.中盘期间双方 同意和局提议,判定为平局(二)(1)1. 本款游戏有“游戏开始”和“重置游戏”和“黑白哪个子先行”三个选项。2. 绘制棋盘,14条横线,14条竖线,在直线交点处下棋子(实心黑白圆形)

4、。3.棋盘处于鼠标监听状态,当鼠标在棋盘上有点击操作的时候,程序会获得鼠标点击的坐标然后换算成对应的棋盘的位置,再判断此处是否有棋子,假如没有,那么在此处画出对应颜色的实心棋子,假如已经有棋子了,则提示玩家此处已经有棋子请重新下棋。4、 系统功能实现及部分核心代码(1) 系统功能实现1游戏开始界面选择哪种子先开始.2游戏开始进行3游戏结束(2)import java.awt.*;import java.awt.event.*;import java.applet.Applet;import java.awt.Color;public class GoBang extends Applet im

5、plements ActionListener, MouseListener, MouseMotionListener, ItemListener int color = 0;/ 旗子的颜色标识 0:白子 1:黑子 boolean isStart = false;/ 游戏开始标志 int bodyArray = new int1616; / 设置棋盘棋子状态 0 无子 1 白子 2 黑子 Button b1 = new Button("游戏开始"); Button b2 = new Button("重置游戏"); Label lblWin = new L

6、abel(" "); Checkbox ckbHB = new Checkbox2; CheckboxGroup ckgHB = new CheckboxGroup(); public void init() setLayout(null); addMouseListener(this); add(b1); b1.setBounds(330, 50, 80, 30); b1.addActionListener(this); add(b2); b2.setBounds(330, 90, 80, 30); b2.addActionListener(this); ckbHB0 =

7、 new Checkbox("白子先", ckgHB, false); ckbHB0.setBounds(320, 20, 60, 30); ckbHB1 = new Checkbox("黑子先", ckgHB, false); ckbHB1.setBounds(380, 20, 60, 30); add(ckbHB0); add(ckbHB1); ckbHB0.addItemListener(this); ckbHB1.addItemListener(this); add(lblWin); lblWin.setBounds(330, 130, 80,

8、30); gameInit(); this.resize(new Dimension(450,350); public void itemStateChanged(ItemEvent e) if (ckbHB0.getState() / 选择黑子先还是白子先 color = 0; else color= 1; public void actionPerformed(ActionEvent e) if (e.getSource() = b1) gameStart(); else reStart(); public void mousePressed(MouseEvent e) public vo

9、id mouseClicked(MouseEvent e) int x1, y1; x1 = e.getX(); y1 = e.getY(); if (e.getX() < 20 | e.getX() > 300 | e.getY() < 20 | e.getY() > 300) return; if (x1 % 20 > 10) x1 += 20; if (y1 % 20 > 10) y1 += 20; x1 = x1 / 20 * 20; y1 = y1 / 20 * 20; setDown(x1, y1); public void mouseEnter

10、ed(MouseEvent e) public void mouseExited(MouseEvent e) public void mouseReleased(MouseEvent e) public void mouseDragged(MouseEvent e) public void mouseMoved(MouseEvent e) public void paint(Graphics g) g.setColor(Color.lightGray); g.fill3DRect(10, 10, 300, 300, true); g.setColor(Color.black); for (in

11、t i = 1; i < 16; i+) g.drawLine(20, 20 * i, 300, 20 * i); g.drawLine(20 * i, 20, 20 * i, 300); public void setDown(int x, int y) / 落子 if (!isStart) / 判断游戏未开始 return; if (bodyArrayx / 20y / 20 != 0) return; Graphics g = getGraphics(); if (color = 1)/ 判断黑子还是白子 g.setColor(Color.black); color = 0; el

12、se g.setColor(Color.white); color = 1; g.fillOval(x - 10, y - 10, 20, 20); bodyArrayx / 20y / 20 = color + 1; if (gameWin1(x / 20, y / 20) / 判断输赢 lblWin.setText(startColor(color) + "赢了!"); isStart = false; if (gameWin2(x / 20, y / 20) / 判断输赢 lblWin.setText(startColor(color) + "赢了!&quo

13、t;); isStart = false; if (gameWin3(x / 20, y / 20) / 判断输赢 lblWin.setText(startColor(color) + "赢了!"); isStart = false; if (gameWin4(x / 20, y / 20) / 判断输赢 lblWin.setText(startColor(color) + "赢了!"); isStart = false; public String startColor(int x) if (x = 0) return "黑子" e

14、lse return "白子" public void gameStart() / 游戏开始 isStart = true; enableGame(false); b2.setEnabled(true); public void gameInit() / 游戏开始初始化 isStart = false; enableGame(true); b2.setEnabled(false); ckbHB0.setState(true); for (int i = 0; i < 16; i+) for (int j = 0; j < 16; j+) bodyArrayij

15、= 0; lblWin.setText(""); public void reStart() / 游戏重新开始 repaint(); gameInit(); public void enableGame(boolean e) / 设置组件状态 b1.setEnabled(e); b2.setEnabled(e); ckbHB0.setEnabled(e); ckbHB1.setEnabled(e); public boolean gameWin1(int x, int y) / 判断输赢 横 int x1, y1, t = 1; x1 = x; y1 = y; for (i

16、nt i = 1; i < 5; i+) if (x1 > 15) break; if (bodyArrayx1 + iy1 = bodyArrayxy) t += 1; else break; for (int i = 1; i < 5; i+) if (x1 < 1) break; if (bodyArrayx1 - iy1 = bodyArrayxy) t += 1; else break; if (t > 4) return true; else return false; public boolean gameWin2(int x, int y) / 判

17、断输赢 竖 int x1, y1, t = 1; x1 = x; y1 = y; for (int i = 1; i < 5; i+) if (x1 > 15) break; if (bodyArrayx1y1 + i = bodyArrayxy) t += 1; else break; for (int i = 1; i < 5; i+) if (x1 < 1) break; if (bodyArrayx1y1 - i = bodyArrayxy) t += 1; else break; if (t > 4) return true; else return f

18、alse; public boolean gameWin3(int x, int y) / 判断输赢 左斜 int x1, y1, t = 1; x1 = x; y1 = y; for (int i = 1; i < 5; i+) if (x1 > 15) break; if (bodyArrayx1 + iy1 - i = bodyArrayxy) t += 1; else break; for (int i = 1; i < 5; i+) if (x1 < 1) break; if (bodyArrayx1 - iy1 + i = bodyArrayxy) t += 1; else break; if (t > 4) return true; else return false; public boolean gameWin4(int x, int y) / 判断输赢 左斜 int x1, y1, t = 1; x1 = x; y1 = y; for (int i = 1; i < 5; i+) if (x1 > 15) break; if (bodyArrayx1 + iy1 + i = bodyArrayxy) t += 1; els

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论