




免费预览已结束,剩余10页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
用java编写一个多种界面的五子棋游戏(可实现人人对弈和人机对弈的双重功能) package wuziqi;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 FiveChessAppletDemopublic static void main(String args)ChessFrame cf=new ChessFrame();cf.show();/界面布局class ChessFrame extends JFrame implements ActionListenerprivate String 窗口大小=20x15,30x20,60x35;/窗口大小选择private String 对弈对象=人人对弈,人机对弈;/对弈对象的选择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视图1,视图2,视图3,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;iitems.length;i+) if(items=null) m.addSeparator(); else if(items=棋盘) JMenu jm=new JMenu(棋盘); ButtonGroup group=new ButtonGroup(); JRadioButtonMenuItem rmenu; for(int j=0;j窗口大小.length;j+) rmenu=makeRadioButtonMenuItem(窗口大小j,target); if(j=0) rmenu.setSelected(true); jm.add(rmenu); group.add(rmenu); m.add(jm); else if(items=模式) JMenu jm=new JMenu(模式); ButtonGroup group=new ButtonGroup(); JRadioButtonMenuItem rmenu; for(int h=0;h对弈对象.length;h+) rmenu=makeRadioButtonMenuItem(对弈对象h,target); if(h=0) rmenu.setSelected(true); jm.add(rmenu); group.add(rmenu); m.add(jm); else m.add(makeMenuItem(items,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=1) cm=new ChessModel (modeChess); MapSize(cm.getWidth(),cm.getHeight(); else System.out.println(u81EAu5B9Au4E49); /各种事件的监听实现public void actionPerformed(ActionEvent e)String arg=e.getActionCommand();/三种视图界面的选择的监听实现tryif(arg.equals(视图3)UIManager.setLookAndFeel(com.sun.java.swing.plaf.windows.WindowsLookAndFeel);else if(arg.equals(视图2)UIManager.setLookAndFeel(com.sun.java.swing.plaf.motif.MotifLookAndFeel);elseUIManager.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(60x35) this.width=60; this.height=35; 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);/第一种界面class ChessModel private int width,height,modeChess; private int x=0,y=0; private int arrMapShow; private boolean isOdd,isExist; public ChessModel() 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(60,35,modeChess); private void PanelInit(int width,int height,int modeChess) this.width=width; this.height=height; this.modeChess=modeChess; arrMapShow=new intwidth+1height+1; for(int i=0;i=width;i+) for(int j=0;j=width+20|x=height+20|y0;public boolean chessExist(int i,int j)if(this.arrMapShowj=1|this.arrMapShowj=2)return true;return false;public void readyplay(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;elsethis.isExist=false;if(getisOdd()setisOdd(false);this.arrMapShowxy=1;elsesetisOdd(true);this.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;jmax)max=max_temp;this.x=i;this.y=j;setX(this.x);setY(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+;elsebreak;x_temp1=x_temp;for(int i=1;i5;i+)x_temp1-=1;if(x_temp10)break;if(this.arrMapShowx_temp1y_temp1=black_or_white)num+;elsebreak;if(num5)max_temp=num;x_temp1=x_temp;y_temp1=y_temp;num=0;for(int i=1;i5;i+)y_temp1-=1;if(y_temp10)break;if(this.arrMapShowx_temp1y_temp1=black_or_white)num+;elsebreak;y_temp1=y_temp;for(int i=1;ithis.height)break;if(this.arrMapShowx_temp1y_temp1=black_or_white)num+;elsebreak;if(nummax_temp&num5)max_temp=num;x_temp1=x_temp;y_temp1=y_temp;num=0;for(int i=1;i5;i+)x_temp1-=1;y_temp1-=1;if(y_temp10|x_temp10)break;if(this.arrMapShowx_temp1y_temp1=black_or_white)num+;elsebreak;x_temp1=x_temp;y_temp1=y_temp;for(int i=1;ithis.height|x_temp1this.width)break;if(this.arrMapShowx_temp1y_temp1=black_or_white)num+;elsebreak;if(nummax_temp&num5)max_temp=num;x_temp1=x_temp;y_temp1=y_temp;num=0;for(int i=1;i5;i+)x_temp1+=1;y_temp1-=1;if(y_temp1this.width)break;if(this.arrMapShowx_temp1y_temp1=black_or_white)num+;elsebreak;x_temp1=x_temp;y_temp1=y_temp;for(int i=1;ithis.height|x_temp1max_temp&num5) 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;elsearrvalue = 1;int x_temp1 = x_temp, y_temp1 = y_temp;for (int i = 1; i this.width)break;if (this.arrMapShowx_temp1y_temp1 = arrvalue)num+;elsebreak;x_temp1 = x_temp;for (int i = 1; i 6; i+) x_temp1 -= 1;if (x_temp1 0)break;if (this.arrMapShowx_temp1y_temp1 = arrvalue)num+;elsebreak;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.arrMapShowx_temp1y_temp1 = arrvalue)num+;elsebreak;y_temp1 = y_temp;for (int i = 1; i this.height)break;if (this.arrMapShowx_temp1y_temp1 = arrvalue)num+;elsebreak;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_temp10)break;if (this.arrMapShowx_temp1y_temp1 = arrvalue)num+;elsebreak;x_temp1=x_temp;y_temp1 = y_temp;num=1;for (int i = 1; i this.height|x_temp1this.width)break;if (this.arrMapShowx_temp1y_temp1 = arrvalue)num+;elsebreak;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_temp1this.width)break;if (this.arrMapShowx_temp1y_temp1 = arrvalue)num+;elsebreak;x_temp1=x_temp;y_temp1 = y_temp;for (int i = 1; i this.height|x_temp10)break;if (this.arrMapShowx_temp1y_temp1 = arrvalue)num+;elsebreak;if(num=5)return true;return false;public void showSuccess(JPanel jp)JOptionPane.showMessageDialog(jp,你赢了,好厉害!,win,JOptionPane.INFORMATION_MESSAGE);public void showDefeat(JPanel jp) JOptionPane.showMessageDialog(jp,你输了,请重新开始!,lost,JOptionPane.INFORMATION_MESSAGE);class MainPanel extends JPanel implements MouseListener,MouseMotionListenerprivate int width,height;private ChessModel cm;MainPanel(ChessModel mm) cm=mm; width=cm.getWidth(); height=cm.getHeight(); addMouseListener(this);public void setModel(ChessModel mm) cm=mm;width=cm.getWidth();height=cm.getHeight();public void paintComponent(Graphics g) super.paintComponent(g); for(int j=0;j=height;j+) for(int i=0;i=width;i+) int v=cm.getarrMapShow()j; draw(g,i,j,v); public void draw(Graphics g,int i,int j,int v) int x=20*i+20; int y=20*j+20; if(i!=width&j!=height) g.setColor(Color.white); g.drawRect(x,y,20,20); if(v=1) g.setColor(Color.gray); g.drawOval(x-8,y-8,16,16); g.setColor(Color.black); g.fillOval(x-8,y-8,16,16); if(v=2) g.setColor(Color.gray); g.drawOval(x-8,y-8,16,16); g.setColor(Color.white); g.fillOval(x-
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年智能穿戴行业创新技术应用前景研究报告
- 巴彦淖尔市2025内蒙古巴彦淖尔市直属乌兰牧骑(市歌舞剧院)招聘演职人员10人笔试历年参考题库附带答案详解
- 软件买卖合同书范本6篇
- 商场三包培训课件
- 商品车日常安全培训课件
- 国家事业单位招聘2025中国农科院质标所招聘笔试笔试历年参考题库附带答案详解
- 引水管道项目技术协议书8篇
- 北京市2025北京市体育设施管理中心应届毕业生招聘2人笔试历年参考题库附带答案详解
- 2025陕西秦巴碧水环境检测有限公司招聘(10人)笔试参考题库附带答案详解
- 2025辽宁沈阳盛京资产管理集团有限公司所属子公司沈阳国际陆港集团有限责任公司招聘14人笔试参考题库附带答案详解
- 2025年度反洗钱阶段考试培训试考试题库(含答案)
- 收割芦苇施工方案
- 普通黄金现货购买合同8篇
- 三力测试考试题库及答案视频讲解
- 2025年河南省人民法院聘用书记员考试试题及答案
- 2025年中学教师资格考试《综合素质》核心考点与解析
- 口腔冠延长术
- 部编版七年级语文上册《闻王昌龄左迁龙标遥有此寄》课件
- 诊所经营管理课件
- 江苏亿洲再生资源科技有限公司资源综合利用技改提升项目 环评报告书
- 质量改进培训-课件
评论
0/150
提交评论