package com.st.text;
import java.util.ArrayList;
import java.util.Collections;
/**
- 1.一副扑克牌有54张牌,扑克的图形有梅花,方片,黑桃,红心
数字有 A 1,2,3.......J Q K
现在有三个人打牌,请给每人随机分发17张牌,并查看剩下的三张底牌
将54张牌存入到集合(ArrayList)中
洗牌(Collections.shuffle(list); )
向三个人依次发牌
查看三个人的牌,以及最后的三张底牌
-- 提示代码
Collections.shuffle(list); // 对集合中的元素随机排列
- @author 30693
*/
public class Text {
public static void main(String[] args) {
String [] a={"♠","♣","♥","♦"};
String [] b={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
ArrayList
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < b.length; j++) {//concat() 方法用于将指定的字符串参数连接到字符串上。
aList.add(a[i].concat(b[j])); //将a和b数组组合的元素存到集合中
}
}
aList.add("小王");
aList.add("大王");
System.out.println("按照顺序排列的54号牌:"+aList);
System.out.println("54张牌:"+aList.size()); //54张牌
Collections.shuffle(aList); //将集合中的数组随机排列
System.out.println("打乱顺序排列的54号牌:"+aList);
ArrayList<String> play01=new ArrayList<>(); //模拟abc三位玩家的手牌
ArrayList<String> play02=new ArrayList<>();
ArrayList<String> play03=new ArrayList<>();
ArrayList<String> end=new ArrayList<>(); //用来 存放三张底牌的集合
for (int i = 0; i < aList.size(); i++) { //分牌
if (i>=51) { //索引位置51,52,53正好为底牌
end.add(aList.get(i));
} else if (i%3==0) {
play01.add(aList.get(i));
}else if (i%3==1) {
play02.add(aList.get(i));
}else if (i%3==2) {
play03.add(aList.get(i));
}
}
//查看三个玩家的牌和底牌
System.out.println("玩家a的手牌"+play01);
System.out.println("玩家b的手牌"+play02);
System.out.println("玩家c的手牌"+play03);
System.out.println("地主牌:"+end);
}
}