1、算法课程设计五子棋 算法设计与分析课程设计说明书 五子棋游戏 学 院: 学生姓名: 专 业: 班 级: 完成时间: 一、简介.21.1 课程设计意义1.2 课设目的1.3 课设要求二、五子棋介绍.22.1 五子棋的棋子2.2 五子棋规则三.程序流程.3四.代码设计与分析.3五.程序调试与运行.23六.结论.30附录一:源代码.30 一、简介1.1 课程设计意义五子棋是起源于中国古代的传统黑白棋种之一。五子棋不仅能增强思维能力,提高智力,而且富含哲理,有助于修身养性。经过一个学期的学习,使我对算法有了一定的了解。但要深入理解算法,应用算法解决实际问题,单靠上课是不够的。必须通过课程设计,设计一个
2、比较大的程序,才能更深一步地理解。在这次课程中,我选择了网络五子棋作为课题。编写网络五子棋游戏要利用到算法来解决五子棋中五子连珠的实现。1.2 课设目的通过课程设计,使学生理论联系实际,在程序设计实践中进一步了解算法的实现,并了解JAVA程序设计方法掌握类和对象的基本编程方法,熟悉GUI图形界面编程技术,初步了解Socket编程方法。1.3 课设要求(1)设计游戏的核心算法;(2)设计可视化的界面且双方所使用的两色棋子和彩色棋盘得体大方、比例恰当;(3)双方轮流用键盘或鼠标下子。只有当鼠标单击在棋盘网格交叉点附近时才可下子,点击其他地方及已有子的位置不能下子,且应给出相应提示;(4)在适当位置
3、可显示比赛结果,比赛中能随时存储当前棋局。二、五子棋介绍2.1 五子棋的棋子五子棋采用两种颜色棋子,黑色棋子和白色棋子,和围棋相同,2.2 五子棋规则五子棋就是五个棋子连在一起就算赢,黑棋先行,下棋下在棋盘交叉线上,由于黑棋先行,优势太大,所以对黑棋设了禁手,又规定了“三手交换”,就是黑棋下第 2 手棋,盘面第 3 着棋之后,白方在应白 2 之前,如感觉黑方棋形不利于己方,可出交换,即执白棋一方变为执黑棋一方。和“五手两打法”,就是黑棋在下盘面上关键的第 5 手时,必须下两步棋,让白方在这两步棋中任选一步,然后再续下。不过一般爱好者不需要遵循这么多规则。三.程序流程 四.代码设计与分析main
4、方法创建了ChessFrame类的一个实例对象(cf),并启动屏幕显示显示该实例对象。public class FiveChessAppletDemo public static void main(String args) ChessFrame cf = new ChessFrame(); cf.show();用类ChessFrame创建五子棋游戏主窗体和菜单import java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.*;import java.io.PrintStream;import
5、javax.swing.JComponent;import javax.swing.JPanel;class ChessFrame extends JFrame implements ActionListener private String strsize=标准棋盘,改进棋盘,扩大棋盘;private String strmode=人机对战,人人对战;public static boolean iscomputer=true,checkcomputer=true;private int width,height;private ChessModel cm;private MainPanel
6、mp;构造五子棋游戏的主窗体public ChessFrame() this.setTitle(五子棋游戏); cm=new ChessModel(1); mp=new MainPanel(cm); Container con=this.getContentPane(); con.add(mp,Center); this.setResizable(false); this.addWindowListener(new ChessWindowEvent(); MapSize(14,14); JMenuBar mbar = new JMenuBar(); this.setJMenuBar(mbar)
7、; JMenu gameMenu = new JMenu(游戏); mbar.add(makeMenu(gameMenu, new Object 开局, null,棋盘,null,模式, null, 退出 , this); JMenu lookMenu =new JMenu(外观); mbar.add(makeMenu(lookMenu,new Object 类型一,类型二,类型三 ,this); JMenu helpMenu = new JMenu(版本); mbar.add(makeMenu(helpMenu, new Object 关于 , this);构造五子棋游戏的主菜单public
8、 JMenu makeMenu(Object parent, Object items, Object target) JMenu m = null; if(parent instanceof JMenu) m = (JMenu)parent; else if(parent instanceof String) m = new JMenu(String)parent); else return null; for(int i = 0; i items.length; i+) if(itemsi = null) m.addSeparator(); else if(itemsi = 棋盘) JMe
9、nu jm = new JMenu(棋盘); ButtonGroup group=new ButtonGroup(); JRadioButtonMenuItem rmenu; for (int j=0;jstrsize.length;j+) rmenu=makeRadioButtonMenuItem(strsizej,target); if (j=0) rmenu.setSelected(true); jm.add(rmenu); group.add(rmenu); m.add(jm); else if(itemsi = 模式) JMenu jm = new JMenu(模式); Button
10、Group group=new ButtonGroup(); JRadioButtonMenuItem rmenu; for (int h=0;hstrmode.length;h+) rmenu=makeRadioButtonMenuItem(strmodeh,target); if(h=0) rmenu.setSelected(true); jm.add(rmenu); group.add(rmenu); m.add(jm); else m.add(makeMenuItem(itemsi, target); return m;构造五子棋游戏的菜单项public JMenuItem makeM
11、enuItem(Object item, Object target) JMenuItem r = null; if(item instanceof String) r = new JMenuItem(String)item); else if(item instanceof JMenuItem) r = (JMenuItem)item; else return null; if(target instanceof ActionListener) r.addActionListener(ActionListener)target); return r;构造五子棋游戏的单选按钮式菜单项publi
12、c JRadioButtonMenuItem makeRadioButtonMenuItem( Object item, Object target) JRadioButtonMenuItem r = null; if(item instanceof String) r = new JRadioButtonMenuItem(String)item); else if(item instanceof JRadioButtonMenuItem) r = (JRadioButtonMenuItem)item; else return null; if(target instanceof Action
13、Listener) r.addActionListener(ActionListener)target); return r; public void MapSize(int w,int h) setSize(w * 24, h * 27); if(this.checkcomputer) this.iscomputer=true; else this.iscomputer=false; mp.setModel(cm); mp.repaint(); public boolean getiscomputer() return this.iscomputer; public void restart
14、() int modeChess = cm.getModeChess(); if(modeChess = 0) cm = new ChessModel(modeChess); MapSize(cm.getWidth(),cm.getHeight(); public void actionPerformed(ActionEvent e) String arg=e.getActionCommand(); try if (arg.equals(类型三) UIManager.setLookAndFeel( com.sun.java.swing.plaf.windows.WindowsLookAndFe
15、el); else if(arg.equals(类型二) UIManager.setLookAndFeel( com.sun.java.swing.plaf.motif.MotifLookAndFeel); else UIManager.setLookAndFeel( javax.swing.plaf.metal.MetalLookAndFeel ); SwingUtilities.updateComponentTreeUI(this); catch(Exception ee) if(arg.equals(标准棋盘) this.width=14; this.height=14; cm=new
16、ChessModel(1); MapSize(this.width,this.height); SwingUtilities.updateComponentTreeUI(this); if(arg.equals(改进棋盘) this.width=18; this.height=18; cm=new ChessModel(2); MapSize(this.width,this.height); SwingUtilities.updateComponentTreeUI(this); if(arg.equals(扩大棋盘) this.width=22; this.height=22; cm=new
17、ChessModel(3); MapSize(this.width,this.height); SwingUtilities.updateComponentTreeUI(this); if(arg.equals(人机对战) this.checkcomputer=true; this.iscomputer=true; cm=new ChessModel(cm.getModeChess(); MapSize(cm.getWidth(),cm.getHeight(); SwingUtilities.updateComponentTreeUI(this); if(arg.equals(人人对战) th
18、is.checkcomputer=false; this.iscomputer=false; cm=new ChessModel(cm.getModeChess(); MapSize(cm.getWidth(),cm.getHeight(); SwingUtilities.updateComponentTreeUI(this); if(arg.equals(开局) restart(); if(arg.equals(关于) JOptionPane.showMessageDialog(null, 第一版, 版本,JOptionPane.PLAIN_MESSAGE ); if(arg.equals(
19、退出) System.exit(0);用类ChessModel实现了整个五子棋程序算法的核心import java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.*;import java.io.PrintStream;import javax.swing.JComponent;import javax.swing.JPanel;class ChessModel 规定棋盘的宽度、高度、棋盘的模式private int width,height,modeChess;规定棋盘方格的横向、纵向坐标priva
20、te int x=0,y=0;棋盘方格的横向、纵向坐标所对应的棋子颜色,数组arrMapShow只有3个值:1,2,3,-1,其中1代表该棋盘方格上下的棋子为黑子,2代表该棋盘方格上下的棋子为白子,3代表为该棋盘方格上没有棋子,-1代表该棋盘方格不能够下棋子private int arrMapShow;交换棋手的标识,棋盘方格上是否有棋子的标识符private boolean isOdd,isExist;public ChessModel() 该构造方法根据不同的棋盘模式(modeChess)来构建对应大小的棋盘public ChessModel(int modeChess) this.isO
21、dd=true; if(modeChess = 1) PanelInit(14, 14, modeChess); if(modeChess = 2) PanelInit(18, 18, modeChess); if(modeChess = 3) PanelInit(22, 22, modeChess); 按照棋盘模式构建棋盘大小private void PanelInit(int width, int height, int modeChess) this.width = width; this.height = height; this.modeChess = modeChess; arrM
22、apShow = new intwidth+1height+1; for(int i = 0; i = width; i+) for(int j = 0; j = width+20 | x = height+20 | y 0;计算棋盘上某一方格上八个方向棋子的最大值,这八个方向分别是:左、右、上、下、左上、左下、右上、右下public boolean chessExist(int i,int j) if(this.arrMapShowij=1 | this.arrMapShowij=2) return true; return false;判断该坐标位置是否可下棋子public void re
23、adyplay(int x,int y) if(badxy(x,y) return; if (chessExist(x,y) return; this.arrMapShowxy=3;在该坐标位置下棋子public void play(int x,int y) if(badxy(x,y) return; if(chessExist(x,y) this.isExist=true; return; else this.isExist=false; if(getisOdd() setisOdd(false); this.arrMapShowxy=1; else setisOdd(true); this
24、.arrMapShowxy=2; 计算机走棋说明:用穷举法判断每一个坐标点的四个方向的的最大棋子数,最后得出棋子数最大值的坐标,下子public void computerDo(int width,int height) int max_black,max_white,max_temp,max=0; setisOdd(true); System.out.println(计算机走棋 .); for(int i = 0; i = width; i+)for(int j = 0; j max) max=max_temp; this.x=i; this.y=j; setX(this.x); setY(
25、this.y); this.arrMapShowthis.xthis.y=2;记录电脑下子后的横向坐标public void setX(int x) this.x=x;记录电脑下子后的纵向坐标public void setY(int y) this.y=y;获取电脑下子的横向坐标public int getX() return this.x;获取电脑下子的纵向坐标public int getY() return this.y;计算棋盘上某一方格上八个方向棋子的最大值,这八个方向分别是:左、右、上、下、左上、左下、右上、右下public int checkMax(int x, int y,int black_or_white) int num=0,max_num,max_temp=0; int x_temp=x,y_temp=y; int x_temp1=x_temp,y_temp1=y_temp; 判断右边 for(int i=1;ithis.width) break; if(this.arrMapShowx_temp1y_temp1=black_or_white) num+; else break; 判断左边 x_temp1=x_temp
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1