斗地主发牌代码
package cn.j.day14.demo02.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class DouDiZhu_zonghe {
public static void main(String[] args) {
HashMap<Integer, String> poker = new HashMap<>();
List<Integer> index = new ArrayList<>();
//准备牌
List<String> colors = List.of("♣", "♦", "♠", "♥");
List<String> numbers = List.of("1", "2", "k", "q", "j", "10", "9", "8", "7", "6", "5", "4", "3");
int indexs = 0;
poker.put(indexs,"大王");
index.add(indexs);
indexs++;
poker.put(indexs,"小王");
index.add(indexs);
indexs++;
for (String co : colors) {
for (String nu : numbers) {
String s = co + nu;
poker.put(indexs,s);
index.add(indexs);
indexs++;
}
}
//洗牌
Collections.shuffle(index);
// System.out.println(index);
//发牌
ArrayList<Integer> play1 = new ArrayList<>();
ArrayList<Integer> play2 = new ArrayList<>();
ArrayList<Integer> play3 = new ArrayList<>();
ArrayList<Integer> dipai = new ArrayList<>();
for (int i = 0; i <=index.size()-1 ; i++) {
Integer in = index.get(i);
if (i>=51){
dipai.add(in);
}else if (i%3==0){
play1.add(in);
}else if (i%3==1){
play2.add(in);
}else if (i%3==2){
play3.add(in);
}
}
//排序
Collections.sort(play1);
Collections.sort(play2);
Collections.sort(play3);
Collections.sort(dipai);
loolpoker("唐伯虎",poker,play1);
loolpoker("祝枝山",poker,play2);
loolpoker("周文斌",poker,play3);
loolpoker("文征明",poker,dipai);
}
//看牌方法
public static void loolpoker(String name,HashMap<Integer,String> poker,ArrayList<Integer> index){
System.out.print(name+": ");
for (Integer i:index){
String value = poker.get(i);
System.out.print(value);
}
System.out.println();
}
}