Don't miss the Java implementation of simple landlord (see the card sorting)

The examples in this article share the specific code for implementing Simple Landlord in Java for your reference. The specific content is as follows

the first method

 /**
 * @param args
 */
 /**
 * 项目编码格式:
 * 1.GBK:只支持简体中文
 * 2.GB2312:支持简体和繁体
 * 3.UTF-8:国际通用的编码格式
 * 
 * 模拟实现斗地主发牌的过程实现步骤
 * 1.准备一副扑克牌
 * a.定义一个Map集合用来存放54张扑克 map的key(下标)对应的是map的值(扑克)
 * b.定义一个list集合 用来保存map集合的key(map集合的key就对应map集合的值)
 * c.定义一个String类型的数组 用来构建牌的花色[♥,♠,♦,♣]
 * d.定义一个String的数组 用来保存扑克具体的值[3,4,5,6,7,8,9,10,J,Q,K,A,2]
 * e.把大王和小王添加到Map集合中
 * 2.洗牌
 * 通过集合的一个操作类Collections提供的一个相关方法实现洗牌的操作
 * 3.发牌的过程
 * a.定义三个玩家和一个接收底牌的集合容器
 * b.遍历ArrayList集合中 如何实现每个玩家轮流拿牌
 * 【J,Q,9,10,2,A........Q】
 * 【0,1,2,3,4,5.........53】
 * 发牌思路分析:
 * 如果当前的下标大于等于51 剩下的牌留作底牌
 * 可以使用下标对3求余数 0%3 = 0 把J给玩家1 1%3 = 1 把Q给玩家2 2%3 = 2 把9给玩家3 3%3 =0 把10给玩家1 4%3 = 1
 * 4.排序
 * 5.看牌
 * 实现每个玩家的牌输出展现
 * 
 */
 
 /*
 * 思路总结:
 * 根据下标排序,所以用map,key就是下标,对应值就是扑克(花色+数)
 * 洗牌的shuffle,排序的sort方法只能对list管用
 * 所以用list集合盛放key,对key洗牌,然后对key排序
 * 用key去map里去对应的值
 * */
 public static void main(String[] args) {
 
 Map<Integer,String> poker = new HashMap<Integer,String>();
 List<Integer> keys = new ArrayList<Integer>();
 
 String[] colors = {"♠","♥","♦","♣"};
 String[] values = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
 
 int index = 0;
 for(String v : values){ //外层循环数,内层循环花色
 for(String c : colors){
 poker.put(index,c+v); //向map里添加key和值
 keys.add(index); //将key添加到list里,便于排序
 index++;
 }
 }
 poker.put(index, "小王");
 keys.add(index);
 index++;
 poker.put(index,"大王");
 keys.add(index);
 
 
 //洗牌
 Collections.shuffle(keys);
 
 //分牌
 List<Integer> player01 = new ArrayList<Integer>(); //创建玩家和底牌集合,专门盛key,根据key去map里去值
 List<Integer> player02 = new ArrayList<Integer>();
 List<Integer> player03 = new ArrayList<Integer>();
 List<Integer> dipai = new ArrayList<Integer>();
 
 for(int i=0; i<keys.size(); i++){ //循环遍历盛key的list集合
 if(i>=51){
 dipai.add(keys.get(i));
 }else if(i%3==0){
 player01.add(keys.get(i));
 }else if(i%3==1){
 player02.add(keys.get(i));
 }else if(i%3==2){
 player03.add(keys.get(i));
 }
 }
 
 //针对玩家和底牌排序
 player01.addAll(dipai);
 Collections.sort(player01);
 Collections.sort(player02);
 Collections.sort(player03);
 Collections.sort(dipai);
 
 
 System.out.println("玩家一(地主):");
 for(Integer key : player01){
 System.out.print(poker.get(key)+" ");
 }
 System.out.println("\n玩家二:");
 for(Integer key : player02){
 System.out.print(poker.get(key)+" ");
 }
 System.out.println("\n玩家三:");
 for(Integer key : player03){
 System.out.print(poker.get(key)+" ");
 }
 System.out.println("\n底牌:");
 for(Integer key : dipai){
 System.out.print(poker.get(key)+" ");
 }
 }

The second method

public static void main(String[] args) {
 
 //保存编号和扑克牌的对应关系
 HashMap<Integer,String> poker = new HashMap<Integer,String>();
 //保存扑克牌的编号
 List<Integer> list = new ArrayList<Integer>();
 
 String[] colors = {"♥","♠","♦","♣"};
 String[] values = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
 
 //把这两个字符串放进HashMap集合中(拼接一起 花色+牌号)同时给每个放进去牌进行编码0--52并存储
 int count = 0;
 for(String v : values){
 for(String color : colors){
 //拼接
 String pk = color.concat(v);
 //把编码和牌存储到HashMap中
 poker.put(count, pk);   
 //把编码存到ArrayList中
 list.add(count);   
 count ++;
 }
 }
 
 poker.put(count, "小王");
 list.add(count);
 count++;
 
 poker.put(count, "大王");
 list.add(count);
 
// for(Integer i :list){
// System.out.print(i+" ");
// }
 
 Collections.shuffle(list);
 
 
 TreeSet<Integer> player01 = new TreeSet<Integer>();
 TreeSet<Integer> player02 = new TreeSet<Integer>();
 TreeSet<Integer> player03 = new TreeSet<Integer>();
  TreeSet<Integer> dipai = new TreeSet<Integer>();
 
  for(int i=0; i<list.size(); i++){
 if(i>=51){
 dipai.add(list.get(i));
 }
 else if(i%3 == 0){
 player01.add(list.get(i));
 }
 else if(i%3 == 1){
 player02.add(list.get(i));
 }
 else if(i%3 == 2){
 player03.add(list.get(i));
 }
 }
  
  
  mingpai("玩家一",player01,poker);
  mingpai("玩家二",player02,poker);
  mingpai("玩家三",player03,poker);
  mingpai("底牌",dipai,poker);
 
 }
 
 //参数列表:String name(玩家名字) ; TreeSet<Integer> i(牌的编号) ; HashMap<Integer,String> pai(牌)
 public static void mingpai(String name,TreeSet<Integer> i,HashMap<Integer,String> pai){
 List<Integer> li = new ArrayList<Integer>();
 System.out.println(name+"的牌是:");
 for(Integer key : i){
 //TreeSet的值就是HashMap的键,所以可以得到对应的值,也就是牌
 String result = pai.get(key);
 //输出看到的牌
 System.out.print(result+" ");
 }

 System.out.println(" ");
 
}

The above is the whole content of this article, I hope it will be helpful to everyone's study, and I hope everyone will support you

The latest high-frequency interview questions collected in 2021 (all organized into documents), there are a lot of dry goods, including mysql, netty, spring, thread, spring cloud, jvm, source code, algorithm and other detailed explanations. There are also detailed learning plans and interviews. Questions, etc., friends who need to obtain these content, please add Q Junyang: 547998459

Guess you like

Origin blog.csdn.net/p1830095583/article/details/114394040