1、天津科技大学Java课程设计高级语言程序设计期末作业题目五子棋 学 号:王琪姓 名:11103333指导老师:程岚岚时间:2013年12月161、开发工具:MyEclipse 2、程序的功能介绍系统实现五子棋人人对战。3、系统分析与设计(一)五子棋基本规则 1.五子棋行棋时,黑棋先下第一子,由天元开始,后白棋在黑棋周围的交叉点的落子。接着黑方再以天元中心的25个交叉点的范围内落盘面的第三子,之后黑白双方相互顺序子。 2.最先在棋盘线交点横向,纵向,斜向形成已方的五个棋子连续的一方为胜。 3.出现禁手对方指出后将判负,其中包括两个或两个以上的活三,活四,长连均称之为禁手。 4.禁手只对黑棋有效,
2、白棋无禁手。 5.黑方禁手形成时,白方需立即指出,黑方将被判负。若白方未发现黑方禁手存在而继续应子,其后指出黑方禁手不能判黑方负。 6.黑方连五与禁手同时形成,因黑方已连成五即黑方已获胜,故禁手规则失效。 7.在对局中,在盘上落下的子又拿起来,此动作称为拔子,若拔子将被判为负。 8.在对局中棋子掉落在棋盘上将被判负。若推子或蹭子,以盘面第一落点为准。用手将棋子推正不算违犯规则。 9.在对局中,一方自行中止比赛如:中途退场,将被判负。 10.在对局中对方宣布认输,本局获胜。 11.超过比赛规定所用时间限制,将被判为负:正式比赛期间,迟到时间超过比赛容许时间将被判负。 12.如下至最终一子仍不分胜
3、负则定为平局。13.中盘期间双方 同意和局提议,判定为平局(二)(1)1. 本款游戏有“游戏开始”和“重置游戏”和“黑白哪个子先行”三个选项。2. 绘制棋盘,14条横线,14条竖线,在直线交点处下棋子(实心黑白圆形)。3.棋盘处于鼠标监听状态,当鼠标在棋盘上有点击操作的时候,程序会获得鼠标点击的坐标然后换算成对应的棋盘的位置,再判断此处是否有棋子,假如没有,那么在此处画出对应颜色的实心棋子,假如已经有棋子了,则提示玩家此处已经有棋子请重新下棋。4、系统功能实现及部分核心代码(1) 系统功能实现1游戏开始界面选择哪种子先开始.2游戏开始进行3游戏结束(2)import java.awt.*;im
4、port java.awt.event.*;import java.applet.Applet;import java.awt.Color;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
5、黑子 Button b1 = new Button(游戏开始); Button b2 = new 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(b
6、2); b2.setBounds(330, 90, 80, 30); 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
7、); add(lblWin); 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(); p
8、ublic void mousePressed(MouseEvent e) public void mouseClicked(MouseEvent e) int x1, y1; x1 = e.getX(); y1 = e.getY(); if (e.getX() 300 | 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 mouseEntered(MouseEve
9、nt 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 (int i = 1; i
10、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; else g.setColor(Co
11、lor.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) + 赢了!); isStart = false; if (gameWin3(x / 2
12、0, 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 黑子; else return 白子; public void gameStart() / 游戏开始 isStart = true; enableGame(fa
13、lse); 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 = 0; lblWin.setText(); public void reStart() / 游戏重新开始 repaint(); gameInit(); public void enableG
14、ame(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 = 1; i 15) break; if (bodyArrayx1 + iy1 = bodyArrayxy) t += 1; else break; for (int i = 1; i 5; i+) if
15、(x1 4) return true; else return false; public boolean gameWin2(int x, int y) / 判断输赢 竖 int x1, y1, t = 1; x1 = x; y1 = y; for (int i = 1; i 15) break; if (bodyArrayx1y1 + i = bodyArrayxy) t += 1; else break; for (int i = 1; i 5; i+) if (x1 4) return true; else return false; public boolean gameWin3(in
16、t x, int y) / 判断输赢 左斜 int x1, y1, t = 1; x1 = x; y1 = y; for (int i = 1; i 15) break; if (bodyArrayx1 + iy1 - i = bodyArrayxy) t += 1; else break; for (int i = 1; i 5; i+) if (x1 4) return true; else return false; public boolean gameWin4(int x, int y) / 判断输赢 左斜 int x1, y1, t = 1; x1 = x; y1 = y; for
17、 (int i = 1; i 15) break; if (bodyArrayx1 + iy1 + i = bodyArrayxy) t += 1; else break; for (int i = 1; i 5; i+) if (x1 4) return true; else return false; 5、小结(开发中遇到的问题,怎么解决的)通过对五子棋游戏的编写,使自己对java语言有了更深的了解。也更加熟悉和了解了java开发工具Eclipse的使用。不过还有很多不足之处,比如没有能设置禁手,没有能设置悔棋,还有很多东西可以扩充完善。欢迎您的下载,资料仅供参考!致力为企业和个人提供合同协议,策划案计划书,学习课件等等打造全网一站式需求
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1