public static void main (String...a){
//有一个枚举类,枚举4种花色
//有一个扑克类,PuKe,字段: int state,String size,HuaSe huase。toString重写。
Iterator<String>it=Stream.of("A","2","3","4","5","6","7","8","9","10","J","Q","K").iterator();
List<PuKe>list=IntStream.range(1,14).mapToObj(x->new PuKe().setSize(x). setState(it.next())).map(x->Stream.of(HuaSe.values()).map(y->x.setHuase(y)).collect(Collectors.toList())).flatMap(List::stream).collect(Collectors.toList());
list.add(new PuKe().setSize(14).setState("小王"));
list.add(new PuKe().setSize(15).setState("大王"));
Collections.shuffle(list);//洗牌
System.out.println(list);
//随机抽取两张牌比大小(玩法规则不变的情况下可以考虑为扑克类写compareTo方法)
Puke pk1=list.get((int)(Math.random()*list.size()));
Puke pk2=list.get((int)(Math.random()*list.size()));
int comp=pk1.getSize()-pk2.getSize();
System.out.printf("%s%s%s",pk1,comp==0?"=":comp>0?">":"<",pk2);
}
|