1. 程式人生 > >隨機發牌器-程式碼

隨機發牌器-程式碼



public class Fapaiqi{
 public static void main(String args[]){

//定義一個數組放入54張牌
  String cards[]={"紅桃A","紅桃2","紅桃3","紅桃4","紅桃5","紅桃6","紅桃7","紅桃8","紅桃9","紅桃10","紅桃J","紅桃Q","紅桃K",
                  "黑桃A","黑桃2","黑桃3","黑桃4","黑桃5","黑桃6","黑桃7","黑桃8","黑桃9","黑桃10","黑桃J","黑桃O","黑桃K",
      "方塊A","方塊2","方塊3","方塊4","方塊5","方塊6","方塊7","方塊8","方塊9","方塊10","方塊J","方塊O","方塊K",
      "梅花A","梅花2","梅花3","梅花4","梅花5","梅花6","梅花7","梅花8","梅花9","梅花10","梅花J","梅花O","梅花K","大王","小王"};
      //隨機產生三張地主牌
      String []cards1=new String[3];
      int i=0;
      while(i<cards1.length){
       int index=(int)(Math.random()*cards.length);
       if(cards[index].equals("no")){
        continue;
       }
       cards1[i]=cards[index];
       cards[index]="no";
      i++;}System.out.println("地主牌:");
      for(int a=0;a<cards1.length;a++){
      System.out.print(cards1[a]+"\t");}
      //產生玩家一的17張牌
      String []player1=new String[17];
      int j=0;
      while(j<player1.length){
       int index=(int)(Math.random()*cards.length);
       if(cards[index].equals("no")){
        continue;
       }player1[j]=cards[index];
        cards[index]="no";j++;}
        System.out.println();
      System.out.println("玩家一:");
      for(int b=0;b<player1.length;b++){
       System.out.print(player1[b]+"\t");
      }
      //產生玩家二的17張牌
      String player2[]=new String[17];
      int k=0;
      while(k<player2.length){
       int index=(int)(Math.random()*cards.length);
       if(cards[index].equals("no")){
        continue;
       }player2[k]=cards[index];
        cards[index]="no";k++;}
        System.out.println();
       System.out.println("玩家二:");
       for(int c=0;c<player2.length;c++){
        System.out.print(player2[c]+"\t");
       }
       //產生玩家三的17張牌
       String player3[]=new String[17];
      int p=0;
      while(p<player3.length){
       int index=(int)(Math.random()*cards.length);
       if(cards[index].equals("no")){
        continue;
       }player3[p]=cards[index];
        cards[index]="no";p++;}
        System.out.println();
       System.out.println("玩家三:");
       for(int c=0;c<player3.length;c++){
        System.out.print(player3[c]+"\t");
 }   
 }
    }