算法是穷举递归法,只不过用java重新写了一遍 import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButtonMenuItem; import javax.swing.SwingUtilities; import javax.swing.UIManager; /* *main方法创建了ChessFrame类的一个实例对象(cf), *并启动屏幕显示显示该实例对象。 * **/ public class wuziqi { public static void main(String args[]) { ChessFrame cf = new ChessFrame(); cf.setVisible(true); } }; /* *类ChessFrame主要功能是创建五子棋游戏主窗体和菜单 **/ class ChessFrame extends JFrame implements ActionListener { private static final long serialVersionUID = 2183726320279905885L; private String[] strmode={"人机对弈","人人对弈"}; private String[] strorder={"玩家先手","计算机先手"}; public static boolean iscomputer=true; public static boolean checkcomputer=true; public static boolean isorder=true; private int width=15,height=15; private ChessModel cm; private MainPanel mp; public static int qipan[][]=new int[15][15]; //构造五子棋游戏的主窗体 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(14,14); setLocation(400,100); 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 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 if(items[i]=="先手顺序") { JMenu jm = new JMenu("先手顺序"); ButtonGroup group=new ButtonGroup(); JRadioButtonMenuItem rmenu; for(int k=0;k<strorder.length;k++) { rmenu=makeRadioButtonMenuItem(strorder[k],target); if(k==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 * 30+67 , h * 30+110); if(!ChessFrame.checkcomputer) { ChessFrame.iscomputer=false; } else { ChessFrame.iscomputer=true; } mp.setModel(cm); mp.repaint(); } public boolean getiscomputer(){ return ChessFrame.iscomputer; } public void restart(){ int modeChess = cm.getModeChess(); if(modeChess <= 3 && modeChess >= 1){ cm = new ChessModel(modeChess); MapSize(cm.getWidth(),cm.getHeight()); 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("人机对弈")){ ChessFrame.checkcomputer=true; ChessFrame.iscomputer=true; cm=new ChessModel(cm.getModeChess()); MapSize(cm.getWidth(),cm.getHeight()); SwingUtilities.updateComponentTreeUI(this); } if(arg.equals("人人对弈")){ ChessFrame.checkcomputer=false; ChessFrame.iscomputer=false; cm=new ChessModel(cm.getModeChess()); MapSize(cm.getWidth(),cm.getHeight()); SwingUtilities.updateComponentTreeUI(this); } if(arg.equals("计算机先手")) { ChessFrame.isorder=false; cm=new ChessModel(cm.getModeChess()); MapSize(cm.getWidth(),cm.getHeight()); SwingUtilities.updateComponentTreeUI(this); } if(arg.equals("玩家先手")) { ChessFrame.isorder=true; cm=new ChessModel(cm.getModeChess()); MapSize(cm.getWidth(),cm.getHeight()); SwingUtilities.updateComponentTreeUI(this); } if(arg.equals("开局")){ restart(); if(ChessFrame.isorder==false) cm.first(); } if(arg.equals("悔棋")){ int modeChess = cm.getModeChess(); if(modeChess <= 3 && modeChess >= 1){ cm = new ChessModel(modeChess); for(int i=0;i<this.width;i++) { for(int j=0;j<this.height;j++) { cm.arrMapShow[i][j] =qipan[i][j]; } } MapSize(cm.getWidth(),cm.getHeight()); } } if(arg.equals("关于")) JOptionPane.showMessageDialog(this, "五子棋游戏测试版本——By 江西财经大学08计一张帅", "关于", 0); if(arg.equals("退出")) System.exit(0); } } /* *类ChessModel实现了整个五子棋程序算法的核心 */ class ChessModel { //棋盘的宽度、高度、棋盘的模式(如20×15) private int width=15; private int modeChess; //棋盘方格的横向、纵向坐标 private int x=0,y=0; private int temp1; private int temp2; private int temp3; private int temp4; int[][] arrMapShow; //交换棋手的标识,棋盘方格上是否有棋子的标识符,先手顺序的标识 private boolean isOdd,isExist; private ChessFrame cf; //1表示黑棋 ,2表示白棋,-1表示空白可下处 public ChessModel() {} //该构造方法根据不同的棋盘模式(modeChess)来构建对应大小的棋盘 public ChessModel(int modeChess){ this.isOdd=true; if(modeChess == 1){ PanelInit(14, 14, modeChess); } } //按照棋盘模式构建棋盘大小 private void PanelInit(int width, int height, int modeChess){ this.width = width; 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] = -1; } } } //获取是否交换棋手的标识符 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.width; } //获取棋盘模式 public int getModeChess(){ return this.modeChess; } //获取棋盘方格上棋子的信息 public int[][] getarrMapShow(){ return arrMapShow; } //判断下子的横向、纵向坐标是否越界 private boolean badxy(int x, int y){ if(x >= width+30 || x < 0) return true; return y >= width+30 || 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; } } //计算机走棋 /* *说明:用穷举法判断每一个坐标点的四个方向的的最大棋子数, *最后得出棋子数最大值的坐标,下子 **/ void computerDo(int flag) { int max_black,max_white,count=0,max=0,max_temp,level,x[] ={-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2},y[]={-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2},i,j; setisOdd(true); for(i = 0; i < width; i++) { for(j = 0; j < width; j++) { if(this.arrMapShow[i][j]==-1) {//算法判断是否下子 if(lian_si(i,j,2)||duan_lian_si(i,j,2)) { level=43; x[level]=i; y[level]=j; } else { if(lian_si(i,j,1)||duan_lian_si(i,j,1)) { level=42; x[level]=i; y[level]=j; } else { if(si_si(i,j,2)&&zhouwei3(i,j,2)||si_san(i,j,2)&&zhouwei3(i,j,2)) { level=41; x[level]=i; y[level]=j; } else { if(si_si(i,j,2)&&zhouwei2(i,j,2)||si_san(i,j,2)&&zhouwei2(i,j,2)) { level=40; x[level]=i; y[level]=j; } else { if(si_si(i,j,2)&&zhouwei1(i,j,2)||si_san(i,j,2)&&zhouwei1(i,j,2)) { level=39; x[level]=i; y[level]=j; } else { if(si_si(i,j,2)||si_san(i,j,2)) { level=38; x[level]=i; y[level]=j; } else { if(huo_lian_san(i,j,1)&&huo_lian_san(i,j,2)) { level=37; x[level]=i; y[level]=j; } else { if(huo_lian_san(i,j,2)&&huo_duan_er(i,j,2)||huo_lian_san(i,j,2)&&huo_er(i,j,2)) { level=36; x[level]=i; y[level]=j; } else { if(huo_lian_san(i,j,2)&&zhouwei3(i,j,2)||duan_lian_san(i,j,2)&&zhouwei3(i,j,2)) { level=35; x[level]=i; y[level]=j; } else { if(huo_lian_san(i,j,2)&&zhouwei2(i,j,2)||duan_lian_san(i,j,2)&&zhouwei2(i,j,2)) { level=34; x[level]=i; y[level]=j; } else { if(huo_lian_san(i,j,2)&&zhouwei1(i,j,2)||duan_lian_san(i,j,2)&&zhouwei1(i,j,2)) { level=33; x[level]=i; y[level]=j; } else { if(huo_lian_san(i,j,2)||duan_lian_san(i,j,2)) { level=32; x[level]=i; y[level]=j; } else { if(si_si(i,j,1)&&zhouwei3(i,j,1)||si_san(i,j,1)&&zhouwei3(i,j,1)) { level=31; x[level]=i; y[level]=j; } else { if(si_si(i,j,1)&&zhouwei2(i,j,1)||si_san(i,j,1)&&zhouwei2(i,j,1)) { level=30; x[level]=i; y[level]=j; } else { if(si_si(i,j,1)&&zhouwei1(i,j,1)||si_san(i,j,1)&&zhouwei1(i,j,1)) { level=29; x[level]=i; y[level]=j; } else { if(si_si(i,j,1)||si_san(i,j,1)) { level=28; x[level]=i; y[level]=j; } else { if(huo_lian_san(i,j,1)&&huo_er(i,j,2)||huo_lian_san(i,j,1)&&huo_duan_er(i,j,2)) { level=27; x[level]=i; y[level]=j; } else { if(huo_lian_san(i,j,1)&&zhouwei3(i,j,1)||duan_lian_san(i,j,1)&&zhouwei3(i,j,1)) { level=26; x[level]=i; y[level]=j; } else { if(huo_lian_san(i,j,1)&&zhouwei2(i,j,1)||duan_lian_san(i,j,1)&&zhouwei2(i,j,1)) { level=25; x[level]=i; y[level]=j; } else { if(huo_lian_san(i,j,1)&&zhouwei1(i,j,1)||duan_lian_san(i,j,1)&&zhouwei1(i,j,1)) { level=24; x[level]=i; y[level]=j; } else { if(huo_lian_san(i,j,1)||duan_lian_san(i,j,1)) { level=23; x[level]=i; y[level]=j; } else { if(lian_duan_si(i,j,2)&&huo_er(i,j,2)||lian_duan_si(i,j,2)&&huo_lian_san(i,j,2)||lian_duan_si(i,j,2)&&huo_duan_er(i,j,2)||si_lian_san(i,j,2)&&huo_er(i,j,2)||si_lian_san(i,j,2)&&huo_lian_san(i,j,2)||si_lian_san(i,j,2)&&huo_duan_er(i,j,2)) { level=22; x[level]=i; y[level]=j; } else { if(san_san(i,j,2)) { level=21; x[level]=i; y[level]=j; } else { if((huo_er(i,j,2)&&san_san(i,j,1))||(huo_duan_er(i,j,2)&&san_san(i,j,1))) { level=20; x[level]=i; y[level]=j; } else { if(san_san(i,j,1)) { level=19; x[level]=i; y[level]=j; } else { if(lian_duan_si(i,j,1)&&huo_er(i,j,1)||lian_duan_si(i,j,1)&&huo_lian_san(i,j,1)||lian_duan_si(i,j,1)&&huo_duan_er(i,j,1)||si_lian_san(i,j,1)&&huo_er(i,j,1)||si_lian_san(i,j,1)&&huo_lian_san(i,j,1)||si_lian_san(i,j,1)&&huo_duan_er(i,j,1)) { level=18; x[level]=i; y[level]=j; } else { if(si_lian_san(i,j,2)||lian_duan_si(i,j,2)) { level=17; x[level]=i; y[level]=j; } else { if(huo_er(i,j,2)&&shi_zi_er(i,j,2)) { level=16; x[level]=i; y[level]=j; } else { if((huo_er(i,j,2)&&huo_er(i,j,1))||(huo_duan_er(i,j,2)&&huo_er(i,j,1))||(huo_duan_er(i,j,2)&&huo_duan_er(i,j,1))||(huo_er(i,j,2)&&huo_duan_er(i,j,1))) { level=15; x[level]=i; y[level]=j; } else { if((huo_er(i,j,2)&&si_er(i,j,2))||(huo_er(i,j,2)&&si_er(i,j,1))) { level=14; x[level]=i; y[level]=j; } else { if(huo_er(i,j,2)||huo_duan_er(i,j,2)) { level=13; x[level]=i; y[level]=j; } else { if(huo_er(i,j,2)&&zhouwei3(i,j,2)||huo_duan_er(i,j,2)&&zhouwei3(i,j,2)) { level=12; x[level]=i; y[level]=j; } else { if(huo_er(i,j,2)&&zhouwei2(i,j,2)||huo_duan_er(i,j,2)&&zhouwei2(i,j,2)) { level=11; x[level]=i; y[level]=j; } else { if(huo_er(i,j,2)&&zhouwei1(i,j,2)||huo_duan_er(i,j,2)&&zhouwei1(i,j,2)) { level=10; x[level]=i; y[level]=j; } else { if(huo_er(i,j,1)&&shi_zi_er(i,j,1)) { level=9; x[level]=i; y[level]=j; } else { if(huo_er(i,j,1)&&zhouwei3(i,j,1)||huo_duan_er(i,j,1)&&zhouwei3(i,j,1)) { level=8; x[level]=i; y[level]=j; } else { if(huo_er(i,j,1)&&zhouwei2(i,j,1)||huo_duan_er(i,j,1)&&zhouwei2(i,j,1)) { level=7; x[level]=i; y[level]=j; } else { if(huo_er(i,j,1)&&zhouwei1(i,j,1)||huo_duan_er(i,j,1)&&zhouwei1(i,j,1)) { level=6; x[level]=i; y[level]=j; } else { if(huo_er(i,j,1)||huo_duan_er(i,j,1)) { level=5; x[level]=i; y[level]=j; } else { if(xie_zi_er(i,j,2)||shi_zi_er(i,j,2)) { level=4; x[level]=i; y[level]=j; } else { if(xie_zi_er(i,j,1)||shi_zi_er(i,j,1)) { level=3; x[level]=i; y[level]=j; } else { if(si_er(i,j,2)) { level=2; x[level]=i; y[level]=j; } else { max_white=checkMax(i,j,2);//判断白子的最大值 max_black=checkMax(i,j,1);//判断黑子的最大值 if(max_white>max_black) max_temp=max_white; else max_temp=max_black; if(max_temp>max&&count==0) { max=max_temp; count++; level=1; x[level]=i; y[level]=j; } else if(count==1) { for(int m=0;m<width-1;m++) for(int n=0;n<width-1;n++) if(arrMapShow[m][n]==2) { level=1; if(m<this.width-1&&m>=1&&n>=1&&n<this.width-1) { if(arrMapShow[m+1][n-1]==-1) { x[level]=m+1; y[level]=n-1; } else if(arrMapShow[m-1][n+1]==-1) { x[level]=m-1; y[level]=n+1; } } } count=0; } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } } if(flag==4) { for(i=43;i>=1;i--) { if(x[i]!=-2) { temp4=i; break; } } } if(flag==3) { for(i=43;i>=1;i--) { if(x[i]!=-2) { this.arrMapShow[x[i]][y[i]]=1; computerDo(flag+1); this.arrMapShow[x[i]][y[i]]=-1; temp3=i; break; } } } if(flag==2) { for(i=43;i>=1;i--) { if(x[i]!=-2) { this.arrMapShow[x[i]][y[i]]=2; computerDo(flag+1); this.arrMapShow[x[i]][y[i]]=-1; temp2=i; break; } } } else if(flag==1) { for(i=43;i>=1;i--) { if(x[i]!=-2) { this.arrMapShow[x[i]][y[i]]=1; computerDo(flag+1); this.arrMapShow[x[i]][y[i]]=-1; temp1=i; break; } } } else if(flag==0) { for(i=43;i>=1;i--) { if(x[i]!=-2) { this.arrMapShow[x[i]][y[i]]=2; computerDo(flag+1); this.arrMapShow[x[i]][y[i]]=-1; if(i>=19&&(temp4!=19&&temp4!=20&&temp4!=31&&temp4!=23&&temp4!=24&&temp4!=25&&temp4!=26&&temp4!=27&&temp4!=28&&temp4!=29&&temp4!=30&&temp4!=42)||temp4>=19) { this.x=x[i]; this.y=y[i]; this.arrMapShow[this.x][this.y]=2; setX(this.x); setY(this.y); if(judgeSuccess(this.x,this.y,true)==true) { JPanel jp=new JPanel(); showDefeat(jp); cf.restart(); } return ; } if(i>=19&&(temp3!=20&&temp3!=19&&temp3!=31&&temp3!=23&&temp3!=24&&temp3!=25&&temp3!=26&&temp3!=27&&temp3!=28&&temp3!=29&&temp3!=30&&temp3!=37&&temp3!=42)||(temp3>=19&&(temp3!=20&&temp3!=19&&temp3!=31&&temp3!=23&&temp3!=24&&temp3!=25&&temp3!=26&&temp3!=27&&temp3!=28&&temp3!=29&&temp3!=30&&temp3!=37&&temp3!=42))) { this.x=x[i]; this.y=y[i]; this.arrMapShow[this.x][this.y]=2; setX(this.x); setY(this.y); if(judgeSuccess(this.x,this.y,true)==true) { JPanel jp=new JPanel(); showDefeat(jp); cf.restart(); } return ; } if(i>=19&&(temp2!=20&&temp2!=19&&temp2!=31&&temp2!=23&&temp2!=24&&temp2!=25&&temp2!=26&&temp2!=27&&temp2!=28&&temp2!=29&&temp2!=30&&temp2!=42)||temp2>=19) { this.x=x[i]; this.y=y[i]; this.arrMapShow[this.x][this.y]=2; setX(this.x); setY(this.y); if(judgeSuccess(this.x,this.y,true)==true) { JPanel jp=new JPanel(); showDefeat(