直接上代码
package com.ys.msjihe18;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/*
* 模拟斗地主发牌
*
*/
public class CollectionsTest {
public static void main(String[] args) {
//买牌
ArrayList<String> box = new ArrayList<String>();
//两个数组来配合出52张牌
String[] arr = {"黑桃","红桃","梅花","方块"};
String[] arr2 = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr2.length; j++) {
box.add(arr[i]+arr2[j]);
}
}
// System.out.println(box);
//大王小王
box.add("大王");
box.add("小王");
//洗牌
Collections.shuffle(box);
// System.out.println(box);
//发牌
//三个玩家
ArrayList<String> 杨帅 = new ArrayList<String>();
ArrayList<String> 龚仕蓉 = new ArrayList<String>();
ArrayList<String> 杨狗蛋 = new ArrayList<String>();
//留三张底牌
for (int i = 0; i < box.size()-3; i++) {
//取模选择给谁发牌
if(i%3 == 0) {
杨帅.add(box.get(i));
}
else if(i%3 == 1) {
龚仕蓉.add(box.get(i));
}
else if(i%3 == 2) {
杨狗蛋.add(box.get(i));
}
}
System.out.println("杨帅: "+杨帅);
System.out.println("龚仕蓉: "+龚仕蓉);
System.out.println("杨狗蛋: "+杨狗蛋);
System.out.println("底牌:");
System.out.println(box.get(box.size()-1));
System.out.println(box.get(box.size()-2));
System.out.println(box.get(box.size()-3));
}
}
运行结果
自己看吧~