【Java】鬥地主綜合案例

鬥地主綜合案例2


 

public class DouDiZhu {

public static void main(String[] args) {

//1.準備牌

//創建一個Map集合,存儲牌的索引和組裝好的牌

Map<Integer,String> poker = new HashMap<>();

//創建一個List集合,存儲牌的索引

List<Integer> pokerIndex = new ArrayList<>();

//定義兩個集合,存儲花色和牌的序號

List<String> colors = List.of("♥","♠","♦","♣");

List<String> numbers = List.of("2","3","4","5","6","7","8","9","10","J","Q","K","A");

int index = 0;

poker.put(index,"大王");

pokerIndex.add(index);

index++;

poker.put(index,"小王");

pokerIndex.add(index);

index++;

for (String number : numbers){

for(String color : colors){

//String temp = ""+color+number;

poker.put(index,color+number);

pokerIndex.add(index);

index++;

}

}

System.out.println(poker);

System.out.println(pokerIndex);

System.out.println("===================");

//2.洗牌

Collections.shuffle(pokerIndex);

//3.發牌

List<Integer> player01 = new ArrayList<>();

List<Integer> player02 = new ArrayList<>();

List<Integer> player03 = new ArrayList<>();

List<Integer> dipai = new ArrayList<>();

//4.排序 使用Collections.sort

int flag = 0;

for(int i : pokerIndex){

if(flag>50){

dipai.add(i);

}

else if(flag%3==0){

player01.add(i);

}else if(flag%3==1){

player02.add(i);

}else if(flag%3==2){

player03.add(i);

}

flag++;

}

//對玩家手裏的牌進行排序

Collections.sort(player01);

Collections.sort(player02);

Collections.sort(player03);

Collections.sort(dipai);

System.out.print("player01:");

for(int i : player01){

System.out.print(poker.get(i)+" ");

}

System.out.println();

System.out.print("player02:");

for(int i : player02){

System.out.print(poker.get(i)+" ");

}

System.out.println();

System.out.print("player03:");

for(int i : player03){

System.out.print(poker.get(i)+" ");

}

System.out.println();

System.out.print("底牌:");

for(int i : dipai){

System.out.print(poker.get(i)+" ");

}


}

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章