在java中的三种扑克手

时间:2014-10-28 20:17:19

标签: java poker

所以我正在进行一场扑克游戏并完成了我的直接和双手但不知道从哪里开始我的3种。任何帮助将在这里被apprciated是我的扑克手包。如果我能完成三手牌的话,我终于可以继续前进,所以这手牌让我完全卡住了。

package edu.rcc.hand;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import edu.rcc.deck.Card;

public class Hand implements Comparable<Object> {

//Card[] theCards = new Card[10];
private ArrayList<Card> theCards = new ArrayList<Card>();

public Hand(ArrayList<Card> theCards) {
    this.theCards = theCards;
}

public String toString() {
    String s = "";
    for (Card c : theCards) {
        s += c.toString() + ", ";
    }
    return s;
}

private ArrayList<Card> getHandInNumericalOrder() {
    ArrayList<Card> sorted = new ArrayList<Card>();
    sorted.addAll(this.theCards);
    Collections.sort(sorted, new Comparator<Card>() {

        @Override
        public int compare(Card o1, Card o2) {
            Integer o1Value = o1.getNumericalValue();
            Integer o2Value = o2.getNumericalValue();
            return o1Value.compareTo(o2Value);

        }

    });
    return sorted;
}

/**
 * a method to get cards that have the same value
 */
private boolean isPair(ArrayList<Card> sortedCards) {

    Card previousCard = null;
    for (Card c : sortedCards) {

        if (previousCard != null && previousCard.isSameFace(c)) {
            return true;
        } else {
            previousCard = c;
        }


    }
    return false;
}


private boolean ThreeOfKind(ArrayList<Card> sortedCards)
 {

    Card previousCard = null;
    for (Card c : sortedCards) {


        if (previousCard != null && previousCard.isSameFace(c) ) {
            return true;
        } else {
            previousCard = c;
        }


    }
    return false;
}

/**
 * if the sum of the integer array is equal to the following:
 * 1 - 
 * @return
 */
private ArrayList<Integer> numberOfSameCards() {
    ArrayList<Card> sortedCards = this.getHandInNumericalOrder();

    ArrayList<Integer> cardCounts = new ArrayList<Integer>();
    int numbSame = 1;
    for (int i = 0; i < sortedCards.size() - 1; ++i ) {
        if (sortedCards.get(i).isSameFace(sortedCards.get(i+1))) {
            ++numbSame;
        } else {
            for (int j = 0; j < numbSame; ++j) {
                cardCounts.add(numbSame);
            }
            numbSame = 1;
        }
    }
    for (int i = 0; i < numbSame; ++i) {
        cardCounts.add(numbSame);
    }


    return cardCounts;
}

/**
 * 5 - high card || strait || strait flush || flush
 * 7 - pair
 * 9 - 2 pair
 * 11 - 3 of a kind
 * 13 - full house
 * 17 - four of a kind
 * @param inputArray
 * @return
 */
private int getSum(ArrayList<Integer> inputArray) {
    int sum = 0;
    for (int i : inputArray) {
        sum += i;
    }
    return sum;
}

/**
 * 0 - high card
 * 1 - pair
 * 2 - 2 pair
 * 3 - three of a kind
 * 4 - strait
 * 5 - flush
 * 6 - full house
 * 7 - four of a kind
 * 8 - strait flush
 * @return
 */
private int getRank() {
    ArrayList<Card> sorted = this.getHandInNumericalOrder();
    int rawSum = getSum(numberOfSameCards());
    switch (rawSum) {
        case 7:
            return 1;
        case 9:
            return 2;
        case 11:
            return 3;
        case 13:
            return 6;
        case 17:
            return 7;
        default:
            //TODO CHECK FOR ALL OTHER HAND TYPES
    }
    boolean isFlush = isFlush(sorted);
    boolean isStrait = isStrait(sorted);
    if (isFlush && isStrait) {
        return 8;
    } else if (isFlush) {
        return 5;
    } else if (isStrait) {
        return 4;
    } else {
        return 0;
    }
}

private boolean isFlush(ArrayList<Card> theCards) {

    for (int i = 0; i < theCards.size() - 1; i++) {
        if (!theCards.get(i).isSameSuit(theCards.get(i + 1))) {
            return false;
        }
    }
    return true;

}

/**
 * 
 * @param sortedCards - all have unique faces
 * @return
 */
private boolean isStrait(ArrayList<Card> sortedCards) {

    for (int i = 0; i < theCards.size(); i++) {

        int current = theCards.get(i).getNumericalValue();

        int next = theCards.get(i + 1).getNumericalValue();

        if (current + 1 == next || 
                (current == 5 && next == 14)) {
            continue;
        } else {
            return false;
        }

    }
    return true;

}

/**
 * 0 -> equal
 * 1 -> this > o
 * -1 -> this < o
 */
@Override
public int compareTo(Object o) {
    if (!(o instanceof Hand)) {
        return 1;
    }
    Hand other = (Hand)o;


    boolean isThisPair = other.isPair(other.getHandInNumericalOrder());
    boolean isThisPair2 = this.isPair(this.getHandInNumericalOrder());
    boolean isStrait = this.isStrait(this.getHandInNumericalOrder());
    boolean isStrait2 = other.isStrait(other.getHandInNumericalOrder());
    boolean isFlush = this.isFlush(this.getHandInNumericalOrder());
    boolean isFlush2 = other.isFlush(other.getHandInNumericalOrder());
    boolean ThreeOfKind = this.isFlush(this.getHandInNumericalOrder());
    boolean ThreeOfKind2 = other.isFlush(other.getHandInNumericalOrder());

    ArrayList<Card> otherNumerical = other.getHandInNumericalOrder();
    ArrayList<Card> thisNumerical = this.getHandInNumericalOrder();


    System.out.println(thisNumerical);
    System.out.println(otherNumerical);


    if(isThisPair2 == true)
    {
        System.out.println("The first hand is a pair");
    }
    if(isThisPair == true){
        System.out.println("The second hand has a pair");
    }
    if (isStrait == true){
        System.out.println("The first hand has a straight");

    }
    if (ThreeOfKind == true){
        System.out.println("The sfirst hand has a straight");

    }
    if (ThreeOfKind2 == true){
        System.out.println("The second hand has a straight");

    }


    System.out.println(isThisPair2);
    System.out.println(isThisPair);

    System.out.println(isStrait);
    System.out.println(isFlush);





    //call the card hierarchy class
    // TODO Auto-generated method stub
    return 0;
}



}

3 个答案:

答案 0 :(得分:2)

使用HashMap计算给定卡片等级出现在手中的次数。通过你的手迭代。如果地图中不存在卡的等级,请将其插入值1.如果地图中已存在卡的值,则将该值增加1.然后,任何值为3的键值对都会跳闸。通过这个概念来寻找成对,满船,四边形,这很容易。

有这个概念,现在实现代码。

答案 1 :(得分:0)

查看你的代码,我建议你使用for循环而不是增强的for循环来迭代它。事实上,如果您已经过了Iterator,我会建议您使用Iterator,但如果没有,您可以使用Collections频率方法计算某个集合中出现的次数。您可以使用ArrayList中的值创建一个临时集,它应该删除重复项,并且您可以使用具有非重复集的For Each循环来查找原始ArrayList中的重复数量。 / p>

答案 2 :(得分:0)

忽略不同的变量。这就是我在项目中的表现。

boolean hasThreeOfAKind = false;
        int[] value = new int[cards.length];
        for(int i =0; i<cards.length; i++){
            Card myCard = cards[i];
            value[i] = myCard.getValue(); //an array that stores the values of the cards passed in
        }
        int counter = 0;
        for(int i =0; i<cards.length; i++){
            for(int j =i+1; j<cards.length; j++){
                if(value[i] == value[j]){
                    counter++; //if a pair is found increment the counter
                }
                if(j == cards.length-1 && counter!=2){ //if the array of values has been looped
                                                       //through and a second "pair" of the same 
                                                       //value hasn't been found.
                    counter = 0; //start again
                }
//if the array has been looped through and 3 cards of the same value has been found
                else if(j== cards.length-1 && counter >= 2){
                    hasThreeOfAKind = true;
                }
            }
        }

        return hasThreeOfAKind;
相关问题