思路
- 模拟斗地主洗牌和发牌
分析:
A:创建一个牌盒(ArrayLast)
B:装牌
C:洗牌
D:发牌
E:看牌
具体实现如下:
- A:创建一个牌盒
- B:装牌
//创建花色数组
String [] colors = {"♠", "♥", "♣", "♦"};
//创建点数数组
String [] numbers={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
//创建一个牌盒
ArrayList<String> arrayList = new ArrayList<>();
//将牌存入牌盒
for (int i = 0; i <colors.length ; i++) {
for (int j = 0; j <numbers.length ; j++) {
arrayList.add(colors[i].concat(numbers[j]));
}
}
arrayList.add("大王");
arrayList.add("小王");
- C:洗牌
//洗牌
Collections.shuffle(arrayList);
//System.out.println(arrayList);//呈现洗牌结果
- D:发牌
//发牌
ArrayList<String> John = new ArrayList<>();
ArrayList<String> Sunny = new ArrayList<>();
ArrayList<String> Tina = new ArrayList<>();
ArrayList<String> LastPoker = new ArrayList<>();
for (int x=0 ; x<arrayList.size();x++) {
if(x>=(arrayList.size()-3)){
LastPoker.add(arrayList.get(x));
}else if (x%3==0){
John.add(arrayList.get(x));
}else if (x%3==1){
Sunny.add(arrayList.get(x));
}else if (x%3==2){
Tina.add(arrayList.get(x));
}
}
- E:看牌(main中直接调用lookPoker方法)
//看牌方法
public static void lookPoker(String name ,ArrayList<String> list){
System.out.print(name+" 的牌是: ");
for (String string:list) {
System.out.print(string+" ");
}
System.out.println();
}
案例结果图: