JAVA專業(yè)課程設計五子棋內附完整代碼_第1頁
JAVA專業(yè)課程設計五子棋內附完整代碼_第2頁
JAVA專業(yè)課程設計五子棋內附完整代碼_第3頁
JAVA專業(yè)課程設計五子棋內附完整代碼_第4頁
JAVA專業(yè)課程設計五子棋內附完整代碼_第5頁
已閱讀5頁,還剩57頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

JAVA課程設計設計題目:五子棋游戲簡明介紹五子棋五子棋起源五子棋,又被稱為“連五子、五子連、串珠、五目、五目碰、五格、五石、五法、五聯(lián)、京棋”。五子棋相傳起源于四千多年前堯帝時期,比圍棋歷史還要悠久,可能早在“堯造圍棋”之前,民間就已經(jīng)有五子棋游戲。相關早期五子棋文史資料和圍棋有相同之處,因為古代五子棋棋具和圍棋是完全相同。 2.現(xiàn)在五子棋標準棋盤(圖所表示)3.五子棋棋子五子棋采取兩種顏色棋子,黑色棋子和白色棋子,和圍棋相同,4.五子棋規(guī)則五子棋就是五個棋子連在一起就算贏,黑棋先行,下棋下在棋盤交叉線上,因為黑棋先行,優(yōu)勢太大,所以對黑棋設了禁手,又要求了“三手交換”,就是黑棋下第2手棋,盤面第3著棋以后,白方在應白2之前,如感覺黑方棋形不利于己方,可出交換,即執(zhí)白棋一方變?yōu)閳?zhí)黑棋一方。和“五手兩打法”,就是黑棋在下盤面上關鍵第5手時,必需下兩步棋,讓白方在這兩步棋中任選一步,然后再續(xù)下。不過通常愛好者不需要遵照這么多規(guī)則。二.程序步驟三.代碼設計和分析main方法創(chuàng)建了ChessFrame類一個實例對象(cf),并開啟屏幕顯示顯示該實例對象。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={"標準棋盤","改善棋盤","擴大棋盤"};privateString[]strmode={"人機對戰(zhàn)","人人對戰(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[]{"開局",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("人機對戰(zhàn)")){this.checkcomputer=true;this.iscomputer=true;cm=newChessModel(cm.getModeChess());MapSize(cm.getWidth(),cm.getHeight());SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("人人對戰(zhàn)")){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實現(xiàn)了整個五子棋程序算法關鍵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;棋盤方格橫向、縱向坐標所對應棋子顏色,數(shù)組arrMapShow只有3個值:1,2,3,-1,其中1代表該棋盤方格上下棋子為黑子,2代表該棋盤方格上下棋子為白子,3代表為該棋盤方格上沒有棋子,-1代表該棋盤方格不能夠下棋子privateint[][]arrMapShow;交換棋手標識,棋盤方格上是否有棋子標識符privatebooleanisOdd,isExist;publicChessModel(){}該結構方法依據(jù)不一樣棋盤模式(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);}}根據(jù)棋盤模式構建棋盤大小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;}}計算機走棋說明:用窮舉法判定每一個坐標點四個方向最大棋子數(shù),最終得出棋子數(shù)最大值坐標,下子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;}統(tǒng)計電腦下子后橫向坐標publicvoidsetX(intx){this.x=x;}統(tǒng)計電腦下子后縱向坐標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;判定右邊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,"你贏了","結果",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;依據(jù)棋盤模式設定面板大小MainPanel(ChessModelmm){cm=mm;width=cm.getWidth();height=cm.getHeight();addMouseListener(this);}依據(jù)棋盤模式設定棋盤寬度和高度publicvoidsetModel(ChessModelmm){cm=mm;width=cm.getWidth();height=cm.getHeight();}依據(jù)坐標計算出棋盤方格棋子信息(如白子還是黑子),然后調用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);}}}依據(jù)提供棋子信息(顏色、坐標)畫棋子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);}}響應鼠標點擊事件,依據(jù)鼠標點擊來下棋,依據(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;}判定是否為人機對弈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(){}}四.程序調試和運行運行:標準棋盤改善棋盤:擴大棋盤:外觀類型二:外觀類型三:人機對戰(zhàn):結果:五.結論經(jīng)過對五子棋游戲編寫,使自己對java語言有了更深了解。也愈加熟悉和了解了java開發(fā)工具Eclipse使用。不過還有很多不足之處,比如沒有能設置禁手,沒有能設置悔棋,還有很多東西能夠擴充完善。完整源代碼:importjava.awt.*;importjava.awt.event.*;importjava.applet.*;importjavax.swing.*;importjava.io.PrintStream;importjavax.swing.JComponent;importjavax.swing.JPanel;classChessFrameextendsJFrameimplementsActionListener{privateString[]strsize={"標準棋盤","改善棋盤","擴大棋盤"};privateString[]strmode={"人機對戰(zhàn)","人人對戰(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[]{"開局",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("人機對戰(zhàn)")){this.checkcomputer=true;this.iscomputer=true;cm=newChessModel(cm.getModeChess());MapSize(cm.getWidth(),cm.getHeight());SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("人人對戰(zhàn)")){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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論