Java编程:五子棋游戏源代码.doc
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 编程 五子棋 游戏 源代码
- 资源描述:
-
______________________________________________________________________________________________________________ //Java编程:五子棋游戏源代码 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; /* *main方法创建了ChessFrame类的一个实例对象(cf), *并启动屏幕显示显示该实例对象。 **/ public class FiveChessAppletDemo { public static void main(String args[]){ ChessFrame cf = new ChessFrame(); cf.show(); } } /* *类ChessFrame主要功能是创建五子棋游戏主窗体和菜单 **/ class ChessFrame extends JFrame implements ActionListener { private String[] strsize={"20x15","30x20","40x30"}; private String[] strmode={"人机对弈","人人对弈"}; public static boolean iscomputer=true,checkcomputer=true; private int width,height; private ChessModel cm; private MainPanel 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(20,15); JMenuBar mbar = new JMenuBar(); this.setJMenuBar(mbar); JMenu gameMenu = new JMenu("游戏"); mbar.add(makeMenu(gameMenu, new Object[] { "开局", "棋盘","模式", null, "退出" }, this)); JMenu lookMenu =new JMenu("视图"); mbar.add(makeMenu(lookMenu,new Object[] { "Metal","Motif","Windows" },this)); JMenu helpMenu = new JMenu("帮助"); mbar.add(makeMenu(helpMenu, new Object[] { "关于" }, this)); } //构造五子棋游戏的主菜单 public 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(items[i] == null) m.addSeparator(); else if(items[i] == "棋盘"){ JMenu jm = new JMenu("棋盘"); ButtonGroup group=new ButtonGroup(); JRadioButtonMenuItem rmenu; for (int j=0;j<strsize.length;j++){ rmenu=makeRadioButtonMenuItem(strsize[j],target); if (j==0) rmenu.setSelected(true); jm.add(rmenu); group.add(rmenu); } m.add(jm); }else if(items[i] == "模式"){ JMenu jm = new JMenu("模式"); ButtonGroup group=new ButtonGroup(); JRadioButtonMenuItem rmenu; for (int h=0;h<strmode.length;h++){ rmenu=makeRadioButtonMenuItem(strmode[h],target); if(h==0) rmenu.setSelected(true); jm.add(rmenu); group.add(rmenu); } m.add(jm); }else m.add(makeMenuItem(items[i], target)); return m; } //构造五子棋游戏的菜单项 public JMenuItem makeMenuItem(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; } //构造五子棋游戏的单选按钮式菜单项 public 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 ActionListener) r.addActionListener((ActionListener)target); return r; } public void MapSize(int w,int h){ setSize(w * 20+50 , h * 20+100 ); if(this.checkcomputer) this.iscomputer=true; else this.iscomputer=false; mp.setModel(cm); mp.repaint(); } public boolean getiscomputer(){ return this.iscomputer; } public void restart(){ int modeChess = cm.getModeChess(); if(modeChess <= 3 && modeChess >= 1){ cm = new ChessModel(modeChess); MapSize(cm.getWidth(),cm.getHeight()); }else{ System.out.println("\u81EA\u5B9A\u4E49"); } } public void actionPerformed(ActionEvent e){ String arg=e.getActionCommand(); try{ if (arg.equals("Windows")) UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); else if(arg.equals("Motif")) 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("20x15")){ this.width=20; this.height=15; cm=new ChessModel(1); MapSize(this.width,this.height); SwingUtilities.updateComponentTreeUI(this); } if(arg.equals("30x20")){ this.width=30; this.height=20; cm=new ChessModel(2); MapSize(this.width,this.height); SwingUtilities.updateComponentTreeUI(this); } if(arg.equals("40x30")){ this.width=40; this.height=30; cm=new 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("人人对弈")){ this.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(this, "五子棋游戏测试版本", "关于", 0); if(arg.equals("退出")) System.exit(0); } } /* *类ChessModel实现了整个五子棋程序算法的核心 */ class ChessModel { //棋盘的宽度、高度、棋盘的模式(如20×15) private int width,height,modeChess; //棋盘方格的横向、纵向坐标 private int x=0,y=0; //棋盘方格的横向、纵向坐标所对应的棋子颜色, //数组arrMapShow只有3个值:1,2,3,-5, //其中1代表该棋盘方格上下的棋子为黑子, //2代表该棋盘方格上下的棋子为白子, //3代表为该棋盘方格上没有棋子, //-5代表该棋盘方格不能够下棋子 private int[][] arrMapShow; //交换棋手的标识,棋盘方格上是否有棋子的标识符 private boolean isOdd,isExist; public ChessModel() {} //该构造方法根据不同的棋盘模式(modeChess)来构建对应大小的棋盘 public ChessModel(int modeChess){ this.isOdd=true; if(modeChess == 1){ PanelInit(20, 15, modeChess); } if(modeChess == 2){ PanelInit(30, 20, modeChess); } if(modeChess == 3){ PanelInit(40, 30, modeChess); } } //按照棋盘模式构建棋盘大小 private void PanelInit(int width, int height, int modeChess){ this.width = width; this.height = height; this.modeChess = modeChess; arrMapShow = new int[width+1][height+1]; for(int i = 0; i <= width; i++){ for(int j = 0; j <= height; j++){ arrMapShow[i][j] = -5; } } } //获取是否交换棋手的标识符 public boolean getisOdd(){ return this.isOdd; } //设置交换棋手的标识符 public void setisOdd(boolean isodd){ if(isodd) this.isOdd=true; else this.isOdd=false; } //获取某棋盘方格是否有棋子的标识值 public boolean getisExist(){ return this.isExist; } //获取棋盘宽度 public int getWidth(){ return this.width; } //获取棋盘高度 public int getHeight(){ return this.height; } //获取棋盘模式 public int getModeChess(){ return this.modeChess; } //获取棋盘方格上棋子的信息 public int[][] getarrMapShow(){ return arrMapShow; } //判断下子的横向、纵向坐标是否越界 private boolean badxy(int x, int y){ if(x >= width+20 || x < 0) return true; return y >= height+20 || y < 0; } //计算棋盘上某一方格上八个方向棋子的最大值, //这八个方向分别是:左、右、上、下、左上、左下、右上、右下 public boolean chessExist(int i,int j){ if(this.arrMapShow[i][j]==1 || this.arrMapShow[i][j]==2) return true; return false; } //判断该坐标位置是否可下棋子 public void readyplay(int x,int y){ if(badxy(x,y)) return; if (chessExist(x,y)) return; this.arrMapShow[x][y]=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.arrMapShow[x][y]=1; }else{ setisOdd(true); this.arrMapShow[x][y]=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 <= height; j++){ if(!chessExist(i,j)){//算法判断是否下子 max_white=checkMax(i,j,2);//判断白子的最大值 max_black=checkMax(i,j,1);//判断黑子的最大值 max_temp=Math.max(max_white,max_black); if(max_temp>max){ max=max_temp; this.x=i; this.y=j; } } } } setX(this.x); setY(this.y); this.arrMapShow[this.x][this.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; //judge right for(int i=1;i<5;i++){ x_temp1+=1; if(x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; } //judge left x_temp1=x_temp; for(int i=1;i<5;i++){ x_temp1-=1; if(x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; } if(num<5) max_temp=num; //judge up x_temp1=x_temp; y_temp1=y_temp; num=0; for(int i=1;i<5;i++){ y_temp1-=1; if(y_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; } //judge down y_temp1=y_temp; for(int i=1;i<5;i++){ y_temp1+=1; if(y_temp1>this.height) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; } if(num>max_temp&&num<5) max_temp=num; //judge left_up x_temp1=x_temp; y_temp1=y_temp; num=0; for(int i=1;i<5;i++){ x_temp1-=1; y_temp1-=1; if(y_temp1<0 || x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; } //judge right_down x_temp1=x_temp; y_temp1=y_temp; for(int i=1;i<5;i++){ x_temp1+=1; y_temp1+=1; if(y_temp1>this.height || x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; } if(num>max_temp&&num<5) max_temp=num; //judge right_up x_temp1=x_temp; y_temp1=y_temp; num=0; for(int i=1;i<5;i++){ x_temp1+=1; y_temp1-=1; if(y_temp1<0 || x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; } //judge left_down x_temp1=x_temp; y_temp1=y_temp; for(int i=1;i<5;i++){ x_temp1-=1; y_temp1+=1; if(y_temp1>this.height || x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; } if(num>max_temp&&num<5) max_temp=num; max_num=max_temp; return max_num; } //判断胜负 public boolean judgeSuccess(int x,int y,boolean isodd){ int num=1; int arrvalue; int x_temp=x,y_temp=y; if(isodd) arrvalue=2; else arrvalue=1; int x_temp1=x_temp,y_temp1=y_temp; //判断右边 for(int i=1;i<6;i++){ x_temp1+=1; if(x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } //判断左边 x_temp1=x_temp; for(int i=1;i<6;i++){ x_temp1-=1; if(x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } if(num==5) return true; //判断上方 x_temp1=x_temp; y_temp1=y_temp; num=1; for(int i=1;i<6;i++){ y_temp1-=1; if(y_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } //判断下方 y_temp1=y_temp; for(int i=1;i<6;i++){ y_temp1+=1; if(y_temp1>this.height) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } if(num==5) return true; //判断左上 x_temp1=x_temp; y_temp1=y_temp; num=1; for(int i=1;i<6;i++){ x_temp1-=1; y_temp1-=1; if(y_temp1<0 || x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } //判断右下 x_temp1=x_temp; y_temp1=y_temp; for(int i=1;i<6;i++){ x_temp1+=1; y_temp1+=1; if(y_temp1>this.height || x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } if(num==5) return true; //判断右上 x_temp1=x_temp; y_temp1=y_temp; num=1; for(int i=1;i<6;i++){ x_temp1+=1; y_temp1-=1; if(y_temp1<0 || x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } //判断左下 x_temp1=x_temp; y_temp1=y_temp; for(int i=1;i<6;i++){ x_temp1-=1; y_temp1+=1; if(y_temp1>this.height || x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp展开阅读全文
咨信网温馨提示:1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。




Java编程:五子棋游戏源代码.doc



实名认证













自信AI助手
















微信客服
客服QQ
发送邮件
意见反馈



链接地址:https://www.zixin.com.cn/doc/2152047.html