java五子棋程序

上传人:xzh****18 文档编号:42089201 上传时间:2018-06-01 格式:DOC 页数:18 大小:98KB
返回 下载 相关 举报
java五子棋程序_第1页
第1页 / 共18页
java五子棋程序_第2页
第2页 / 共18页
java五子棋程序_第3页
第3页 / 共18页
java五子棋程序_第4页
第4页 / 共18页
java五子棋程序_第5页
第5页 / 共18页
点击查看更多>>
资源描述

《java五子棋程序》由会员分享,可在线阅读,更多相关《java五子棋程序(18页珍藏版)》请在金锄头文库上搜索。

1、/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 v

2、oid 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=

3、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(

4、);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

5、 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);elsereturn null;for(int i = 0; i =

6、1)cm = new ChessModel(modeChess);MapSize(cm.getWidth(),cm.getHeight();elseSystem.out.println(“u81EAu5B9Au4E49“);public void actionPerformed(ActionEvent e)String arg=e.getActionCommand();tryif (arg.equals(“Windows“)UIManager.setLookAndFeel(“com.sun.java.swing.plaf.windows.WindowsLookAndFeel“);else if

7、(arg.equals(“Motif“)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

8、);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);MapS

9、ize(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;t

10、his.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 实现了整个

11、五子棋程序算法的核心 */ class ChessModel /棋盘的宽度、高度、棋盘的模式(如 2015) private int width,height,modeChess; /棋盘方格的横向、纵向坐标 private int x=0,y=0; /棋盘方格的横向、纵向坐标所对应的棋子颜色, /数组 arrMapShow 只有 3 个值:1,2,3,-5,/其中 1 代表该棋盘方格上下的棋子为黑子, /2 代表该棋盘方格上下的棋子为白子, /3 代表为该棋盘方格上没有棋子, /-5 代表该棋盘方格不能够下棋子 private int arrMapShow; /交换棋手的标识,棋盘方格上是否

12、有棋子的标识符 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); /按照棋盘模式构建棋盘大小 priv

13、ate 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+20 | x = height+20 | y max)max=max_temp;this.x=i;this.y=j;setX(this.x);setY(this.y);this.arrMapShowthis.xthis.y=2; /

14、记录电脑下子后的横向坐标 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,ma

15、x_num,max_temp=0;int x_temp=x,y_temp=y;int x_temp1=x_temp,y_temp1=y_temp;/judge rightfor(int i=1;ithis.width)break;if(this.arrMapShowx_temp1y_temp1=black_or_white)num+;elsebreak;/judge leftx_temp1=x_temp;for(int i=1;ithis.height)break;if(this.arrMapShowx_temp1y_temp1=black_or_white)num+;elsebreak;if

16、(nummax_tempif(this.arrMapShowx_temp1y_temp1=black_or_white)num+;elsebreak;if(nummax_tempif(this.arrMapShowx_temp1y_temp1=black_or_white)num+;elsebreak;/judge left_downx_temp1=x_temp;y_temp1=y_temp;for(int i=1;ithis.height | x_temp1max_tempif(this.arrMapShowx_temp1y_temp1=arrvalue)num+;elsebreak;/判断左边x_tem

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号