台灣最具影響力的-資訊專家社群 - 讓您成為最有價值的IT專業人才
線上人數
1576
 
會員總數:229130
接案會員:6761
文章總數:2320
討論主題:175195
歡迎您免費加入會員
討論區列表 >> Java >> [JAVA] [求助] 撲克牌與踩地雷的寫法....(內附程式碼)

[變換順序]
[我要回覆]
1


回應主題 加入我的關注話題 檢舉此篇討論 將提問者加入個人黑名單
[JAVA] [求助] 撲克牌與踩地雷的寫法....(內附程式碼)
價值 : 0 QP  點閱數:4618 回應數:1

點圖分享到Plurk吧!
樓主

凌ㄚ哲
初學者
3 1
4 1
發送站內信

我有兩個程式要請問的....
一....踩地雷...
這題是說..要把踩地雷的地圖寫出來....
用 10*10 的陣列..放入30顆地雷

111
1*1
111

地雷用*號表示...數字是代表附近的地雷數...
沒有地雷就寫0....

以下是我寫出來的..
請各位幫我看看...我可以怎麼改進...或是有什麼更好的寫法....
class bombmap{
        public static void main(String args[]){
                int i,j,k,temp,x,y,counter1,counter2;
                int map[][] = new int [10][10];
                boolean repeat;
                temp = 0;
                counter1 = 0;
                counter2 = 0;
                for(i=0;i<10;i++)
                        for(j=0;j<10;j++)
                                map[i][j]=0;
                for(k=0;k<30;k++){
                        repeat = false;
                        counter1++;
                        while(!repeat){
                                repeat = true;
                                counter2=0;
                                x = (int)(Math.random()*10);
                                y = (int)(Math.random()*10);
                                map[x][y]=-1;
                                for(i=0;i<10;i++)
                                        for(j=0;j<10;j++)
                                                if(map[i][j]==-1)
                                                        counter2++;
                                if(counter1!=counter2)
                                        repeat = false;
                        }
                }
                for(i=0;i<10;i++)
                        for(j=0;j<10;j++)
                                if(map[i][j]==-1)
                                        for(x=(i-1);x<(i+1);x++)
                                                for(y=(j-1);y<(j+1);y++)
                                                        if(x>0 && y>0 && x<10 && y<10 && map[x][y]!=-1)
                                                                map[x][y]++;
                System.out.println();
                for(i=0;i<10;i++){
                        System.out.print("\t");
                        for(j=0;j<10;j++){                
                                if(map[i][j]==-1)
                                        System.out.print("*");
                                else
                                        System.out.print(map[i][j]);
                        }
                        System.out.println();                
                }                        
        }
}


二..撲克牌
這題是要隨機選52個數出來...
52/13   拿來當花色...
52%13  拿來當點數...
發五張牌...
然後再來判斷結果為何...
對子.兩對.葫蘆.鐵枝.順子.同花.同花順

以下是我的程式碼...請各位高手幫我看看是否哪裡可以改進..
或是有更好的寫法...謝謝...

class puke{
        public static void main(String args[]){
                int i,j,k,temp,d,sc,result,n,counter;
                int o[] = new int [5];//亂數
                int c[] = new int [5];//花色
                int p[] = new int [5];//點數
                int s[] = new int [4];//相同的個數
                boolean repeat;//避開相同亂數
                for(k=0;k<5;k++){//發幾次牌
                d=0;//數字之間的連帶關係
                sc=0;//相同花色的數目
                result=0;//情況
                n=1;//不重複的個數
                temp=0;
                for(i=0;i<4;i++)//陣列歸零
                        s[i]=0;
                for(i=0;i<5;i++){
                        repeat = false;
                        /*---- 檢查是否有相同的亂數 ----*/
                        while(!repeat){
                                repeat = true;
                                temp = (int)(Math.random()*52+1);
                                if(i!=0)
                                        for(j=(i-1);j>=0;j--)
                                                if(temp == o[j])
                                                        repeat = false;
                        }
                        /*---- End ----*/
                        o[i] = temp;
                }
                for(i=0;i<5;i++){
                        c[i] = o[i] / 13 +1;
                        p[i] = o[i] % 13;
                        if(p[i]==0){
                                p[i]=13;
                                c[i]--;
                        }
                }
                for(i=0;i<5;i++){
                        for(j=(i+1);j<5;j++){
                                if(p[i]>p[j]){
                                        temp = p[i];
                                        p[i] = p[j];
                                        p[j] = temp;
                                        temp = c[i];
                                        c[i] = c[j];
                                        c[j] = temp;
                                }
                        }
                }
                /*---- 判斷總共有幾個數字以及重複的個數 ----*/                        
                for(i=0;i<4;i++){
                        counter=0;
                        for(j=(i+1);j<5;j++){
                                if(p[i]==p[j]){
                                        s[i]++;
                                        counter++;
                                }
                                else{
                                        n++;
                                        break;
                                }
                        }
                        i+=counter;
                }        
                /*---- 判斷 End ----*/
                /*---- 判斷同花 -----*/
                i=0;
                for(j=1;j<5;j++)
                        if(c[i] == c[j] )
                                sc++;
                if(sc==4)
                        result = 4;
                /*---- 判斷同花 End ----*/
                /*---- 判斷連續關係 ----*/                                
                for(i=4;i>0;i--)
                        if((p[i]-p[i-1])==1)
                                d++;
                /*---- 判斷連續關係 End ----*/
                /*---- 判斷所有情況 ----*/
                counter=0;
                switch(n){
                        case 2 : for(i=0;i<4;i++){
                                         if(s[i]==3)
                                                 result = 7;
                                         else
                                                 result = 6;
                                         }                
                                 break;
                        case 3 : for(i=0;i<4;i++){
                                        if(s[i]==2)
                                                result = 3;
                                        else
                                                result = 2;
                                 }
                                 break;
                        case 4 : result = 1;
                                 break;
                        case 5 : if(d == 4){
                                        if(sc == 4)
                                                result = 8;
                                        else
                                                result = 5;
                                 }
                                 /*---- 特殊情形(A 10 J Q K) ----*/
                                 else if( ((p[1]-p[0])==9) && d==3 )
                                         result = 5;
                                 else if( ((p[1]-p[0])==9) && d==3 && (sc==4))
                                         result = 8;
                                 /*---- End ----*/
                                 else
                                         result = 0;        
                                 break;
                }
                /*---- 判斷所有情況 End----*/        
                /*---- 印出結果 ----*/
                System.out.println();
                System.out.print("你拿到的牌是 :");
                for(i=0;i<5;i++){
                        switch(c[i]){
                                case 1 : System.out.print("\t黑桃 "+p[i]);
                                         break;
                                case 2 : System.out.print("\t紅心 "+p[i]);
                                         break;
                                case 3 : System.out.print("\t方塊 "+p[i]);
                                         break;
                                case 4 : System.out.print("\t梅花 "+p[i]);
                                         break;                
                        }
                }
                System.out.println();                                                                 
                System.out.print("U got ");
                switch(result){
                        case 0 : System.out.println("Nothing!!");
                                 break;
                        case 1 : System.out.println("One Pair!!");
                                 break;
                        case 2 : System.out.println("Two Pair!!");
                                 break;
                        case 3 : System.out.println("Triple!!");
                                 break;
                        case 4 : System.out.println("Flush!!");
                                 break;
                        case 5 : System.out.println("Straight!!");
                                 break;
                        case 6 : System.out.println("Full House!!");
                                 break;
                        case 7 : System.out.println("Four a Kind!!");
                                 break;
                        case 8 : System.out.println("Flush Straight!!");
                                 break;
                }
                /*---- 結果 End ----*/
                System.out.println();        
                }
        }
}



本篇文章發表於2004-12-30 01:39
什麼是iT Power資訊報 2010年藍色小舖認證專家風雲榜完全名單
1樓
作者回應

凌ㄚ哲
檢舉此回應
更正!!!!
撲克牌有地方寫錯!!!
/*---- 判斷所有情況 ----*/
這個地方
以下是更正過的....
/*---- 判斷所有情況 ----*/
                switch(n){
                        case 2 : result = 6;
                                 for(i=0;i<4;i++)
                                         if(s[i]==3)
                                                 result = 7;                
                                 break;
                        case 3 : result = 2;
                                 for(i=0;i<4;i++)
                                        if(s[i]==2)
                                                result = 3;
                                 break;
                        case 4 : result = 1;
                                 break;
                        case 5 : result = 0;
                                 if(d == 4){
                                        if(sc == 4)
                                                result = 8;
                                        else
                                                 result = 5;
                                 }                
                                 /*---- 特殊情形(A 10 J Q K) ----*/
                                 if( ((p[1]-p[0])==9) && d==3 )
                                         result = 5;
                                 if( ((p[1]-p[0])==9) && d==3 && (sc==4))
                                         result = 8;
                                 /*---- End ----*/
                                 break;
                }
                /*---- 判斷所有情況 End----*/        


本篇文章回覆於2004-12-30 02:59
--未登入的會員無法查看對方簽名檔--
[變換順序]
1
 

回覆
如要回應,請先登入.