1、Java 语言程序设计课程设计报告学 院: 信息科学技术学院 班 级: 软件技术 2 班 姓 名: 王更新 学 号: 1108900505 指导教师: 郭韶升 课设时间: 2014-03-17 至 2014-03-26 二 O 一四 年 三月 二十六 日1目 录一、设计要求2二、设计步骤22.1 程序流程图.22.2 程序的功能分配.3三、设计正文63.1 创建棋盘类.63.2 判断输赢功能实现.93.3 测试结果.9四、心得体会.12五、参考文献.12附录 (源代码).132一、课程设计要求设计一个 1515 围棋棋盘,由两玩家交替进行对战,并可以实现以下功能:1.选择落子的先后顺序 2.重
2、置棋盘3.刷新重新开始4.退出提示并且规定退出者判为负,但退出过程中要有提示。以防不小心点错了。最后判断某一方是否为五子连珠。实现一个简单的多用户五子棋的游戏程序,包括如下两个界面(1)选择对弈桌(执黑、执白) 。(2)在游戏界面,有开始,退出(游戏未结束、点退出自动判负) ;二、设计步骤2.1 程序流程图32.2 程序的功能分配a. 棋盘的绘制public void draw_qipan(Graphics G) /画棋盘 15*15G.setColor(Color.lightGray);G.fill3DRect(10,10,300,300,true);G.setColor(Color.bla
3、ck);for(int i=1;i300 | e.getY()300) return;if (x1%2010) x1+=10;if(y1%2010) y1+=10;x1=x1/20*20;y1=y1/20*20;set_Qizi(x1,y1);m*=(-1);4d. 按钮的触发事件public void actionPerformed(ActionEvent e) Graphics g=getGraphics();if (e.getSource()=b1) Game_start();elseGame_re();if(e.getSource()=exist)Game_re();lblWin.se
4、tText(Get_qizi_color(color_Qizi)+“输了!“);intGame_Start=0; e. 判断落子的位置及画出相应的黑白棋子public void set_Qizi(int x,int y) /落子if (intGame_Start=0) /判断游戏未开始return;if (intGame_Bodyx/20y/20!=0) return;Graphics g=getGraphics();if (color_Qizi=1)/判断黑子还是白子g.setColor(Color.black);color_Qizi=0;elseg.setColor(Color.white
5、);color_Qizi=1;g.fillOval(x-10,y-10,20,20);intGame_Bodyx/20y/20=color_Qizi+1;f. 判断胜负if (Game_win_1(x/20,y/20) /判断输赢 1lblWin.setText(Get_qizi_color(color_Qizi)+“赢了!“);intGame_Start=0;if (Game_win_2(x/20,y/20) /判断输赢 2lblWin.setText(Get_qizi_color(color_Qizi)+“赢了!“);5intGame_Start=0;if (Game_win_3(x/20
6、,y/20) /判断输赢 3lblWin.setText(Get_qizi_color(color_Qizi)+“赢了!“);intGame_Start=0;if (Game_win_4(x/20,y/20) /判断输赢 4lblWin.setText(Get_qizi_color(color_Qizi)+“赢了!“);intGame_Start=0; 6三、设计正文3.1 创建棋盘类Public class WcyChess extends Applet implements ActionListener,MouseListener,MouseMotionListener,ItemListe
7、nerint color_Qizi=0;/旗子的颜色标识 0:白子 1:黑子int intGame_Start=0;/游戏开始标志 0 未开始 1 游戏中int intGame_Body=new int1616; /设置棋盘棋子状态int m=-1;Button b1=new Button(“开始“);Button b2=new Button(“重置游戏“);Label lblWin=new Label(“ “);Checkbox ckbHB=new Checkbox3;Button exist = new Button(“退出“); CheckboxGroup ckgHB=new Check
8、boxGroup();NetchatClient chat=new NetchatClient();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=new Checkbox(“执白“,ckgHB,false);ckbHB0.setBounds(320,20,60,30);
9、ckbHB1=new Checkbox(“执黑“,ckgHB,false);ckbHB1.setBounds(380,20,60,30);ckbHB2=new Checkbox(“观看“,ckgHB, false);add(ckbHB0);add(ckbHB1);add(ckbHB2);ckbHB0.addItemListener(this);ckbHB1.addItemListener(this);add(lblWin);lblWin.setBounds(330,180,80,30);lblWin.setBackground(Color.red);lblWin.setText(“胜利者!“)
10、;/没有显示?ckbHB2.setBounds(440, 20,60, 30);add(exist);exist.setBounds(330,130,80,30);7exist.addActionListener(this);/ add(chat);chat.setBounds(20, 500, 300, 300);chat.frame();chat.setVisible(true);Game_start_csh();setSize(500,600); setVisible(true);public void itemStateChanged(ItemEvent e) if (ckbHB0.g
11、etState() /选择黑子还是白子color_Qizi=0;elsecolor_Qizi=1;public void mousePressed(MouseEvent e)public void mouseClicked(MouseEvent e) Graphics g=getGraphics();int x1,y1;x1=e.getX();y1=e.getY();if (e.getX()300 | e.getY()300) return;if (x1%2010) x1+=10;if(y1%2010) y1+=10;x1=x1/20*20;y1=y1/20*20;set_Qizi(x1,y1
12、);m*=(-1);public void actionPerformed(ActionEvent e) Graphics g=getGraphics();if (e.getSource()=b1) Game_start();else Game_re();8if(e.getSource()=exist)Game_re();lblWin.setText(Get_qizi_color(color_Qizi)+“输了!“);intGame_Start=0;public void mouseEntered(MouseEvent e)public void mouseExited(MouseEvent
13、e)public void mouseReleased(MouseEvent e)public void mouseDragged(MouseEvent e)public void mouseMoved(MouseEvent e)public void paint(Graphics g) draw_qipan(g);3.2 判断输赢功能实现if (Game_win_1(x/20,y/20) /判断输赢 1lblWin.setText(Get_qizi_color(color_Qizi)+“赢了!“);intGame_Start=0;if (Game_win_2(x/20,y/20) /判断输赢
14、 2lblWin.setText(Get_qizi_color(color_Qizi)+“赢了!“);intGame_Start=0;if (Game_win_3(x/20,y/20) /判断输赢 3lblWin.setText(Get_qizi_color(color_Qizi)+“赢了!“);intGame_Start=0;if (Game_win_4(x/20,y/20) /判断输赢 4lblWin.setText(Get_qizi_color(color_Qizi)+“赢了!“);intGame_Start=0;93.3 测试结果a. 进入游戏界面游戏开始的界面有三个选择项,用户可以选
15、择相应的角色,选择完毕后点击开始进入游戏。10b. 选择角色,开始下棋首先达到五个棋子连在一块的赢了,并在红色区域显示谁赢了!c. 下完后,重新开始下完一盘后游戏停止,点击重新开始,界面回到初始界面,选择角色继续游戏.11d.游戏中点退出,自动判输在下棋的过程当中谁中途退出,即点击退出,系统自动判断谁输12四、课程设计心得体会通过此次课程设计,将我本学期所学的 JAVA 知识得到巩固和应用,在设计的过程中我遇到了很到问题,不过在老师和同学们的帮助和自己的思考下还是很好的完成了。这此课程设计还让我懂得了写程序不能闭门造车,要努力拓宽知识面,开阔视野,拓展思维。它还让我学会了在网上查阅那些无限的资
16、料。由于自己的分析设计和程序经验不足,该系统设计和实现过程中,还有许多没有完善的地方,比如用户界面设计不够美观,异常出错处理比较差等多方面问题,这些都有待进一步完善和提高。对于文中出现的不足和系统中出现的问题敬请老师指导。五、参考文献1. 吴其庆编著.Java程序设计实例教程.北京:冶金工业出版社2. 柳西玲.许斌编著.Java语言应用开发基础.北京:清华大学出版社 3丁振凡 Java 语言实用教程 :北京邮电大学出版社13附录 (源代码)import .*;import java.io.*;import java.applet.*;import java.awt.*;import java.
17、awt.event.*;import java.applet.Applet;import java.awt.Color;Public class wuziqi extends Applet implements ActionListener,MouseListener,MouseMotionListener,ItemListenerint color_Qizi=0;/旗子的颜色标识 0:白子 1:黑子int intGame_Start=0;/游戏开始标志 0 未开始 1 游戏中int intGame_Body=new int1616; /设置棋盘棋子状态int m=-1;Button b1=n
18、ew Button(“开始“);Button b2=new Button(“重新开始“);Label lblWin=new Label(“ “);Checkbox ckbHB=new Checkbox3;Button exist = new Button(“退出“); CheckboxGroup ckgHB=new CheckboxGroup();public void init()setLayout(null);addMouseListener(this);add(b1);b1.setBounds(330,50,80,30);b1.addActionListener(this);add(b2
19、);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);ckbHB2=new Checkbox(“观看“,ckgHB, false);add(ckbHB0);add(ckbHB1);add(ckbHB2);ckbHB0.addItemListener(this);ck
20、bHB1.addItemListener(this);add(lblWin);lblWin.setBounds(330,180,80,30);lblWin.setBackground(Color.red);14lblWin.setText(“胜利者!“);/没有显示?ckbHB2.setBounds(440, 20,60, 30);add(exist);exist.setBounds(330,130,80,30);exist.addActionListener(this);Game_start_csh();setSize(500,600); setVisible(true);public vo
21、id itemStateChanged(ItemEvent e)if (ckbHB0.getState() /选择黑子还是白子color_Qizi=0;elsecolor_Qizi=1;public void mousePressed(MouseEvent e)public void mouseClicked(MouseEvent e)Graphics g=getGraphics();int x1,y1;x1=e.getX();y1=e.getY();if (e.getX()300 | e.getY()300)return;if (x1%2010)x1+=10;if(y1%2010)y1+=1
22、0;x1=x1/20*20;y1=y1/20*20;set_Qizi(x1,y1);m*=(-1)public void actionPerformed(ActionEvent e)Graphics g=getGraphics();if (e.getSource()=b1)Game_start();15elseGame_re();if(e.getSource()=exist)Game_re();/ color_Qizi=m;lblWin.setText(Get_qizi_color(color_Qizi)+“输了!“);intGame_Start=0;public void mouseEnte
23、red(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)draw_qipan(g);public void set_Qizi(int x,int y) /落子if (intGame_Start=0) /判断游戏未开始return;if (intGame_Bodyx
24、/20y/20!=0)return;Graphics g=getGraphics();if (color_Qizi=1)/判断黑子还是白子g.setColor(Color.black);color_Qizi=0;elseg.setColor(Color.white);color_Qizi=1;g.fillOval(x-10,y-10,20,20);intGame_Bodyx/20y/20=color_Qizi+1;if (Game_win_1(x/20,y/20) /判断输赢 1lblWin.setText(Get_qizi_color(color_Qizi)+“赢了!“);intGame_S
25、tart=0;16if (Game_win_2(x/20,y/20) /判断输赢 2lblWin.setText(Get_qizi_color(color_Qizi)+“赢了!“);intGame_Start=0;if (Game_win_3(x/20,y/20) /判断输赢 3lblWin.setText(Get_qizi_color(color_Qizi)+“赢了!“);intGame_Start=0;if (Game_win_4(x/20,y/20) /判断输赢 4lblWin.setText(Get_qizi_color(color_Qizi)+“赢了!“);intGame_Start
26、=0;public String Get_qizi_color(int x)if (x=0)return “黑子“;elsereturn “白子“;public void draw_qipan(Graphics G) /画棋盘 15*15G.setColor(Color.lightGray);G.fill3DRect(10,10,300,300,true);G.setColor(Color.black);for(int i=1;i15)break;if (intGame_Bodyx1+iy1=intGame_Bodyxy)t+=1;elsebreak;for (int i=1;i4)retur
27、n true;elsereturn false;public boolean Game_win_2(int x,int y) /纵向判断输赢int x1,y1,t=1;x1=x;y1=y;for (int i=1;i15)break;if (intGame_Bodyx1y1+i=intGame_Bodyxy)t+=1;elsebreak;for (int i=1;i4)return true;elsereturn false;public boolean Game_win_3(int x,int y) /左斜判断输赢 int x1,y1,t=1;x1=x;y1=y;for (int i=1;i
28、15)break;if (intGame_Bodyx1+iy1-i=intGame_Bodyxy)t+=1;elsebreak;for (int i=1;i4)return true;elsereturn false;public boolean Game_win_4(int x,int y) /左斜判断输赢 int x1,y1,t=1;x1=x;y1=y;for (int i=1;i15)break;if (intGame_Bodyx1+iy1+i=intGame_Bodyxy)t+=1;elsebreak;for (int i=1;i4)21return true;elsereturn false;