Java版炸金花

Posted 活跃的咸鱼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java版炸金花相关的知识,希望对你有一定的参考价值。

过年在家和村里小伙伴天天炸金花,作为程序员肯定得用程序模拟一个。

运行结果如下:

源代码附上

import java.util.*;

public class FriedGoldenFlower 
    public static final List<String> POKE = new ArrayList<>(52);
    public static final String[] NUMBERS = "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A";
    public static final String[] COLORS = "♦", "♣", "♥", "♠";

    public static void main(String[] args) 
        System.out.println("==========欢乐炸金花==========");
        initPoke();
        Player[] players = initPlayer();
        licensing(players);
        comparePlayerPoke(players);
    

    // 初始化玩家
    private static Player[] initPlayer() 
        Player ly = new Player();
        ly.setName("老有");
        Player lq = new Player();
        lq.setName("老强");
        Player lj = new Player();
        lj.setName("老江");
        Player ll = new Player();
        ll.setName("老亮");
        Player wlb = new Player();
        wlb.setName("王老板");
        System.out.println("赌博的玩家有:" + "老有,老强,老江,老亮,王老板");
        return new Player[]ly, lq, lj, ll, wlb;
    

    // 初始化牌
    public static void initPoke() 
        System.out.println("==========开始初始化牌==========");
        for (String color : COLORS) 
            for (String number : NUMBERS) 
                POKE.add(color + number);
            
        
        System.out.println("初始化后的牌:"+POKE);
        System.out.println("==========开始洗牌==========");
        Collections.shuffle(POKE);
        System.out.println("洗完后的牌:"+POKE);
    

    // 得到单个牌的大小:即每张牌在数组NUMBERS的下标值
    public static int getPokeNumberSize(String poke) 
        checkPoke(poke);
        String number;
        if (poke.length() == 3) 
            number = poke.substring(1, 3);
         else 
            number = poke.substring(1, 2);
        
        for (int i = 0; i < NUMBERS.length; i++) 
            if (number.equals(NUMBERS[i])) 
                return i;
            
        
        return -1;
    

    // 检验poke是否合法
    private static void checkPoke(String poke) 
        if (poke == null) 
            throw new NullPointerException("poke为空");
        
        if (poke.length() < 2 || poke.length() > 3) 
            throw new IllegalArgumentException("poke长度不合法");
        
        String color = poke.substring(0, 1);
        // 判断花色是否正常
        for (String s : COLORS) 
            if (color.equals(s)) 
                return;
            
        
        throw new IllegalArgumentException("poke花色不合法");
    

    // 得到单个牌花色的大小:即每张牌的花色在数组COLORS的下标值
    public static int getPokeColorSize(String poke) 
        checkPoke(poke);
        String pokeColor = poke.substring(0, 1);
        for (int i = 0; i < COLORS.length; i++) 
            if (pokeColor.equals(COLORS[i])) 
                return i;
            
        
        return -1;
    

    // 得到牌型大小:即每种牌型在types数组中的下标
    public static int getPokTypeSize(String pokeType) 
        if (pokeType == null) 
            throw new NullPointerException("pokeType为空");
        
        String[] types = "散牌", "对子", "顺子", "金花", "同花顺", "豹子";
        for (int i = 0; i < types.length; i++) 
            if (pokeType.equals(types[i])) 
                return i;
            
        
        return -1;
    

    // 判断牌的类型,什么牌都不是就是散牌
    public static POKE_TYPE pokeType(List<String> poke) 
        checkPokeSize(poke);
        if (isPAIR(poke)) 
            return POKE_TYPE.PAIR;
         else if (isSTRAIGHT_GOLDEN_FLOWER(poke)) 
            return POKE_TYPE.STRAIGHT_GOLDEN_FLOWER;
         else if (isSTRAIGHT(poke)) 
            return POKE_TYPE.STRAIGHT;
         else if (isGOLDEN_FLOWER(poke)) 
            return POKE_TYPE.GOLDEN_FLOWER;
         else if (isBOMB(poke)) 
            return POKE_TYPE.BOMB;
         else 
            return POKE_TYPE.ZHA_PAI;
        
    

    // 是否是金花
    private static boolean isGOLDEN_FLOWER(List<String> poke) 
        String firstPokeColor = getPokeColor(poke.get(0));
        String secondPokeColor = getPokeColor(poke.get(1));
        String lastPokeColor = getPokeColor(poke.get(2));
        return firstPokeColor.equals(secondPokeColor) && secondPokeColor.equals(lastPokeColor);
    

    // 检验牌的大小是否是三张
    private static void checkPokeSize(List<String> poke) 
        if (poke.size() != 3) 
            throw new IllegalArgumentException("poke的长度必须是3");
        
    

    // 是否是顺子
    private static boolean isSTRAIGHT(List<String> poke) 
        // 排序3张牌
        sortPoke(poke);
        String firstPoke = poke.get(0);
        String secondPoke = poke.get(1);
        String lastPoke = poke.get(2);

        int i1 = getPokeNumberSize(firstPoke);
        int i2 = getPokeNumberSize(secondPoke);
        int i3 = getPokeNumberSize(lastPoke);

        firstPoke = getPokeNumber(firstPoke);
        secondPoke = getPokeNumber(secondPoke);
        lastPoke = getPokeNumber(lastPoke);
        if (i2 - 1 == i1 && i2 + 1 == i3) 
            return true;
         else return firstPoke.equals("2") && secondPoke.equals("3") && lastPoke.equals("A");
    

    // 理牌
    private static void sortPoke(List<String> poke) 
        checkPokeSize(poke);
        // 比较大小
        poke.sort(FriedGoldenFlower::comparePokeNumber);
    

    // 比较两张牌的大小
    public static int comparePokeNumber(String poke1, String poke2) 
        // 比较大小
        int size1 = getPokeNumberSize(poke1);
        int size2 = getPokeNumberSize(poke2);
        if (size1 > size2) 
            return size1;
         else if (size1 == size2) 
            // 大小相等返回0
            return 0;
         else 
            return size1 - size2;
        
    

    // 比较两张牌花色的大小
    public static int comparePokeColor(String poke1, String poke2) 
        int color1 = getPokeColorSize(poke1);
        int color2 = getPokeColorSize(poke2);
        if (color1 > color2) 
            return color1;
         else 
            return color1 - color2;
        
    

    // 得到3张牌中最大的那张
    public static String getMaxPoke(List<String> poke) 
        checkPokeSize(poke);
        sortPoke(poke);
        return poke.get(2);
    

    // 得到3张牌中最小的那张
    public static String getMinPoke(List<String> poke) 
        checkPokeSize(poke);
        sortPoke(poke);
        return poke.get(0);
    

    // 得到3张牌中间的那张
    public static String getMidPoke(List<String> poke) 
        checkPokeSize(poke);
        sortPoke(poke);
        return poke.get(1);
    

    // 比较两个杂牌的大小
    public static int compareZHA_PAI(List<String> poke1, List<String> poke2) 
        String maxPoke1 = getMaxPoke(poke1);
        String maxPoke2 = getMaxPoke(poke2);
        int cmp1 = comparePokeNumber(maxPoke1, maxPoke2);
        if (cmp1 > 0) 
            return 1;
         else if (cmp1 == 0) 
            String midPoke1 = getMidPoke(poke1);
            String midPoke2 = getMidPoke(poke2);
            int cmp2 = comparePokeNumber(midPoke1, midPoke2);
            if (cmp2 > 0) 
                return 1;
             else if (cmp2 == 0) 
                String minPoke1 = getMinPoke(poke1);
                String minPoke2 = getMinPoke(poke2);
                int cmp3 = comparePokeNumber(minPoke1, minPoke2);
                if (cmp3 > 0) 
                    return 1;
                 else if (cmp3 == 0) 
                    // 三张牌一样大,比较最大牌的花色
                    return comparePokeColor(maxPoke1, maxPoke2);
                 else 
                    return -1;
                
             else 
                return -1;
            
         else 
            return -1;
        
    

    // 比较两个炸弹大小
    public static int compareBOMB(List<String> poke1, List<String> poke2) 
        String maxPoke1 = getMaxPoke(poke1);
        String maxPoke2 = getMaxPoke(poke2);
        return comparePokeNumber(maxPoke1, maxPoke2);
    

    // 比较两个对子大小
    public static int comparePAIR(List<String> poke1, List<String> poke2) 
        sortPoke(poke1);
        sortPoke(poke2);
        String pair1 = poke1.get(1);
        String pairOther1 = poke1.get(1).equals(poke1.get(0)) ? poke1.get(2) : poke1.get(0);
        String pair2 = poke2.get(1);
        String pairOther2 = poke2.get(1).equals(poke2.get(0)) ? poke2.get(2) : poke2.get(0);
        int cmp = comparePokeNumber(pair1, pair2);
        if (cmp > 0) 
            return 1;
         else if (cmp == 0) 
            int cmp2 = comparePokeNumber(pairOther1, pairOther2);
            if (cmp2 > 0) 
                return 1;
             else if (cmp2 == 0) 
                // 比较对子的花色
                return comparePokeColor(pair1, pair2);
             else 
                return -1;
            
         else 
            return -1;
        
    

    // 比较两个顺子大小
    public static int compareSTRAIGHT(List<String> poke1以上是关于Java版炸金花的主要内容,如果未能解决你的问题,请参考以下文章

Java版炸金花

关于扎金花php版

斗地主——扎金花——3DMark

炸金花的JS实现从0开始之 -------现在什么都不会

python编写扎金花小程序

诈金花的概率