普歌-毅雁团队-(详细通俗易懂)Java 实现 斗地主 案例 实现有序

上次已经介绍了Java实现斗地主案例到手的牌无序斗地主到手牌无序的案例,这次分享Java实现斗地主到手牌有序的实现。

斗地主案例实现步骤

分析:

首先来梳理一下规则:
(1)准备牌阶段:斗地主总共54张牌,大王小王各一张(特殊对待),其他52张牌,分别4种花色,每种花色13张。四种花色分别为♥ ♦ ♠ ♣;每一种花色中的13张牌(由大到小)2 A K Q J 10 9 8 7 6 5 4 3,定义Map集合进行存储牌的索引和组装好的牌,循环遍历两个集合组装52张牌;如:♥ 7,♠ 8等;

(2)洗牌阶段:使用集合工具类Collections方法,其中static void shuffle(List<?> list)方法对牌进行随机打乱。

(3)发牌阶段:要求每一位玩家拥有17张牌,剩余三张作为底牌,一人一张轮流发牌:集合的索引(0-53)%3,定义4个集合,来存储3个玩家的牌和场上的底牌。索引%3,有三个值(0,1,2),0%3=0,1%3=1,2%3=2,3%3=0,就可以给三名玩家发牌了,当索引>=51时,改为发底牌.

(4)排序阶段:使用Collections中的方法,sort方法对集合中的元素进行排序

(5)看牌阶段:使用查表法,遍历一个集合,获取到另一个集合的key,通过key查找到value。

实现:

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class DouDiZhu {
public static void main(String[] args) {
    //1.准备牌
    //创建一个Map集合,存储牌的索引和组装好的牌
    HashMap<Integer, String> poker = new HashMap<>();
    //创建一个ArrayList集合,存储牌的索引
    ArrayList<Integer> pokerNumber = new ArrayList<>();
    //定义两个数组存储花色和牌的顺序
    String[] colors = {"♠", "♥", "♣", "♦"};
    String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
    //先把大王和小王存储到集合中
    //定义第一张牌的索引
    int index = 0;
    poker.put(index, "大王");
    pokerNumber.add(index);
    index++;
    poker.put(index, "小王");
    pokerNumber.add(index);
    index++;
    //循环嵌套遍历两个数组,花色和顺序,组装52张牌,存储到集合中
    for (String number : numbers) {
        for (String color : colors) {
            poker.put(index, color + number);
            pokerNumber.add(index);
            index++;
        }
    }
    //2.洗牌
    //使用Collections中的方法shuffle(List)方法,对poker的索引进行洗牌
    Collections.shuffle(pokerNumber);
    //3.发牌,定义四个集合,存储玩家牌的索引和底牌的索引
    ArrayList<Integer> player01 = new ArrayList<>();
    ArrayList<Integer> player02 = new ArrayList<>();
    ArrayList<Integer> player03 = new ArrayList<>();
    ArrayList<Integer> diPai = new ArrayList<>();
    //遍历索引ArrayList集合,获取每一张牌的索引
    for (int i = 0; i < pokerNumber.size(); i++) {
        Integer in = pokerNumber.get(i);
        //分出三张底牌
        if (i >= 51) {
            //给底牌发牌
            diPai.add(in);
        } else if (i % 3 == 0) {
            //给玩家1发牌
            player01.add(in);
        } else if (i % 3 == 1) {
            //给玩家2发牌
            player02.add(in);
        } else if (i % 3 == 2) {
            //给玩家3发牌
            player03.add(in);
        }
    }
    //4.排序,使用Collections中的方法sort(List),对玩家的牌进行排序
    Collections.sort(player01);
    Collections.sort(player02);
    Collections.sort(player03);
    //5.看牌,为了提高代码的复用性,定义一个方法
    lookPoker("玩家1",poker,player01);
    lookPoker("玩家2",poker,player02);
    lookPoker("玩家3",poker,player03);
    lookPoker("底牌",poker,diPai);


}

/*看牌的方法,应该传递姓名,Map集合和索引
  查表法:遍历玩家或底牌集合,获取牌的索引
  使用牌的索引去Map集合,找到对应的牌*/
public static void lookPoker(String name, HashMap<Integer, String> poker, ArrayList<Integer> list) {
    //输出玩家姓名
    System.out.print(name+": ");
    //遍历玩家或底牌集合,获取牌的索引
    for(Integer key:list){
        //通过牌的索引,通过Map集合get()方法找到牌
        String value = poker.get(key);
        //输出牌
        System.out.print(value+" ");
    }
    System.out.println();
  }
}

结果

玩家1: ♦2 ♦A ♣K ♦K ♠Q ♥10 ♣10 ♣9 ♥8 ♣8 ♦8 ♦6 ♠5 ♦5 ♥4 ♣3 ♦3 
玩家2: ♠2 ♠K ♥K ♥Q ♣Q ♦Q ♥J ♥9 ♦9 ♠7 ♣7 ♦7 ♣6 ♥5 ♣5 ♠3 ♥3 
玩家3: 大王 小王 ♥2 ♣2 ♠A ♥A ♣A ♠J ♣J ♦J ♠9 ♥7 ♠6 ♥6 ♠4 ♣4 ♦4 
底牌: ♦10 ♠8 ♠10 

感谢您的阅读,不足之处欢迎指正!

猜你喜欢

转载自blog.csdn.net/weixin_51749554/article/details/113773113