Design Ideas of Doudizhu:
- Prepare cards: 54 cards, stored in a set
Special cards: King, King
Other 52 cards: define an array/set, store 4 suits: ♠, ♥, ♣, ♦
Define an array/set, store 13 serial numbers: 2, A, K,,,,,,3
Loop setting to traverse two arrays/collections, and assemble 52 cards: ♠2, ♠A,,,,,,,
- Shuffle
Methods of using collection tools Collections
Static void shuffle(List<?> list) uses the specified random source to replace the specified list
Randomly disrupt the order of elements in the set
- Licensing
Requirement: 1 player has 17 cards, the remaining 3 cards are the hole cards, and each card is dealt in turn: the index of the set (0-53)%3
Define 4 sets to store the cards and hole cards of 3 players
- Look at the cards
Print the set directly, traverse the set storing players and hole cards
package com.itheima.demo.demo1;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
/*斗地主案例:
1.准备牌
2.洗牌
3.发牌
4,看牌
*/
public class doudizhu {
public static void main(String[] args) {
//1.准备牌
//定义一个存储54张牌的arrayList集合,泛型使用String
ArrayList<String> poker=new ArrayList<>();
//定义俩个数组,一个花色,一个数字
String [] colors={"♠","♥","♣","♦"};
String [] numbers ={"2","A","k","q","j","10","9","8","7","6","5","4","3"};
//把大小王存储到poker里
poker.add("大王");
poker.add("小王");
//2.洗牌
//先把所有牌放入一个集合中
for (String number : numbers) {
for (String color : colors) {
poker.add(color+number);
//System.out.print(poker);
}
}
// System.out.print(poker);
//调用集合的工具类collection中的方法进行洗牌
Collections.shuffle(poker);
//System.out.print(poker);
//3.发牌
//定义4个集合代表3个玩家和一个底牌
ArrayList<String> wanjia1=new ArrayList<>();
ArrayList<String> wanjia2=new ArrayList<>();
ArrayList<String> wanjia3=new ArrayList<>();
ArrayList<String> dipai=new ArrayList<>();
//然后发牌,最后三张给底牌,前51张用模3,0给玩家1,1给玩家2,2给玩家三
for(int i=0;i<poker.size();i++){
String p=poker.get(i);
if(i>=51){
dipai.add(p);
}else if(i%3 ==0){
wanjia1.add(p);
}else if(i%3 ==1){
wanjia2.add(p);
}else if(i%3 ==2){
wanjia3.add(p);
}
}
//System.out.print(dipai);
//4看牌
System.out.println("小海"+wanjia1);
System.out.println("贺哥"+wanjia2);
System.out.println("娅姐"+wanjia3);
System.out.println("底牌"+dipai);
}
}