版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
JAVA課程設(shè)計(jì)設(shè)計(jì)題目:五子棋游戲簡(jiǎn)明介紹五子棋五子棋起源五子棋,又被稱為“連五子、五子連、串珠、五目、五目碰、五格、五石、五法、五聯(lián)、京棋”。五子棋相傳起源于四千多年前堯帝時(shí)期,比圍棋歷史還要悠久,可能早在“堯造圍棋”之前,民間就已經(jīng)有五子棋游戲。相關(guān)早期五子棋文史資料和圍棋有相同之處,因?yàn)楣糯遄悠迤寰吆蛧迨峭耆嗤?2.現(xiàn)在五子棋標(biāo)準(zhǔn)棋盤(圖所表示)3.五子棋棋子五子棋采取兩種顏色棋子,黑色棋子和白色棋子,和圍棋相同,4.五子棋規(guī)則五子棋就是五個(gè)棋子連在一起就算贏,黑棋先行,下棋下在棋盤交叉線上,因?yàn)楹谄逑刃校瑑?yōu)勢(shì)太大,所以對(duì)黑棋設(shè)了禁手,又要求了“三手交換”,就是黑棋下第2手棋,盤面第3著棋以后,白方在應(yīng)白2之前,如感覺(jué)黑方棋形不利于己方,可出交換,即執(zhí)白棋一方變?yōu)閳?zhí)黑棋一方。和“五手兩打法”,就是黑棋在下盤面上關(guān)鍵第5手時(shí),必需下兩步棋,讓白方在這兩步棋中任選一步,然后再續(xù)下。不過(guò)通常愛(ài)好者不需要遵照這么多規(guī)則。二.程序步驟三.代碼設(shè)計(jì)和分析main方法創(chuàng)建了ChessFrame類一個(gè)實(shí)例對(duì)象(cf),并開(kāi)啟屏幕顯示顯示該實(shí)例對(duì)象。publicclassFiveChessAppletDemo{publicstaticvoidmain(Stringargs[]){ChessFramecf=newChessFrame();cf.show();}}用類ChessFrame創(chuàng)建五子棋游戲主窗體和菜單importjava.awt.*;importjava.awt.event.*;importjava.applet.*;importjavax.swing.*;importjava.io.PrintStream;importjavax.swing.JComponent;importjavax.swing.JPanel;classChessFrameextendsJFrameimplementsActionListener{privateString[]strsize={"標(biāo)準(zhǔn)棋盤","改善棋盤","擴(kuò)大棋盤"};privateString[]strmode={"人機(jī)對(duì)戰(zhàn)","人人對(duì)戰(zhàn)"};publicstaticbooleaniscomputer=true,checkcomputer=true;privateintwidth,height;privateChessModelcm;privateMainPanelmp;結(jié)構(gòu)五子棋游戲主窗體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[]{"開(kāi)局",null,"棋盤",null,"模式",null,"退出"},this));JMenulookMenu=newJMenu("外觀");mbar.add(makeMenu(lookMenu,newObject[]{"類型一","類型二","類型三"},this));JMenuhelpMenu=newJMenu("版本");mbar.add(makeMenu(helpMenu,newObject[]{"相關(guān)"},this));}結(jié)構(gòu)五子棋游戲主菜單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;}結(jié)構(gòu)五子棋游戲菜單項(xiàng)publicJMenuItemmakeMenuItem(Objectitem,Objecttarget){JMenuItemr=null;if(iteminstanceofString)r=newJMenuItem((String)item);elseif(iteminstanceofJMenuItem)r=(JMenuItem)item;elsereturnnull;if(targetinstanceofActionListener)r.addActionListener((ActionListener)target);returnr;}結(jié)構(gòu)五子棋游戲單選按鈕式菜單項(xiàng)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("標(biāo)準(zhǔn)棋盤")){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("擴(kuò)大棋盤")){this.width=22;this.height=22;cm=newChessModel(3);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("人機(jī)對(duì)戰(zhàn)")){this.checkcomputer=true;this.iscomputer=true;cm=newChessModel(cm.getModeChess());MapSize(cm.getWidth(),cm.getHeight());SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("人人對(duì)戰(zhàn)")){this.checkcomputer=false;this.iscomputer=false;cm=newChessModel(cm.getModeChess());MapSize(cm.getWidth(),cm.getHeight());SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("開(kāi)局")){restart();}if(arg.equals("相關(guān)"))JOptionPane.showMessageDialog(null,"第一版","版本",JOptionPane.PLAIN_MESSAGE);if(arg.equals("退出"))System.exit(0);}}用類ChessModel實(shí)現(xiàn)了整個(gè)五子棋程序算法關(guān)鍵importjava.awt.*;importjava.awt.event.*;importjava.applet.*;importjavax.swing.*;importjava.io.PrintStream;importjavax.swing.JComponent;importjavax.swing.JPanel;classChessModel{要求棋盤寬度、高度、棋盤模式privateintwidth,height,modeChess;要求棋盤方格橫向、縱向坐標(biāo)privateintx=0,y=0;棋盤方格橫向、縱向坐標(biāo)所對(duì)應(yīng)棋子顏色,數(shù)組arrMapShow只有3個(gè)值:1,2,3,-1,其中1代表該棋盤方格上下棋子為黑子,2代表該棋盤方格上下棋子為白子,3代表為該棋盤方格上沒(méi)有棋子,-1代表該棋盤方格不能夠下棋子privateint[][]arrMapShow;交換棋手標(biāo)識(shí),棋盤方格上是否有棋子標(biāo)識(shí)符privatebooleanisOdd,isExist;publicChessModel(){}該結(jié)構(gòu)方法依據(jù)不一樣棋盤模式(modeChess)來(lái)構(gòu)建對(duì)應(yīng)大小棋盤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);}}根據(jù)棋盤模式構(gòu)建棋盤大小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;}}}獲取是否交換棋手標(biāo)識(shí)符publicbooleangetisOdd(){returnthis.isOdd;}設(shè)置交換棋手標(biāo)識(shí)符publicvoidsetisOdd(booleanisodd){if(isodd)this.isOdd=true;elsethis.isOdd=false;}獲取某棋盤方格是否有棋子標(biāo)識(shí)值publicbooleangetisExist(){returnthis.isExist;}獲取棋盤寬度publicintgetWidth(){returnthis.width;}獲取棋盤高度publicintgetHeight(){returnthis.height;}獲取棋盤模式publicintgetModeChess(){returnthis.modeChess;}獲取棋盤方格上棋子信息publicint[][]getarrMapShow(){returnarrMapShow;}判定下子橫向、縱向坐標(biāo)是否越界privatebooleanbadxy(intx,inty){if(x>=width+20||x<0)returntrue;returny>=height+20||y<0;}計(jì)算棋盤上某一方格上八個(gè)方向棋子最大值,這八個(gè)方向分別是:左、右、上、下、左上、左下、右上、右下publicbooleanchessExist(inti,intj){if(this.arrMapShow[i][j]==1||this.arrMapShow[i][j]==2)returntrue;returnfalse;}判定該坐標(biāo)位置是否可下棋子publicvoidreadyplay(intx,inty){if(badxy(x,y))return;if(chessExist(x,y))return;this.arrMapShow[x][y]=3;}在該坐標(biāo)位置下棋子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;}}計(jì)算機(jī)走棋說(shuō)明:用窮舉法判定每一個(gè)坐標(biāo)點(diǎn)四個(gè)方向最大棋子數(shù),最終得出棋子數(shù)最大值坐標(biāo),下子publicvoidcomputerDo(intwidth,intheight){intmax_black,max_white,max_temp,max=0;setisOdd(true);System.out.println("計(jì)算機(jī)走棋...");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;}統(tǒng)計(jì)電腦下子后橫向坐標(biāo)publicvoidsetX(intx){this.x=x;}統(tǒng)計(jì)電腦下子后縱向坐標(biāo)publicvoidsetY(inty){this.y=y;}獲取電腦下子橫向坐標(biāo)publicintgetX(){returnthis.x;}獲取電腦下子縱向坐標(biāo)publicintgetY(){returnthis.y;}計(jì)算棋盤上某一方格上八個(gè)方向棋子最大值,這八個(gè)方向分別是:左、右、上、下、左上、左下、右上、右下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;判定右邊f(xié)or(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,"你贏了","結(jié)果",JOptionPane.INFORMATION_MESSAGE);}輸棋后提醒publicvoidshowDefeat(JPaneljp){JOptionPane.showMessageDialog(jp,"你輸了","結(jié)果",JOptionPane.INFORMATION_MESSAGE);}}用類MainPanel關(guān)鍵完成以下功效:1、構(gòu)建一個(gè)面板,在該面板上畫(huà)上棋盤;2、處理在該棋盤上鼠標(biāo)事件(如鼠標(biāo)左鍵點(diǎn)擊、鼠標(biāo)右鍵點(diǎn)擊、鼠標(biāo)拖動(dòng)等)importjava.awt.*;importjava.awt.event.*;importjava.applet.*;importjavax.swing.*;importjava.io.PrintStream;importjavax.swing.JComponent;importjavax.swing.JPanel;classMainPanelextendsJPanelimplementsMouseListener,MouseMotionListener{設(shè)定棋盤寬度和高度privateintwidth,height;privateChessModelcm;依據(jù)棋盤模式設(shè)定面板大小MainPanel(ChessModelmm){cm=mm;width=cm.getWidth();height=cm.getHeight();addMouseListener(this);}依據(jù)棋盤模式設(shè)定棋盤寬度和高度publicvoidsetModel(ChessModelmm){cm=mm;width=cm.getWidth();height=cm.getHeight();}依據(jù)坐標(biāo)計(jì)算出棋盤方格棋子信息(如白子還是黑子),然后調(diào)用draw方法在棋盤上畫(huà)出對(duì)應(yīng)棋子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);}}}依據(jù)提供棋子信息(顏色、坐標(biāo))畫(huà)棋子publicvoiddraw(Graphicsg,inti,intj,intv){intx=20*i+20;inty=20*j+20;畫(huà)棋盤if(i!=width&&j!=height){g.setColor(Color.darkGray);g.drawRect(x,y,20,20);}畫(huà)黑色棋子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);}畫(huà)白色棋子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);}}響應(yīng)鼠標(biāo)點(diǎn)擊事件,依據(jù)鼠標(biāo)點(diǎn)擊來(lái)下棋,依據(jù)下棋判定勝敗等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;}判定是否為人機(jī)對(duì)弈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){}響應(yīng)鼠標(biāo)拖動(dòng)事件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;響應(yīng)退出窗口classChessWindowEventextendsWindowAdapter{publicvoidwindowClosing(WindowEvente){System.exit(0);}ChessWindowEvent(){}}四.程序調(diào)試和運(yùn)行運(yùn)行:標(biāo)準(zhǔn)棋盤改善棋盤:擴(kuò)大棋盤:外觀類型二:外觀類型三:人機(jī)對(duì)戰(zhàn):結(jié)果:五.結(jié)論經(jīng)過(guò)對(duì)五子棋游戲編寫,使自己對(duì)java語(yǔ)言有了更深了解。也愈加熟悉和了解了java開(kāi)發(fā)工具Eclipse使用。不過(guò)還有很多不足之處,比如沒(méi)有能設(shè)置禁手,沒(méi)有能設(shè)置悔棋,還有很多東西能夠擴(kuò)充完善。完整源代碼:importjava.awt.*;importjava.awt.event.*;importjava.applet.*;importjavax.swing.*;importjava.io.PrintStream;importjavax.swing.JComponent;importjavax.swing.JPanel;classChessFrameextendsJFrameimplementsActionListener{privateString[]strsize={"標(biāo)準(zhǔn)棋盤","改善棋盤","擴(kuò)大棋盤"};privateString[]strmode={"人機(jī)對(duì)戰(zhàn)","人人對(duì)戰(zhàn)"};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[]{"開(kāi)局",null,"棋盤",null,"模式",null,"退出"},this));JMenulookMenu=newJMenu("外觀");mbar.add(makeMenu(lookMenu,newObject[]{"類型一","類型二","類型三"},this));JMenuhelpMenu=newJMenu("版本");mbar.add(makeMenu(helpMenu,newObject[]{"相關(guān)"},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("標(biāo)準(zhǔn)棋盤")){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("擴(kuò)大棋盤")){this.width=22;this.height=22;cm=newChessModel(3);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("人機(jī)對(duì)戰(zhàn)")){this.checkcomputer=true;this.iscomputer=true;cm=newChessModel(cm.getModeChess());MapSize(cm.getWidth(),cm.getHeight());SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("人人對(duì)戰(zhàn)")){this.checkcomputer=false;this.iscomputer=false;cm=newChessModel(cm.getModeChess());MapSize(cm.getWidth(),cm.getHeight());SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("開(kāi)局")){restart();}if(arg.equals("相關(guān)"))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("計(jì)算機(jī)走棋...");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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 施工現(xiàn)場(chǎng)施工防恐怖襲擊制度
- 現(xiàn)代科技在學(xué)生心理健康教育中的應(yīng)用前景
- 科技教育與家庭教育的融合策略
- 拆除工程專項(xiàng)施工方案和技術(shù)措施
- 個(gè)人與單位借款合同模板大全
- 專業(yè)拳擊教練聘任合同
- 產(chǎn)學(xué)研合作協(xié)議合同新
- 個(gè)人雇傭合同樣本
- 個(gè)人購(gòu)房抵押借款合同范本
- 個(gè)人車輛投資共享合同2025
- 《新時(shí)代公民道德建設(shè)實(shí)施綱要》、《新時(shí)代愛(ài)國(guó)主義教育實(shí)施綱要》知識(shí)競(jìng)賽試題庫(kù)55題(含答案)
- 2024-2030年中國(guó)假睫毛行業(yè)市場(chǎng)發(fā)展趨勢(shì)與前景展望戰(zhàn)略分析報(bào)告
- 2019-2020學(xué)年七年級(jí)(上)期末數(shù)學(xué)試卷2附解析
- 德國(guó)職業(yè)學(xué)校教育質(zhì)量保障體系研究
- 第3篇 助跑 項(xiàng)目六 異形芯片分揀與安裝講解
- 傳統(tǒng)戲劇藝術(shù)與人工智能的創(chuàng)新傳播渠道
- 2024年越南天然食用香料與色素行業(yè)現(xiàn)狀及前景分析2024-2030
- 實(shí)體瘤療效評(píng)價(jià)標(biāo)準(zhǔn)(RECIST11)
- 教案-中國(guó)書(shū)法史
- 隧道施工-緒論(使用)
- 2024年湖南高速鐵路職業(yè)技術(shù)學(xué)院?jiǎn)握新殬I(yè)適應(yīng)性測(cè)試題庫(kù)附答案
評(píng)論
0/150
提交評(píng)論