您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > C/C++资料 > JAVA五子棋游戏源代码(人机对战)
//Java编程:五子棋游戏源代码importjava.awt.*;importjava.awt.event.*;importjava.applet.*;importjavax.swing.*;importjava.io.PrintStream;importjavax.swing.JComponent;importjavax.swing.JPanel;/**main方法创建了ChessFrame类的一个实例对象(cf),*并启动屏幕显示显示该实例对象。**/publicclassFiveChessAppletDemo{publicstaticvoidmain(Stringargs[]){ChessFramecf=newChessFrame();cf.show();}}/**类ChessFrame主要功能是创建五子棋游戏主窗体和菜单**/classChessFrameextendsJFrameimplementsActionListener{privateString[]strsize={20x15,30x20,40x30};privateString[]strmode={人机对弈,人人对弈};publicstaticbooleaniscomputer=true,checkcomputer=true;privateintwidth,height;privateChessModelcm;privateMainPanelmp;//构造五子棋游戏的主窗体publicChessFrame(){this.setTitle(五子棋游戏);cm=newChessModel(1);mp=newMainPanel(cm);Containercon=this.getContentPane();con.add(mp,Center);this.setResizable(false);this.addWindowListener(newChessWindowEvent());MapSize(20,15);JMenuBarmbar=newJMenuBar();this.setJMenuBar(mbar);JMenugameMenu=newJMenu(游戏);mbar.add(makeMenu(gameMenu,newObject[]{开局,棋盘,模式,null,退出},this));JMenulookMenu=newJMenu(视图);mbar.add(makeMenu(lookMenu,newObject[]{Metal,Motif,Windows},this));JMenuhelpMenu=newJMenu(帮助);mbar.add(makeMenu(helpMenu,newObject[]{关于},this));}//构造五子棋游戏的主菜单publicJMenumakeMenu(Objectparent,Objectitems[],Objecttarget){JMenum=null;if(parentinstanceofJMenu)m=(JMenu)parent;elseif(parentinstanceofString)m=newJMenu((String)parent);elsereturnnull;for(inti=0;iitems.length;i++)if(items[i]==null)m.addSeparator();elseif(items[i]==棋盘){JMenujm=newJMenu(棋盘);ButtonGroupgroup=newButtonGroup();JRadioButtonMenuItemrmenu;for(intj=0;jstrsize.length;j++){rmenu=makeRadioButtonMenuItem(strsize[j],target);if(j==0)rmenu.setSelected(true);jm.add(rmenu);group.add(rmenu);}m.add(jm);}elseif(items[i]==模式){JMenujm=newJMenu(模式);ButtonGroupgroup=newButtonGroup();JRadioButtonMenuItemrmenu;for(inth=0;hstrmode.length;h++){rmenu=makeRadioButtonMenuItem(strmode[h],target);if(h==0)rmenu.setSelected(true);jm.add(rmenu);group.add(rmenu);}m.add(jm);}elsem.add(makeMenuItem(items[i],target));returnm;}//构造五子棋游戏的菜单项publicJMenuItemmakeMenuItem(Objectitem,Objecttarget){JMenuItemr=null;if(iteminstanceofString)r=newJMenuItem((String)item);elseif(iteminstanceofJMenuItem)r=(JMenuItem)item;elsereturnnull;if(targetinstanceofActionListener)r.addActionListener((ActionListener)target);returnr;}//构造五子棋游戏的单选按钮式菜单项publicJRadioButtonMenuItemmakeRadioButtonMenuItem(Objectitem,Objecttarget){JRadioButtonMenuItemr=null;if(iteminstanceofString)r=newJRadioButtonMenuItem((String)item);elseif(iteminstanceofJRadioButtonMenuItem)r=(JRadioButtonMenuItem)item;elsereturnnull;if(targetinstanceofActionListener)r.addActionListener((ActionListener)target);returnr;}publicvoidMapSize(intw,inth){setSize(w*20+50,h*20+100);if(this.checkcomputer)this.iscomputer=true;elsethis.iscomputer=false;mp.setModel(cm);mp.repaint();}publicbooleangetiscomputer(){returnthis.iscomputer;}publicvoidrestart(){intmodeChess=cm.getModeChess();if(modeChess=3&&modeChess=1){cm=newChessModel(modeChess);MapSize(cm.getWidth(),cm.getHeight());}else{System.out.println(\u81EA\u5B9A\u4E49);}}publicvoidactionPerformed(ActionEvente){Stringarg=e.getActionCommand();try{if(arg.equals(Windows))UIManager.setLookAndFeel(com.sun.java.swing.plaf.windows.WindowsLookAndFeel);elseif(arg.equals(Motif))UIManager.setLookAndFeel(com.sun.java.swing.plaf.motif.MotifLookAndFeel);elseUIManager.setLookAndFeel(javax.swing.plaf.metal.MetalLookAndFeel);SwingUtilities.updateComponentTreeUI(this);}catch(Exceptionee){}if(arg.equals(20x15)){this.width=20;this.height=15;cm=newChessModel(1);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals(30x20)){this.width=30;this.height=20;cm=newChessModel(2);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals(40x30)){this.width=40;this.height=30;cm=newChessModel(3);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals(人机对弈)){this.checkcomputer=true;this.iscomputer=true;cm=newChessModel(cm.getModeChess());MapSize(cm.getWidth(),cm.getHeight());SwingUtilities.updateComponentTreeUI(this);}if(arg.equals(人人对弈)){this.checkcomputer=false;this.iscomputer=false;cm=newChessModel(cm.getModeChess());MapSize(cm.getWidth(),cm.getHeight());SwingUtilities.updateComponentTreeUI(this);}if(arg.equals(开局)){restart();}if(arg.equals(关于))JOptionPane.showMessageDialog(this,五子棋游戏测试版本,关于,0);if(arg.equals(退出))System.exit(0);}}/**类ChessModel实现了整个五子棋程序算法的核心*/classChessModel{//棋盘的宽度、高度、棋盘的模式(如20×15)privateintwidth,height,modeChess;//棋盘方格的横向、纵向坐标privateintx=0,y=0;//棋盘方格的横向、纵向坐标所对应的棋子颜色,//数组arrMapShow只有3个值:1,2,3,-5,//其中1代表该棋盘方格上下的棋子为黑子,//2代表该棋盘方格上下的棋子为白子,//3代表为该棋盘方格上没有棋子,//-5代表该棋盘方格不能够下棋子privateint[][]arrMapShow;//交换棋手的标识,棋盘方格上是否有棋子的标识符privatebooleanisOdd,isExist;publicChessModel(){}//该构造方法根据不同的棋盘模式(modeChess)来构建对应大小的棋盘publicChessModel(intmodeChess){this.isOdd=true;if(modeChess==1){PanelInit(20,15,modeChess);}if(modeChess==2){PanelInit(30,20,modeChess);}if(modeChess==3){PanelInit(40,30,modeChess);}}//按照棋盘模式构建棋盘大小privatevoidPa
本文标题:JAVA五子棋游戏源代码(人机对战)
链接地址:https://www.777doc.com/doc-7203091 .html