JAVA专业课程设计五子棋内附完整代码_第1页
JAVA专业课程设计五子棋内附完整代码_第2页
JAVA专业课程设计五子棋内附完整代码_第3页
JAVA专业课程设计五子棋内附完整代码_第4页
JAVA专业课程设计五子棋内附完整代码_第5页
已阅读5页,还剩57页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

JAVA课程设计设计题目:五子棋游戏简明介绍五子棋五子棋起源五子棋,又被称为“连五子、五子连、串珠、五目、五目碰、五格、五石、五法、五联、京棋”。五子棋相传起源于四千多年前尧帝时期,比围棋历史还要悠久,可能早在“尧造围棋”之前,民间就已经有五子棋游戏。相关早期五子棋文史资料和围棋有相同之处,因为古代五子棋棋具和围棋是完全相同。 2.现在五子棋标准棋盘(图所表示)3.五子棋棋子五子棋采取两种颜色棋子,黑色棋子和白色棋子,和围棋相同,4.五子棋规则五子棋就是五个棋子连在一起就算赢,黑棋先行,下棋下在棋盘交叉线上,因为黑棋先行,优势太大,所以对黑棋设了禁手,又要求了“三手交换”,就是黑棋下第2手棋,盘面第3着棋以后,白方在应白2之前,如感觉黑方棋形不利于己方,可出交换,即执白棋一方变为执黑棋一方。和“五手两打法”,就是黑棋在下盘面上关键第5手时,必需下两步棋,让白方在这两步棋中任选一步,然后再续下。不过通常爱好者不需要遵照这么多规则。二.程序步骤三.代码设计和分析main方法创建了ChessFrame类一个实例对象(cf),并开启屏幕显示显示该实例对象。publicclassFiveChessAppletDemo{publicstaticvoidmain(Stringargs[]){ChessFramecf=newChessFrame();cf.show();}}用类ChessFrame创建五子棋游戏主窗体和菜单importjava.awt.*;importjava.awt.event.*;importjava.applet.*;importjavax.swing.*;importjava.io.PrintStream;importjavax.swing.JComponent;importjavax.swing.JPanel;classChessFrameextendsJFrameimplementsActionListener{privateString[]strsize={"标准棋盘","改善棋盘","扩大棋盘"};privateString[]strmode={"人机对战","人人对战"};publicstaticbooleaniscomputer=true,checkcomputer=true;privateintwidth,height;privateChessModelcm;privateMainPanelmp;结构五子棋游戏主窗体publicChessFrame(){this.setTitle("五子棋游戏");cm=newChessModel(1);mp=newMainPanel(cm);Containercon=this.getContentPane();con.add(mp,"Center");this.setResizable(false);this.addWindowListener(newChessWindowEvent());MapSize(14,14);JMenuBarmbar=newJMenuBar();this.setJMenuBar(mbar);JMenugameMenu=newJMenu("游戏");mbar.add(makeMenu(gameMenu,newObject[]{"开局",null,"棋盘",null,"模式",null,"退出"},this));JMenulookMenu=newJMenu("外观");mbar.add(makeMenu(lookMenu,newObject[]{"类型一","类型二","类型三"},this));JMenuhelpMenu=newJMenu("版本");mbar.add(makeMenu(helpMenu,newObject[]{"相关"},this));}结构五子棋游戏主菜单publicJMenumakeMenu(Objectparent,Objectitems[],Objecttarget){JMenum=null;if(parentinstanceofJMenu)m=(JMenu)parent;elseif(parentinstanceofString)m=newJMenu((String)parent);elsereturnnull;for(inti=0;i<items.length;i++)if(items[i]==null)m.addSeparator();elseif(items[i]=="棋盘"){JMenujm=newJMenu("棋盘");ButtonGroupgroup=newButtonGroup();JRadioButtonMenuItemrmenu;for(intj=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);}elseif(items[i]=="模式"){JMenujm=newJMenu("模式");ButtonGroupgroup=newButtonGroup();JRadioButtonMenuItemrmenu;for(inth=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);}elsem.add(makeMenuItem(items[i],target));returnm;}结构五子棋游戏菜单项publicJMenuItemmakeMenuItem(Objectitem,Objecttarget){JMenuItemr=null;if(iteminstanceofString)r=newJMenuItem((String)item);elseif(iteminstanceofJMenuItem)r=(JMenuItem)item;elsereturnnull;if(targetinstanceofActionListener)r.addActionListener((ActionListener)target);returnr;}结构五子棋游戏单选按钮式菜单项publicJRadioButtonMenuItemmakeRadioButtonMenuItem(Objectitem,Objecttarget){JRadioButtonMenuItemr=null;if(iteminstanceofString)r=newJRadioButtonMenuItem((String)item);elseif(iteminstanceofJRadioButtonMenuItem)r=(JRadioButtonMenuItem)item;elsereturnnull;if(targetinstanceofActionListener)r.addActionListener((ActionListener)target);returnr;}publicvoidMapSize(intw,inth){setSize(w*24,h*27);if(this.checkcomputer)this.iscomputer=true;elsethis.iscomputer=false;mp.setModel(cm);mp.repaint();}publicbooleangetiscomputer(){returnthis.iscomputer;}publicvoidrestart(){intmodeChess=cm.getModeChess();if(modeChess<=3&&modeChess>=0){cm=newChessModel(modeChess);MapSize(cm.getWidth(),cm.getHeight());}}publicvoidactionPerformed(ActionEvente){Stringarg=e.getActionCommand();try{if(arg.equals("类型三"))UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");elseif(arg.equals("类型二"))UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");elseUIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");SwingUtilities.updateComponentTreeUI(this);}catch(Exceptionee){}if(arg.equals("标准棋盘")){this.width=14;this.height=14;cm=newChessModel(1);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("改善棋盘")){this.width=18;this.height=18;cm=newChessModel(2);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("扩大棋盘")){this.width=22;this.height=22;cm=newChessModel(3);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("人机对战")){this.checkcomputer=true;this.iscomputer=true;cm=newChessModel(cm.getModeChess());MapSize(cm.getWidth(),cm.getHeight());SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("人人对战")){this.checkcomputer=false;this.iscomputer=false;cm=newChessModel(cm.getModeChess());MapSize(cm.getWidth(),cm.getHeight());SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("开局")){restart();}if(arg.equals("相关"))JOptionPane.showMessageDialog(null,"第一版","版本",JOptionPane.PLAIN_MESSAGE);if(arg.equals("退出"))System.exit(0);}}用类ChessModel实现了整个五子棋程序算法关键importjava.awt.*;importjava.awt.event.*;importjava.applet.*;importjavax.swing.*;importjava.io.PrintStream;importjavax.swing.JComponent;importjavax.swing.JPanel;classChessModel{要求棋盘宽度、高度、棋盘模式privateintwidth,height,modeChess;要求棋盘方格横向、纵向坐标privateintx=0,y=0;棋盘方格横向、纵向坐标所对应棋子颜色,数组arrMapShow只有3个值:1,2,3,-1,其中1代表该棋盘方格上下棋子为黑子,2代表该棋盘方格上下棋子为白子,3代表为该棋盘方格上没有棋子,-1代表该棋盘方格不能够下棋子privateint[][]arrMapShow;交换棋手标识,棋盘方格上是否有棋子标识符privatebooleanisOdd,isExist;publicChessModel(){}该结构方法依据不一样棋盘模式(modeChess)来构建对应大小棋盘publicChessModel(intmodeChess){this.isOdd=true;if(modeChess==1){PanelInit(14,14,modeChess);}if(modeChess==2){PanelInit(18,18,modeChess);}if(modeChess==3){PanelInit(22,22,modeChess);}}根据棋盘模式构建棋盘大小privatevoidPanelInit(intwidth,intheight,intmodeChess){this.width=width;this.height=height;this.modeChess=modeChess;arrMapShow=newint[width+1][height+1];for(inti=0;i<=width;i++){for(intj=0;j<=height;j++){arrMapShow[i][j]=-1;}}}获取是否交换棋手标识符publicbooleangetisOdd(){returnthis.isOdd;}设置交换棋手标识符publicvoidsetisOdd(booleanisodd){if(isodd)this.isOdd=true;elsethis.isOdd=false;}获取某棋盘方格是否有棋子标识值publicbooleangetisExist(){returnthis.isExist;}获取棋盘宽度publicintgetWidth(){returnthis.width;}获取棋盘高度publicintgetHeight(){returnthis.height;}获取棋盘模式publicintgetModeChess(){returnthis.modeChess;}获取棋盘方格上棋子信息publicint[][]getarrMapShow(){returnarrMapShow;}判定下子横向、纵向坐标是否越界privatebooleanbadxy(intx,inty){if(x>=width+20||x<0)returntrue;returny>=height+20||y<0;}计算棋盘上某一方格上八个方向棋子最大值,这八个方向分别是:左、右、上、下、左上、左下、右上、右下publicbooleanchessExist(inti,intj){if(this.arrMapShow[i][j]==1||this.arrMapShow[i][j]==2)returntrue;returnfalse;}判定该坐标位置是否可下棋子publicvoidreadyplay(intx,inty){if(badxy(x,y))return;if(chessExist(x,y))return;this.arrMapShow[x][y]=3;}在该坐标位置下棋子publicvoidplay(intx,inty){if(badxy(x,y))return;if(chessExist(x,y)){this.isExist=true;return;}elsethis.isExist=false;if(getisOdd()){setisOdd(false);this.arrMapShow[x][y]=1;}else{setisOdd(true);this.arrMapShow[x][y]=2;}}计算机走棋说明:用穷举法判定每一个坐标点四个方向最大棋子数,最终得出棋子数最大值坐标,下子publicvoidcomputerDo(intwidth,intheight){intmax_black,max_white,max_temp,max=0;setisOdd(true);System.out.println("计算机走棋...");for(inti=0;i<=width;i++){for(intj=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;}统计电脑下子后横向坐标publicvoidsetX(intx){this.x=x;}统计电脑下子后纵向坐标publicvoidsetY(inty){this.y=y;}获取电脑下子横向坐标publicintgetX(){returnthis.x;}获取电脑下子纵向坐标publicintgetY(){returnthis.y;}计算棋盘上某一方格上八个方向棋子最大值,这八个方向分别是:左、右、上、下、左上、左下、右上、右下publicintcheckMax(intx,inty,intblack_or_white){intnum=0,max_num,max_temp=0;intx_temp=x,y_temp=y;intx_temp1=x_temp,y_temp1=y_temp;判定右边for(inti=1;i<5;i++){x_temp1+=1;if(x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}判定左边x_temp1=x_temp;for(inti=1;i<5;i++){x_temp1-=1;if(x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}if(num<5)max_temp=num;判定上面x_temp1=x_temp;y_temp1=y_temp;num=0;for(inti=1;i<5;i++){y_temp1-=1;if(y_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}判定下面y_temp1=y_temp;for(inti=1;i<5;i++){y_temp1+=1;if(y_temp1>this.height)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}if(num>max_temp&&num<5)max_temp=num;判定左上方x_temp1=x_temp;y_temp1=y_temp;num=0;for(inti=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++;elsebreak;}判定右下方x_temp1=x_temp;y_temp1=y_temp;for(inti=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++;elsebreak;}if(num>max_temp&&num<5)max_temp=num;判定右上方x_temp1=x_temp;y_temp1=y_temp;num=0;for(inti=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++;elsebreak;}判定左下方x_temp1=x_temp;y_temp1=y_temp;for(inti=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++;elsebreak;}if(num>max_temp&&num<5)max_temp=num;max_num=max_temp;returnmax_num;}判定胜败publicbooleanjudgeSuccess(intx,inty,booleanisodd){intnum=1;intarrvalue;intx_temp=x,y_temp=y;if(isodd)arrvalue=2;elsearrvalue=1;intx_temp1=x_temp,y_temp1=y_temp;判定右边胜败for(inti=1;i<6;i++){x_temp1+=1;if(x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}判定左边胜败x_temp1=x_temp;for(inti=1;i<6;i++){x_temp1-=1;if(x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}if(num==5)returntrue;判定上方胜败x_temp1=x_temp;y_temp1=y_temp;num=1;for(inti=1;i<6;i++){y_temp1-=1;if(y_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}判定下方胜败y_temp1=y_temp;for(inti=1;i<6;i++){y_temp1+=1;if(y_temp1>this.height)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}if(num==5)returntrue;判定左上胜败x_temp1=x_temp;y_temp1=y_temp;num=1;for(inti=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++;elsebreak;}判定右下胜败x_temp1=x_temp;y_temp1=y_temp;for(inti=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++;elsebreak;}if(num==5)returntrue;判定右上胜败x_temp1=x_temp;y_temp1=y_temp;num=1;for(inti=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++;elsebreak;}判定左下胜败x_temp1=x_temp;y_temp1=y_temp;for(inti=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_temp1]==arrvalue)num++;elsebreak;}if(num==5)returntrue;returnfalse;}赢棋后提醒publicvoidshowSuccess(JPaneljp){JOptionPane.showMessageDialog(jp,"你赢了","结果",JOptionPane.INFORMATION_MESSAGE);}输棋后提醒publicvoidshowDefeat(JPaneljp){JOptionPane.showMessageDialog(jp,"你输了","结果",JOptionPane.INFORMATION_MESSAGE);}}用类MainPanel关键完成以下功效:1、构建一个面板,在该面板上画上棋盘;2、处理在该棋盘上鼠标事件(如鼠标左键点击、鼠标右键点击、鼠标拖动等)importjava.awt.*;importjava.awt.event.*;importjava.applet.*;importjavax.swing.*;importjava.io.PrintStream;importjavax.swing.JComponent;importjavax.swing.JPanel;classMainPanelextendsJPanelimplementsMouseListener,MouseMotionListener{设定棋盘宽度和高度privateintwidth,height;privateChessModelcm;依据棋盘模式设定面板大小MainPanel(ChessModelmm){cm=mm;width=cm.getWidth();height=cm.getHeight();addMouseListener(this);}依据棋盘模式设定棋盘宽度和高度publicvoidsetModel(ChessModelmm){cm=mm;width=cm.getWidth();height=cm.getHeight();}依据坐标计算出棋盘方格棋子信息(如白子还是黑子),然后调用draw方法在棋盘上画出对应棋子publicvoidpaintComponent(Graphicsg){super.paintComponent(g);for(intj=0;j<=height;j++){for(inti=0;i<=width;i++){intv=cm.getarrMapShow()[i][j];draw(g,i,j,v);}}}依据提供棋子信息(颜色、坐标)画棋子publicvoiddraw(Graphicsg,inti,intj,intv){intx=20*i+20;inty=20*j+20;画棋盘if(i!=width&&j!=height){g.setColor(Color.darkGray);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-8,y-8,16,16);}if(v==3){g.setColor(Color.cyan);g.drawOval(x-8,y-8,16,16);}}响应鼠标点击事件,依据鼠标点击来下棋,依据下棋判定胜败等publicvoidmousePressed(MouseEventevt){intx=(evt.getX()-10)/20;inty=(evt.getY()-10)/20;System.out.println(x+""+y);if(evt.getModifiers()==MouseEvent.BUTTON1_MASK){cm.play(x,y);System.out.println(cm.getisOdd()+""+cm.getarrMapShow()[x][y]);repaint();if(cm.judgeSuccess(x,y,cm.getisOdd())){cm.showSuccess(this);evt.consume();ChessFrame.iscomputer=false;}判定是否为人机对弈if(ChessFrame.iscomputer&&!cm.getisExist()){puterDo(cm.getWidth(),cm.getHeight());repaint();if(cm.judgeSuccess(cm.getX(),cm.getY(),cm.getisOdd())){cm.showDefeat(this);evt.consume();}}}}publicvoidmouseClicked(MouseEventevt){}publicvoidmouseReleased(MouseEventevt){}publicvoidmouseEntered(MouseEventmouseevt){}publicvoidmouseExited(MouseEventmouseevent){}publicvoidmouseDragged(MouseEventevt){}响应鼠标拖动事件publicvoidmouseMoved(MouseEventmoveevt){intx=(moveevt.getX()-10)/20;inty=(moveevt.getY()-10)/20;cm.readyplay(x,y);repaint();}}importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;响应退出窗口classChessWindowEventextendsWindowAdapter{publicvoidwindowClosing(WindowEvente){System.exit(0);}ChessWindowEvent(){}}四.程序调试和运行运行:标准棋盘改善棋盘:扩大棋盘:外观类型二:外观类型三:人机对战:结果:五.结论经过对五子棋游戏编写,使自己对java语言有了更深了解。也愈加熟悉和了解了java开发工具Eclipse使用。不过还有很多不足之处,比如没有能设置禁手,没有能设置悔棋,还有很多东西能够扩充完善。完整源代码:importjava.awt.*;importjava.awt.event.*;importjava.applet.*;importjavax.swing.*;importjava.io.PrintStream;importjavax.swing.JComponent;importjavax.swing.JPanel;classChessFrameextendsJFrameimplementsActionListener{privateString[]strsize={"标准棋盘","改善棋盘","扩大棋盘"};privateString[]strmode={"人机对战","人人对战"};publicstaticbooleaniscomputer=true,checkcomputer=true;privateintwidth,height;privateChessModelcm;privateMainPanelmp;publicChessFrame(){this.setTitle("五子棋游戏");cm=newChessModel(1);mp=newMainPanel(cm);Containercon=this.getContentPane();con.add(mp,"Center");this.setResizable(false);this.addWindowListener(newChessWindowEvent());MapSize(14,14);JMenuBarmbar=newJMenuBar();this.setJMenuBar(mbar);JMenugameMenu=newJMenu("游戏");mbar.add(makeMenu(gameMenu,newObject[]{"开局",null,"棋盘",null,"模式",null,"退出"},this));JMenulookMenu=newJMenu("外观");mbar.add(makeMenu(lookMenu,newObject[]{"类型一","类型二","类型三"},this));JMenuhelpMenu=newJMenu("版本");mbar.add(makeMenu(helpMenu,newObject[]{"相关"},this));}publicJMenumakeMenu(Objectparent,Objectitems[],Objecttarget){JMenum=null;if(parentinstanceofJMenu)m=(JMenu)parent;elseif(parentinstanceofString)m=newJMenu((String)parent);elsereturnnull;for(inti=0;i<items.length;i++)if(items[i]==null)m.addSeparator();elseif(items[i]=="棋盘"){JMenujm=newJMenu("棋盘");ButtonGroupgroup=newButtonGroup();JRadioButtonMenuItemrmenu;for(intj=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);}elseif(items[i]=="模式"){JMenujm=newJMenu("模式");ButtonGroupgroup=newButtonGroup();JRadioButtonMenuItemrmenu;for(inth=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);}elsem.add(makeMenuItem(items[i],target));returnm;}publicJMenuItemmakeMenuItem(Objectitem,Objecttarget){JMenuItemr=null;if(iteminstanceofString)r=newJMenuItem((String)item);elseif(iteminstanceofJMenuItem)r=(JMenuItem)item;elsereturnnull;if(targetinstanceofActionListener)r.addActionListener((ActionListener)target);returnr;}publicJRadioButtonMenuItemmakeRadioButtonMenuItem(Objectitem,Objecttarget){JRadioButtonMenuItemr=null;if(iteminstanceofString)r=newJRadioButtonMenuItem((String)item);elseif(iteminstanceofJRadioButtonMenuItem)r=(JRadioButtonMenuItem)item;elsereturnnull;if(targetinstanceofActionListener)r.addActionListener((ActionListener)target);returnr;}publicvoidMapSize(intw,inth){setSize(w*24,h*27);if(this.checkcomputer)this.iscomputer=true;elsethis.iscomputer=false;mp.setModel(cm);mp.repaint();}publicbooleangetiscomputer(){returnthis.iscomputer;}publicvoidrestart(){intmodeChess=cm.getModeChess();if(modeChess<=3&&modeChess>=0){cm=newChessModel(modeChess);MapSize(cm.getWidth(),cm.getHeight());}}publicvoidactionPerformed(ActionEvente){Stringarg=e.getActionCommand();try{if(arg.equals("类型三"))UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");elseif(arg.equals("类型二"))UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");elseUIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");SwingUtilities.updateComponentTreeUI(this);}catch(Exceptionee){}if(arg.equals("标准棋盘")){this.width=14;this.height=14;cm=newChessModel(1);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("改善棋盘")){this.width=18;this.height=18;cm=newChessModel(2);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("扩大棋盘")){this.width=22;this.height=22;cm=newChessModel(3);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("人机对战")){this.checkcomputer=true;this.iscomputer=true;cm=newChessModel(cm.getModeChess());MapSize(cm.getWidth(),cm.getHeight());SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("人人对战")){this.checkcomputer=false;this.iscomputer=false;cm=newChessModel(cm.getModeChess());MapSize(cm.getWidth(),cm.getHeight());SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("开局")){restart();}if(arg.equals("相关"))JOptionPane.showMessageDialog(null,"第一版","版本",JOptionPane.PLAIN_MESSAGE);if(arg.equals("退出"))System.exit(0);}}importjava.awt.*;importjava.awt.event.*;importjava.applet.*;importjavax.swing.*;importjava.io.PrintStream;importjavax.swing.JComponent;importjavax.swing.JPanel;classChessModel{privateintwidth,height,modeChess;privateintx=0,y=0;privateint[][]arrMapShow;privatebooleanisOdd,isExist;publicChessModel(){}publicChessModel(intmodeChess){this.isOdd=true;if(modeChess==1){PanelInit(14,14,modeChess);}if(modeChess==2){PanelInit(18,18,modeChess);}if(modeChess==3){PanelInit(22,22,modeChess);}}privatevoidPanelInit(intwidth,intheight,intmodeChess){this.width=width;this.height=height;this.modeChess=modeChess;arrMapShow=newint[width+1][height+1];for(inti=0;i<=width;i++){for(intj=0;j<=height;j++){arrMapShow[i][j]=-1;}}}publicbooleangetisOdd(){returnthis.isOdd;}publicvoidsetisOdd(booleanisodd){if(isodd)this.isOdd=true;elsethis.isOdd=false;}publicbooleangetisExist(){returnthis.isExist;}publicintgetWidth(){returnthis.width;}publicintgetHeight(){returnthis.height;}publicintgetModeChess(){returnthis.modeChess;}publicint[][]getarrMapShow(){returnarrMapShow;}privatebooleanbadxy(intx,inty){if(x>=width+20||x<0)returntrue;returny>=height+20||y<0;}publicbooleanchessExist(inti,intj){if(this.arrMapShow[i][j]==1||this.arrMapShow[i][j]==2)returntrue;returnfalse;}publicvoidreadyplay(intx,inty){if(badxy(x,y))return;if(chessExist(x,y))return;this.arrMapShow[x][y]=3;}publicvoidplay(intx,inty){if(badxy(x,y))return;if(chessExist(x,y)){this.isExist=true;return;}elsethis.isExist=false;if(getisOdd()){setisOdd(false);this.arrMapShow[x][y]=1;}else{setisOdd(true);this.arrMapShow[x][y]=2;}}publicvoidcomputerDo(intwidth,intheight){intmax_black,max_white,max_temp,max=0;setisOdd(true);System.out.println("计算机走棋...");for(inti=0;i<=width;i++){for(intj=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;}publicvoidsetX(intx){this.x=x;}publicvoidsetY(inty){this.y=y;}publicintgetX(){returnthis.x;}publicintgetY(){returnthis.y;}publicintcheckMax(intx,inty,intblack_or_white){intnum=0,max_num,max_temp=0;intx_temp=x,y_temp=y;intx_temp1=x_temp,y_temp1=y_temp;for(inti=1;i<5;i++){x_temp1+=1;if(x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}x_temp1=x_temp;for(inti=1;i<5;i++){x_temp1-=1;if(x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}if(num<5)max_temp=num;x_temp1=x_temp;y_temp1=y_temp;num=0;for(inti=1;i<5;i++){y_temp1-=1;if(y_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}y_temp1=y_temp;for(inti=1;i<5;i++){y_temp1+=1;if(y_temp1>this.height)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}if(num>max_temp&&num<5)max_temp=num;x_temp1=x_temp;y_temp1=y_temp;num=0;for(inti=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++;elsebreak;}x_temp1=x_temp;y_temp1=y_temp;for(inti=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++;elsebreak;}if(num>max_temp&&num<5)max_temp=num;x_temp1=x_temp;y_temp1=y_temp;num=0;for(inti=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++;elsebreak;}x_temp1=x_temp;y_temp1=y_temp;for(inti=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++;elsebreak;}if(num>max_temp&&num<5)max_temp=num;max_num=max_temp;returnmax_num;}publicbooleanjudgeSuccess(intx,inty,booleanisodd){intnum=1;intarrvalue;intx_temp=x,y_temp=y;if(isodd)arrvalue=2;elsearrvalue=1;intx_temp1=x_temp,y_temp1=y_temp;for(inti=1;i<6;i++){x_temp1+=1;if(x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}x_temp1=x_temp;for(inti=1;i<6;i++){x_temp1-=1;if(x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}if(num==5)returntrue;x_temp1=x_temp;y_temp1=y_temp;num=1;for(inti=1;i<6;i++){y_temp1-=1;if(y_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}y_temp1=y_temp;for(inti=1;i<6;i++){y_temp1+=1;if(y_temp1>this.height)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}if(num==5)returntrue;x_temp1=x_temp;y_temp1=y_temp;num=1;for(inti=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++;elsebreak;}x_temp1=x_temp;y_temp1=y_temp;for(inti=1;i<6;i++){x_temp1+=1;y_temp1+=1;if(y_temp1>this.height||x_temp1>this.width)break;if

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论