斗地主案例
/*
* 1,准备牌
* 2.发牌
* 3.排序
* 4.看牌*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class DouDiZhu {
public static void main(String[] args) {
//1.准备牌
HashMap<Integer,String> poker = new HashMap<>();
ArrayList<Integer> pokerIndex = new ArrayList<>();
String[] color = {"♠","♥","♣","♦"};
String[] numbers = {"2","A","K","J","Q","10","9","8","7","6","5","4","3"};
int index = 0;
poker.put(index,"大王");
pokerIndex.add(index);
index++;
poker.put(index,"小王");
pokerIndex.add(index);
index++;
for (String s : color) {
for (String number : numbers) {
poker.put(index,s+number);
pokerIndex.add(index);
index++;
}
}
System.out.println("看牌:");
System.out.println(poker);
Collections.shuffle(pokerIndex);
//发牌
ArrayList<Integer> one = new ArrayList<>();
ArrayList<Integer> two = new ArrayList<>();
ArrayList<Integer> three = new ArrayList<>();
ArrayList<Integer> diapi = new ArrayList<>();
for (int i = 0; i < pokerIndex.size(); i++) {
Integer in = pokerIndex.get(i);
if(i >= 51){
diapi.add(in);
}else if (i % 3 == 0){
one.add(in);
}else if (i % 3 == 1) {
two.add(in);
}else if (i % 3 == 2) {
three.add(in);
}
}
Collections.sort(one);
Collections.sort(two);
Collections.sort(three);
Collections.sort(diapi);
//看牌
lookpoker("孙佳明",poker,one);
lookpoker("王天昊",poker,two);
lookpoker("董耘伯",poker,three);
lookpoker("底牌",poker,diapi);
}
private static void lookpoker(String name , HashMap<Integer,String> poker, ArrayList<Integer> pokerIndex) {
System.out.print(name + ": ");
for (Integer key : pokerIndex) {
String value = poker.get(key);
System.out.print(value + " ");
}
System.out.println();
}
}