發新話題

[教學]手機版 踩地雷

[教學]手機版 踩地雷

複製內容到剪貼板
代碼:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;

public class bomb extends MIDlet implements ItemCommandListener{
        private Display display;
        private Image pic=null,tmppic=null;
        private Command tuch,flag,dont,none,cancel,cont;
        private boolean firtuch,losest,restart;
        private int flgc=0,bmn=0,tmp,bombary[][]=new int [5][5],flgary[][]=new int [5][5];
        private ImageItem stit,it[][]=new ImageItem[5][5];
        public Form frm,lwfrm;
        public bomb(){
                display = Display.getDisplay(this);
        }
        public void startApp(){
                int m,n,c;Random rnd=new Random();
                frm=new Form("手機踩地雷");
                try{pic = Image.createImage("/start.png");}catch(Exception e){}
                stit=new ImageItem("", pic, Item.LAYOUT_CENTER,"",Item.HYPERLINK);
                frm.append(stit);
                display.setCurrent(frm);
                try{Thread.sleep(2000);}catch(Exception e){}
                tuch=new Command("踩點", Command.ITEM, 1);
                flag=new Command("拆彈", Command.ITEM, 2);
                dont=new Command("標記", Command.ITEM, 3);
                none=new Command("恢復", Command.ITEM, 4);
                cancel=new Command("取消", Command.BACK, 5);
                frm=new Form("踩地雷");
                try{pic = Image.createImage("/by.png");}catch(Exception e){}
                for(int i=0;i<5;i++)for(int j=0;j<5;j++){
                                if(j==4)it[i][j]=new ImageItem("", pic, Item.LAYOUT_NEWLINE_AFTER,""+i+j,Item.HYPERLINK);
                                else it[i][j]=new ImageItem("", pic, Item.LAYOUT_LEFT ,""+i+j,Item.HYPERLINK);
                                it[i][j].addCommand(tuch);
                                it[i][j].addCommand(flag);
                                it[i][j].addCommand(dont);
                                it[i][j].addCommand(none);
                                it[i][j].addCommand(cancel);
                                it[i][j].setItemCommandListener(this);
                                frm.append(it[i][j]);
                }while(1==1){
                        firtuch=false;losest=false;restart=false;
                        flgc=0;bmn=0;
                        try{pic = Image.createImage("/by.png");}catch(Exception e){}
                        for(int i=0;i<5;i++)for(int j=0;j<5;j++){//Clear
                                it[i][j].setImage(pic);
                                flgary[i][j]=0;
                                bombary[i][j]=0;
                        }for(int i=0;i<2;i++)for(int j=0;j<5;j++)bombary[i][j]=9;//放入15顆炸彈  Ps. 25/15
                        for(int i=0;i<5;i++)for(int j=0;j<5;j++){//打散
                                m=Math.abs(rnd.nextInt()%5);
                                n=Math.abs(rnd.nextInt()%5);
                                c=bombary[i][j];
                                bombary[i][j]=bombary[m][n];
                                bombary[m][n]=c;
                        }display.setCurrent(frm);//顯示
                        while(true)
                                if(losest==true){
                                        check(0);
                                        break;
                                }
                        while(restart==false);
                }
        }
        public void commandAction(Command c, Item i){//事件觸發
                String comm = c.getLabel();
                ImageItem gotItem = (ImageItem)i;
                int okcount,count;
                int news[][]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
                int p=gotItem.getAltText().charAt(0)-'0';
                int q=gotItem.getAltText().charAt(1)-'0';
                if(comm.equals("繼續"))restart=true;
                if(comm.equals("踩點")){
                        if(firtuch==false){
                                for(int m=0;m<5;m++){
                                        for(int n=0;n<5;n++)
                                                if(bombary[m][n]<9){
                                                        tmp=bombary[m][n];
                                                        bombary[m][n]=bombary[p][q];
                                                        bombary[p][q]=tmp;
                                                        firtuch=true;
                                                        break;
                                                }if(firtuch==true)break;
                                }
                                for(int m=0;m<5;m++)for(int n=0;n<5;n++){
                                        if(bombary[m][n]==9)bmn++;
                                        else {
                                                count=0;
                                                for(int k=0;k<8;k++)
                                                        if(((m+news[k][0])>-1)&&((m+news[k][0])<5)&&((n+news[k][1])>-1)&&((n+news[k][1])<5))//判斷邊界
                                                                if(bombary[m+news[k][0]][n+news[k][1]]==9)count++;
                                                bombary[m][n]=count;
                                        }
                                }
                                for(int m=0;m<5;m++){                                            //Show the
                                        for(int n=0;n<5;n++)                                         //
                                                System.out.print(bombary[m][n]+" ");//
                                        System.out.println("");                                        //
                                }
                        }
                        if(bombary[p][q]!=9)try{pic = Image.createImage("/"+bombary[p][q]+".png");}catch(Exception e){}
                        else {
                                try{pic = Image.createImage("/9.png");}catch(Exception e){}
                                try{tmppic = Image.createImage("/0.png");}catch(Exception e){}
                                for(int m=0;m<5;m++)for(int n=0;n<5;n++)
                                        if(bombary[m][n]==9)it[m][n].setImage(pic);
                                        else it[m][n].setImage(tmppic);
                                try{pic = Image.createImage("/tuch.png");}catch(Exception e){}
                                it[p][q].setImage(pic);
                                losest=true;
                        }
                        bombary[p][q]=10;
                        it[p][q].setImage(pic);
                }if(comm.equals("拆彈")&&(flgary[p][q]!=1)){
                        flgary[p][q]=1;flgc++;okcount=0;
                        for(int m=0;m<5;m++)for(int n=0;n<5;n++)
                                if((flgary[m][n]==1)&&(bombary[m][n]==9))okcount++;//計算正確數量
                        if((okcount==bmn)&&(bmn==flgc)){
                                check(1);
                                restart=true;
                        }try{pic = Image.createImage("/flag.png");}catch(Exception e){}
                        it[p][q].setImage(pic);
                }if(comm.equals("標記")){
                        try{pic = Image.createImage("/dk.png");}catch(Exception e){}
                        it[p][q].setImage(pic);
                }if(comm.equals("恢復")){
                        if(flgary[p][q]==1){
                                flgary[p][q]=0;
                                flgc--;
                        }try{pic = Image.createImage("/by.png");}catch(Exception e){}
                        it[p][q].setImage(pic);
                }
        }
        public void check(int lw){
                String lwstr[]={"很抱歉 你輸囉","恭喜你 你贏囉"};
                String lwfile[]={"/lose.png","/win.png"};
                Image lwpic=null;
                ImageItem lwit;
                try{Thread.sleep(2000);}catch(Exception e){}
                lwfrm=new Form(lwstr[lw]);
                display.setCurrent(lwfrm);
                try{lwpic = Image.createImage(lwfile[lw]);}catch(Exception e){}
                lwit=new ImageItem("", lwpic, Item.LAYOUT_CENTER ,"00",Item.PLAIN);
                cont=new Command("繼續", Command.ITEM, 1);//繼續遊戲的案鈕
                lwit.addCommand(cont);
                lwit.setItemCommandListener(this);
                lwfrm.append(lwit);
        }
        public void pauseApp(){        
        
        }
        public void destroyApp(boolean destroyBoolean){
               
        }
}
[code]import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;

public class bomb extends MIDlet implements ItemCommandListener{
         ... [/quote]

[ 本帖最後由 philxyz0316 於 2006-8-8 20:39 編輯 ]

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。