1、五子棋package game;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 a
2、rgs) 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;priv
3、ate 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 = n
4、ew 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
5、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(itemsi = null) m.addS
6、eparator(); else if(itemsi = 棋盘) JMenu 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 =
7、模式) JMenu jm = new JMenu(模式); ButtonGroup 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
8、 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)ta
9、rget); 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 re
10、turn 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 getiscompute
11、r() 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(); try if (a
12、rg.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.updateCompon
13、entTreeUI(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); Swing
14、Utilities.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(); MapSiz
15、e(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.
16、equals(关于) JOptionPane.showMessageDialog(this, 五子棋游戏测试版本, 关于, 0); if(arg.equals(退出) System.exit(0);/*类ChessModel实现了整个五子棋程序算法的核心*/class ChessModel /棋盘的宽度、高度、棋盘的模式(如2015)private int width,height,modeChess;/棋盘方格的横向、纵向坐标private int x=0,y=0;/棋盘方格的横向、纵向坐标所对应的棋子颜色,/数组arrMapShow只有3个值:1,2,3,-5,/其中1代表该棋盘方格上下的
17、棋子为黑子,/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 =
18、 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 intwidth+1height+1; for(int i = 0; i = width; i+) for(int
19、 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 readyplay(int x,int y) if(badxy(x,y) return; if (chessExist(x,y) retu
20、rn; 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.arrMapShowxy=2; /计算机走棋/*说明:用穷举法判断每一个坐标点的四个方向的的最大棋子数,*最后得出棋子数最大值的坐
21、标,下子*/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(this.y); this.arrMapShowthis.xthis.y=2;/记录电脑下子后的横向坐标public
22、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_
23、temp=x,y_temp=y; int x_temp1=x_temp,y_temp1=y_temp; /judge right for(int i=1;ithis.width) break; if(this.arrMapShowx_temp1y_temp1=black_or_white) num+; else break; /judge left 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+; el
24、se break; if(num5) max_temp=num; /judge up 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+; else break; /judge down y_temp1=y_temp; for(int i=1;ithis.height) break; if(this.arrMapShowx_temp1y_temp1=black_
25、or_white) num+; else break; if(nummax_temp&num5) max_temp=num; /judge left_up 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+; else break; /judge right_down 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+; else break; if(nummax_temp&num5) max_temp=num; /judge right_up x_temp1=x_temp; y_temp1=y_temp; num=0; for(int i=1;i5;i+) x_temp1+=1; y_temp1-=1; if(y_temp1
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1