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

下载本文档

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

文档简介

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

2、,白棋无禁手。5. 黑方禁手形成时,白方需立即指出,黑方将被判负。若白方未发现黑方禁手存在而继续应子,其后指出黑方禁手不能判黑方负。6. 黑方连五与禁手同时形成,因黑方已连成五即黑方已获胜,故禁手规则失效。7. 在对局中,在盘上落下的子又拿起来,此动作称为拔子,若拔子将被判为负。8. 在对局中棋子掉落在棋盘上将被判负。若推子或蹭子,以盘面第一落点为准。用手将棋子推正不算违犯规则。9. 在对局中,一方自行中止比赛如:中途退场,将被判负。10. 在对局中对方宣布认输,本局获胜。11. 超过比赛规定所用时间限制,将被判为负:正式比赛期间,迟到时间超过比赛容许时间将被判负。12. 如下至最终一子仍不分

3、胜负则定为平局。13. 中盘期间双方 同意和局提议,判定为平局( 二) (1)1.本款游戏有“游戏开始”和“重置游戏”和“黑白哪个子先行”三个选项。-可编辑修改 -。2. 绘制棋盘, 14 条横线, 14 条竖线,在直线交点处下棋子(实心黑白圆形) 。3. 棋盘处于鼠标监听状态, 当鼠标在棋盘上有点击操作的时候, 程序会获得鼠标点击的坐标然后换算成对应的棋盘的位置,再判断此处是否有棋子,假如没有,那么在此处画出对应颜色的实心棋子,假如已经有棋子了, 则提示玩家此处已经有棋子请重新下棋。四、系统功能实现及部分核心代码(1)系统功能实现1 游戏开始界面选择哪种子先开始.2游戏开始进行-可编辑修改

4、-。3 游戏结束-可编辑修改 -。( 2)public class GoBang extends Applet implements 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 = n

5、ew Button("重置游戏 ");Label lblWin = new Label(" ");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, 3

6、0);b2.addActionListener(this);ckbHB0 = 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);

7、lblWin.setBounds(330, 130, 80, 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 mou

8、sePressed(MouseEvent e) public void 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

9、);-可编辑修改 -。public void mouseEntered(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

10、.setColor(Color.black);for (int 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.bl

11、ack);color = 0;-可编辑修改 -。 else 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(col

12、or) + "赢了 !");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 &quo

13、t;黑子 " else 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+) b

14、odyArrayij = 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 (int i =

15、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) /判断输赢竖

16、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 false

17、;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; e

18、lse 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; else break;for (int i = 1; i < 5; i+) if (x1 < 1) break;if (bodyArrayx1 - iy1

温馨提示

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

评论

0/150

提交评论