Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2014-05-22代码游戏 #34

Open
leonar opened this issue May 22, 2014 · 2 comments
Open

2014-05-22代码游戏 #34

leonar opened this issue May 22, 2014 · 2 comments

Comments

@leonar
Copy link

leonar commented May 22, 2014

梭哈游戏
要求:给定两副牌型(每副5张),比较大小
1.不考虑花色
数字比较:A>K>Q>J>10>9>8 >7>6>5>4>3>2
牌型比较:四条>三条加一对>顺子>三条>二对>单对>散牌

2.考虑花色
牌型比较:同花顺>四条>三条加一对>同花>顺子>三条>二对>单对>散牌。

@jcm872000
Copy link

定义了一些java对象。不好放全部代码了。就贴比较重要的算法吧:判断牌型+牌型之间比较(含同牌型最大牌面方色比较)

    public static int numNotZero(int[] array) {
        int j = 0;
        for (int i : array) {
            if (i > 0) {
                j++;
            }
        }
        return j;
    }

    public static int maxNum(int[] array) {
        int max = 0;
        for (int i : array) {
            if (i > max) {
                max = i;
            }
        }
        return max;
    }

    public static int[] record(Card[] cards) {
        int[] array = new int[13];
        for (Card c : cards) {
            array[c.getNumber() - 2]++;
        }
        return array;
    }
    public static int whichType(Card[] cards) {
        int[] array = record(cards);
        int type = 0;
        int num = numNotZero(array);
        int max = maxNum(array);
        if (num == 2) {
            if (max == 4) {
                type = Card.Fullhouse;
            } else if (max == 3) {
                type = Card.ThreeWithakind;
            }
        } else if (num == 3) {
            if (max == 3) {
                type = Card.ThreeWithCard;
            } else if (max == 2) {
                type = Card.TwoPair;
            }
        } else if (num == 4) {
            if (max == 2) {
                type = Card.OnePair;
            }
        } else if (num == 5) {
            boolean isStraight = isStraight(array);
            boolean isFullhouse = isFullhouse(cards);
            if (isStraight && isFullhouse) {
                type = Card.StraightFlush;
            } else if (isStraight) {
                type = Card.Straight;
            } else if (isFullhouse) {
                type = Card.Fullhouse;
            } else {
                type = Card.Zilch;
            }
        }
        return type;
    }
    public static boolean compareBig(Card[] cardsA, Card[] cardsB) {
        int typeA = whichType(cardsA);
        int typeB = whichType(cardsB);
        boolean isBig = true;
        if (typeA < typeB) {
            return true;
        } else if (typeA > typeB) {
            return false;
        } else {
            sort(cardsA);
            sort(cardsB);
            if (Card.StraightFlush == typeA) {
                // 都是同花顺
                isBig = compareNumAndColor(cardsA[0], cardsB[0]);
            } else if (Card.FourofaKind == typeA) {
                // 都是四条
                isBig = compareNumAndColor(cardsA[0], cardsB[0]);
            } else if (Card.Fullhouse == typeA) {
                // 都是同花
                isBig = compareColorAndNum(cardsA[0], cardsB[0]);
            } else if (Card.Straight == typeA) {
                // 都是顺子
                isBig = compareNumAndColor(cardsA[0], cardsB[0]);
            } else if (Card.ThreeWithakind == typeA) {
                // 都是三条带一对
                isBig = compareNumAndColor(cardsA[2], cardsB[2]);
            } else if (Card.ThreeWithCard == typeA) {
                // 都是三条带散牌
                isBig = compareNumAndColor(cardsA[2], cardsB[2]);
            } else if (Card.TwoPair == typeA) {
                // 都是两对
                int tA = twoPairType(cardsA);
                int tB = twoPairType(cardsB);
                Card tmp1, tmp2, tmp3, tmp4;
                if (tA == 2) {
                    tmp1 = cardsA[0];
                    tmp2 = cardsA[3];
                } else if (tA == 1) {
                    tmp1 = cardsA[0];
                    tmp2 = cardsA[2];
                } else {
                    tmp1 = cardsA[1];
                    tmp2 = cardsA[3];
                }
                if (tB == 2) {
                    tmp3 = cardsB[0];
                    tmp4 = cardsB[3];
                } else if (tB == 1) {
                    tmp3 = cardsB[0];
                    tmp4 = cardsB[2];
                } else {
                    tmp3 = cardsB[1];
                    tmp4 = cardsB[3];
                }
                isBig = comparePairNum(tmp1, tmp2, tmp3, tmp4);

            } else if (Card.OnePair == typeA) {
                // 都是一对
                isBig = compareNumAndColor(cardsA[onePairType(cardsA)], cardsB[onePairType(cardsB)]);
            } else {
                // 散牌
                isBig = compareNumAndColor(cardsA[0], cardsB[0]);
            }
        }
        return isBig;
    }

@leonar
Copy link
Author

leonar commented May 29, 2014

统计花色和牌型,每种牌计算数量

public class soha {

static byte[] handRank(String[] hand) {

    byte[] rank = new byte[6];//rank[0]牌型,rank[1]牌型最大的数字
    byte[] cCount = new byte[15];//cCount[2]开始每种牌数量
    boolean[] isUsed = new boolean[15];//每种牌是否有
    byte sCount = 0;//花色数量
    byte sCheck = 0;

    for (int i = 0; i < hand.length; ++i) {
        char s = hand[i].charAt(1);
        String v = hand[i].substring(0, 1);
        if (s == 'S' && (sCheck & 1) == 0) {//Spade 黑桃
            sCount++;
            sCheck += 1;
        } else if (s == 'C' && (sCheck & 2) == 0) {//Club 梅花
            sCount++;
            sCheck += 2;
        } else if (s == 'H' && (sCheck & 4) == 0) {//Heart 红桃
            sCount++;
            sCheck += 4;
        } else if (s == 'D' && (sCheck & 8) == 0) {//Diamond 方片
            sCount++;
            sCheck += 8;
        }

        if (v.equals("T")) {//T=10
            cCount[10]++;
        } else if (v.equals("J")) {
            cCount[11]++;
        } else if (v.equals("Q")) {
            cCount[12]++;
        } else if (v.equals("K")) {
            cCount[13]++;
        } else if (v.equals("A")) {
            cCount[14]++;
        } else {
            cCount[Byte.parseByte(v)]++;
        }
    }

    rank[0] = 1; // 普通牌
    for (byte i = 14; i >= 0; --i) 
    {
        if (cCount[i] == 4) { 
            rank[0] = 8;// 4条
            rank[1] = i;
            isUsed[i] = true;
            break;
        }
        if (cCount[i] == 3) { 
            rank[0] = 4;// 3条
            rank[1] = i;
            isUsed[i] = true;
            for (byte j = (byte) (i - 1); j >= 0; --j) { 
                if (cCount[j] == 2) {
                    rank[0] = 7;// 3条加一对
                    rank[2] = j;
                    isUsed[j] = true;
                    break;
                }
            }
            break;
        }
        if (cCount[i] == 2) { 
            rank[0] = 2;// 一对
            rank[1] = i;
            isUsed[i] = true;
            for (byte j = (byte) (i - 1); j >= 0; --j) {
                if (cCount[j] == 3) { 
                    rank[0] = 7;// 3条加一对
                    rank[1] = j;
                    rank[2] = i;
                    isUsed[j] = true;
                    break;
                }
                if (cCount[j] == 2) { 
                    rank[0] = 3;// 两对
                    rank[2] = j;
                    isUsed[j] = true;
                    break;
                }
            }
            break;
        }
        if (i > 5 && cCount[i] == 1 && cCount[i - 1] == 1 && cCount[i - 2] == 1 && cCount[i - 3] == 1 && cCount[i - 4] == 1) { 
            rank[0] = 5;// 顺子
            rank[1] = i;
            isUsed[i] = true;
            break;
        }
    }
    if (rank[0] < 5 && sCount == 1) {
        rank[0] = 6;// 同花
    } 
    else if (rank[0] == 5 && sCount == 1) {
        rank[0] = 9;// 同花顺
    } 

    byte cValue = 14;
    for (byte i = 1; i < rank.length; ++i) {
        if (rank[i] > 1) {
            continue;
        }
        while (cValue > 1) {
            if (!isUsed[cValue] && cCount[cValue] > 0) {
                rank[i] = cValue--;
                break;
            }
            cValue--;
        }
    }

    return rank;
}

public static void main(String[] args) {
    String[] card1 = { "4H", "5C", "6S", "7D", "8C" };
    String[] card2 = { "3D", "4S", "5H", "6C", "2D" };
    byte[] player1 = handRank(card1);
    byte[] player2 = handRank(card2);

    for (byte i = 0; i < player1.length; ++i) {
        if (player1[i] > player2[i]) {
            System.out.println("player1 win!");
            break;
        }
        if (player1[i] < player2[i]) {
            System.out.println("player2 win!");
            break;
        }
    }
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants