java用集合实现斗地主有序排列
Posted yeqingyun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java用集合实现斗地主有序排列相关的知识,希望对你有一定的参考价值。
import java.util.*; public class NewDouDiZhu { public static void main(String[] args) { HashMap<Integer,String> poker= new HashMap<>(); //1.组装牌 ArrayList<Integer> suoyin = new ArrayList<>(); List<String> color = new ArrayList<>(); color.add("红桃"); color.add("方块"); color.add("黑桃"); color.add("梅花"); String[] number = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"}; int index = 0; for (String n:number){ for (String c:color){ poker.put(index,c+n); suoyin.add(index); index++; } } poker.put(index++,"大王"); poker.put(index++,"小王"); System.out.println(poker); System.out.println(suoyin); //2.洗牌 Collections.shuffle(suoyin); System.out.println(suoyin); //3.发牌 ArrayList<Integer> player1 = new ArrayList<>(); ArrayList<Integer> player2 = new ArrayList<>(); ArrayList<Integer> player3 = new ArrayList<>(); ArrayList<Integer> dipai = new ArrayList<>(); for (int n =0;n<suoyin.size();n++) { Integer in = suoyin.get(n); if (n>=50){ dipai.add(in); }else if (n%3==0){ player1.add(in); }else if (n%3==1){ player2.add(in); }else if(n%3==2){ player3.add(in); } } Collections.sort(player1); Collections.sort(player2); Collections.sort(player3); System.out.println("底牌是: "+dipai); System.out.println(player1); System.out.println(player3); System.out.println(player2); //看牌 for (Integer s:player1){ String pai =poker.get(s); System.out.print(pai+" "); } } }
以上是关于java用集合实现斗地主有序排列的主要内容,如果未能解决你的问题,请参考以下文章
阶段1 语言基础+高级_1-3-Java语言高级_04-集合_03 斗地主案例(单列)_2_斗地主案例的代码实现
Java学习笔记34(集合框架八:综合案例:模拟斗地主的洗牌发牌)