获得所有可能的5张扑克牌,获得7张牌

时间:2015-11-22 20:48:32

标签: java poker

我正试图找到21张5张牌的扑克牌,可以在5张牌和2张牌的情况下形成牌。这是我到目前为止所做的:

public String[] joinArrays(String[] first, String[] second) {
        String[] result = new String[first.length + second.length];
        for (int i = 0; i < first.length; i++) {
            result[i] = first[i];
        }
        for (int i = 0; i < second.length; i++) {   
            result[i + first.length] = second[i]; 
        }
        return result;
    }

    public String[][] getAllPossibleHands(String[] board, String[] hand){
        String[] allCards = joinArrays(board, hand);
        String[][] allHands = new String[21][5];
        ...
    }

有关从何处出发的任何提示?我有一个包含7个元素的数组,想要从这些元素中创建7C5(21)5元素数组。

谢谢!

1 个答案:

答案 0 :(得分:3)

每只手选择7张卡中的两张不使用。添加所有其他

int cardsSelected = 0;
int hand = 0;
// select first card not to be in the hand
for(int firstCard = 0; firstCard < 7; firstCard++){
    // select first card not to be in the hand
    for(int secondCard = firstCard + 1; secondCard < 7; secondCard++){
        // every card that is not the first or second will added to the hand
        for(int i = 0; i < 7; i++){
            if(i != firstCard && i != secondCard){
                allHands[hand][cardsSelected++] = allCards[i];
            }
        }
        // next hand
        cardsSelected = 0;
        hand++;
    }
}