我正在构建一个程序来运行5回合5张抽奖。我有一些代码仍主要在我的PokerHandValue类上工作。

PokerHandValue类正在评估双手而不是单独评估的唯一问题。我认为问题所在的PokerHandClass由于我测试各种组件而过分膨胀。如何为程序添加一些代码,使其仅将一只手评估为一个对象?

我的主班:

public static void main(String[] args) {
    Cards deck = new Cards();

    //setting loop for playing 5 hands
    int playedHands = 5;

    do {
        //creating the deck and player hands
        List<String> playableDeck = new ArrayList<>(deck.ShuffleDeck());
        List<String> playerHand = new ArrayList<>(playableDeck.subList(0, 5));
        for (int i = 0; i < 5; i++) {
            playableDeck.remove(i);
        }
        List<String> dealerHand = new ArrayList<>(playableDeck.subList(6, 11));
        for (int j = 0; j < 5; j++) {
            playableDeck.remove(j);
        }

        //assessing the value of hand dealt to player
        PokerHandValue playerValue = new PokerHandValue(playerHand);
        int pValue = playerValue.getValue();
        String pReason = playerValue.getResult();
        int playerHigh = playerValue.getHighCard();

        //assessing the value of hand dealt to player
        PokerHandValue dealerValue = new PokerHandValue(dealerHand);
        int dValue = dealerValue.getValue();
        String dReason = dealerValue.getResult();
        int dealerHigh = dealerValue.getHighCard();
        //displaying the output and value and results of each hands
        System.out.print("Black: " + playerHand.toString().replace("[", "").replace("]", ""));
        System.out.println(" and White: " + dealerHand.toString().replace("[", "").replace("]", ""));
        //determining the winner
        if (dValue < pValue) {
            System.out.println("White wins. - with a " + pReason + "\n");
        } else if (pValue < dValue) {
            System.out.println("Black wins. - with a " + dReason + "\n");
        } else if (playerHigh == dealerHigh) {
            if (playerHigh < dealerHigh) {
                String stringDHighCard = determineFace(Integer.toString(dealerHigh));
                System.out.println("Black wins. with a " + stringDHighCard + "\n");
            } else {
                String stringPHighCard = determineFace(Integer.toString(playerHigh));
                System.out.println("White wins. - with a " + stringPHighCard + "\n");
            }
        } else System.out.println("Tie.\n");
        playedHands--;
        //Removing the deck and hands to reshuffle deck and re-deal
        playableDeck.removeAll(playableDeck);
        playerHand.removeAll(playerHand);
        dealerHand.removeAll(dealerHand);
    }
    while (playedHands > 0);
}
static String determineFace(String faceValue){
    String face=faceValue;
    if (face.equals("11")) {
        face = "J";
    }
    else if(face.equals("12")) {
        face = "Q";
    }
    else if (face.equals("13")) {
        face = "K";
    }
    else face = "A";
    return face;
}


我的PokerHandValue类评估单个扑克手:

import java.util.*;

class PokerHandValue {

    private int highCard;
    private int value;
    private String result;
    private List<String>thisHand;

    PokerHandValue(List<String> newHand) {

        this.thisHand=newHand;
        Collections.sort(thisHand);
        int i = 0;
        String card1 = thisHand.get(0);
        String card1num = Character.toString(card1.charAt(0));

        if (isJack(card1num)){
            card1num = "11";
        }
        if (isQueen(card1num)){
            card1num = "12";
        }
        if (isKing(card1num)){
            card1num = "13";
        }
        if (isAce(card1num)){
            card1num = "14";
        }
        int card1val = Integer.parseInt(card1num);
        String card1face = Character.toString(card1.charAt(1));

        String card2 = newHand.get(1);
        String card2num = Character.toString(card2.charAt(0));
        if (isJack(card2num)){
            card2num = "11";
        }
        if (isQueen(card2num)){
            card2num = "12";
        }
        if (isKing(card2num)){
            card2num = "13";
        }
        if (isAce(card2num)){
            card2num = "14";
        }
        int card2val = Integer.parseInt(card2num);
        String card2face = Character.toString(card2.charAt(1));

        String card3 = newHand.get(2);
        String card3num = Character.toString(card3.charAt(0));
        if (isJack(card3num)){
            card3num = "11";
        }
        if (isQueen(card3num)){
            card3num = "12";
        }
        if (isKing(card3num)){
            card3num = "13";
        }
        if (isAce(card3num)){
            card3num = "14";
        }
        int card3val = Integer.parseInt(card3num);
        String card3face = Character.toString(card3.charAt(1));

        String card4 = newHand.get(3);
        String card4num = Character.toString(card4.charAt(0));
        if (isJack(card4num)){
            card4num = "11";
        }
        if (isQueen(card4num)){
            card4num = "12";
        }
        if (isKing(card4num)){
            card4num = "13";
        }
        if (isAce(card4num)){
            card4num = "14";
        }
        int card4val = Integer.parseInt(card4num);
        String card4face = Character.toString(card4.charAt(1));

        String card5 = newHand.get(4);
        String card5num = Character.toString(card5.charAt(0));
        if (isJack(card5num)){
            card5num = "11";
        }
        if (isQueen(card5num)){
            card5num = "12";
        }
        if (isKing(card5num)){
            card5num = "13";
        }
        if (isAce(card5num)){
            card5num = "14";
        }
        int card5val = Integer.parseInt(card5num);
        String card5face = Character.toString(card5.charAt(1));

        String[] sortedHand = {card1, card2, card3, card4, card5};
        if ((card1face.equals(card2face) && card1face.equals(card3face) && card1face.equals(card4face)
                && card1face.equals(card5face)) && (card1.equals(sortedHand[i]) && card2num.equals(sortedHand[i + 1])
                && card3num.equals(sortedHand[i + 2]) && card4num.equals(sortedHand[i + 3]) &&
                card4num.equals(sortedHand[i + 4]))) {
            this.value = 9;
            this.result = "Straight Flush";
        } else if (((card1.charAt(1)==(card2.charAt(1)))&&(card1.charAt(1)==(card3.charAt(1)))&&
        (card1.charAt(1)==(card4.charAt(1))))||(((card2.charAt(1)==(card3.charAt(1)))&&(card2.charAt(1)==(card4.charAt(1)))&&
                (card2.charAt(1)==(card5.charAt(1)))&&(card1.charAt(1)==(card5.charAt(1)))))){
            this.value = 8;
            this.result = "Four of a kind";
        } else if (((card1.charAt(1)==(card2.charAt(1)))&&(card1.charAt(1)==(card3.charAt(1)))&&
                ((card4.charAt(1)==(card5.charAt(1)))))||((card1.charAt(1)==(card2.charAt(1)))
                &&(card3.charAt(1)==(card4.charAt(1)))&&((card3.charAt(1)==(card5.charAt(1)))))){
            this.value = 7;
            this.result = "Full House";
        } else if (card1face.equals(card2face) && card1face.equals(card3face) &&
                card1face.equals(card4face) && card1face.equals(card5face)) {
            this.value = 6;
            this.result = "Flush";
        } else if (card1.equals(sortedHand[i]) && card2num.equals(sortedHand[i + 1]) &&
                card3num.equals(sortedHand[i + 2]) && card4num.equals(sortedHand[i + 3])
                && card4num.equals(sortedHand[i + 4])) {
            this.value = 5;
            this.result = "Straight";
        } else if (((card1.charAt(1)==(card2.charAt(1)))&&(card1.charAt(1)==(card3.charAt(1))))||(
                card3.charAt(0)==card4.charAt(0)&&card3.charAt(0)==card5.charAt(0))
                ) {
            this.value = 4;
            this.result = "Three of a kind";
        } else if (((card1val == card2val || card1val == card3val || card1val == card4val || card1val == card5val) ||
                (card2val == card3val || card2val == card4val || card2val == card5val) || (card3val == card4val ||
                card3val == card5val) || (card4val == card5val)) && ((card1val == card2val || card1val == card3val ||
                card1val == card4val || card1val == card5val) || (card2val == card3val || card2val == card4val ||
                card2val == card5val) || (card3val == card4val || card3val == card5val) || (card4val == card5val))) {
            this.value = 3;
            this.result = "Two pair";
        } else if ((card1val == card2val || card1val == card3val || card1val == card4val || card1val == card5val)
                || (card2val == card3val || card2val == card4val || card2val == card5val) ||
                (card3val == card4val || card3val == card5val) || (card4val == card5val)) {
            this.value = 2;
            this.result = "Pair";
        } else {
            this.highCard=card1val;
            if (card1val < card2val || card1val < card3val || card1val < card4val) {
                this.highCard = card2val;
                if (card2val < card3val || card2val < card4val || card2val < card5val) {
                    this.highCard = card3val;
                    if (card3val < card4val || card3val < card5val) {
                        this.highCard = card4val;
                        if (card4val < card5val) {
                            this.highCard = card5val;
                        }
                    }
                }
                this.value = 1;
                this.result = "High Card of " + highCard;
            }
        }
    }
    int getValue(){
        return value;
    }

    String getResult(){
        return result;
    }

    int getHighCard() {
        return highCard;
    }

     private boolean isJack(String card) {
        if (card.equals("J")){
            return true;
        }
        else return false;
    }
     private boolean isQueen(String card) {
        if (card.equals("Q")){
            return true;
        }
        else return false;
    }
     private boolean isKing(String card) {
        if (card.equals("K")){
            return true;
        }
        else return false;
    }
     private boolean isAce(String card) {
        if (card.equals("A")){
            return true;
        }
        else return false;
    }}

My Cards class.

    //Creating the cards and suits arrays
    private int card,suit;
    private static String[] CARDS = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
    private static String[] SUITS = {"♠", "♣", "♦", "♥"};
    private List<String> inOrderDeck=new ArrayList<>();
    private List<String> shuffledDeck;

    List <String> ShuffleDeck(){
        //creating a deck that is in order of C2-CA,D2-DA,H2-HA,S2-SA
        int n=52;
        int cnt=0;
        String[] deck = new String[n];
        for (String CARD : CARDS) {
            for (String SUIT : SUITS) {
                deck[cnt] = (CARD + SUIT);
                cnt++;
            }
        }
        //Adding set Arrays to List
        inOrderDeck.addAll(Arrays.asList(deck).subList(0, n));
        //Shuffling the deck
        List<String> shuffledDeck=new ArrayList<>(inOrderDeck);
        inOrderDeck.removeAll(inOrderDeck);
        Collections.shuffle(shuffledDeck);
        return shuffledDeck;
    }
}

最佳答案

每次呼叫ShuffleDeck时,都会向inOrderDeck添加52张卡片。第一手牌就可以,但是随后的每手牌数将增加52,这将在最后一手牌开始前在甲板上产生270张牌。

在我看来,您有三个选择:


在您将经过改组的甲板退还到
ShuffledDeck
不要使用静态列表存储未整理的
甲板,并在每次调用ShuffleDeck时重新创建
分离
将未洗牌的套牌创建为createDeck方法,并且
仅在ShuffleDeck方法中执行甲板的改组。


我会去最后一个。

07-24 18:58