Shuffling a deck in Java

2020-07-23 04:52发布

问题:

I have another exercise I have to do that I really need help with. I don't even know if my isFlush method is working because for seem reason my deck isnt shuffling and dealing a hand and I'm completely stuck. Can someone help me or point me in the right direction or something? Here is the exercise:

Exercise 12.5 The goal of this exercise is to write a program that generates random poker hands and classifies them, so that we can estimate the probability of the various poker hands. Don’t worry if you don’t play poker; I’ll tell you everything you need to know.

a. As a warmup, write a program that uses shuffleDeck and subdeck to generate and print four random poker hands with five cards each. Did you get anything good? Here are the possible poker hands, in increasing order of value: pair: two cards with the same rank two pair: two pairs of cards with the same rank three of a kind: three cards with the same rank straight: five cards with ranks in sequence flush: five cards with the same suit full house: three cards with one rank, two cards with another four of a kind: four cards with the same rank straight flush: five cards in sequence and with the same suit

b. Write a method called isFlush that takes a Deck as a parameter and returns a boolean indicating whether the hand contains a flush.

c. Write a method called isThreeKind that takes a hand and returns a boolean indicating whether the hand contains Three of a Kind.

d. Write a loop that generates a few thousand hands and checks whether they contain a flush or three of a kind. Estimate the probability of getting one of those hands.

e. Write methods that test for the other poker hands. Some are easier than others. You might find it useful to write some general-purpose helper methods that can be used for more than one test.

f. In some poker games, players get seven cards each, and they form a hand with the best five of the seven. Modify your program to generate seven-card hands and recompute the probabilities.

And here is my code:

public class Poker {

    public static void main(String[] args) {
        Deck deck = new Deck ();
        Deck.dealDeck (deck);
    }

}
class Card {

    int suit, rank;
    int index = 0;
    //Card[] deck = new Card [52];

    public Card () {
        this.suit = 0; this.rank = 0;
    }

    public Card (int suit, int rank) {
        this.suit = suit; this.rank = rank;
    }

    public static void printCard (Card c) {
        String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
        String[] ranks = { "narf", "Ace", "2", "3", "4", "5", "6",
        "7", "8", "9", "10", "Jack", "Queen", "King" };
        System.out.println (ranks[c.rank] + " of " + suits[c.suit]);
    }
}

class Deck {

    Card[] cards;


    public Deck (int n) {
        cards = new Card[n];
    }

    public Deck () {
        cards = new Card[52];
        int index = 0;
        for (int suit = 0; suit <= 3; suit++) {
            for (int rank = 1; rank <= 13; rank++) {
                cards[index] = new Card (suit, rank);
                index++;
            }
        }
    }

    public static Deck dealDeck(Deck deck){
        shuffle(deck);
        Deck hand1 = subDeck(deck, 0, 4);
        Deck hand2 = subDeck(deck, 5, 9);
        Deck hand3 = subDeck(deck, 10, 14);
        Deck hand4 = subDeck(deck, 15, 19);
        Deck pack = subDeck(deck, 20, 51);
        return deck;
    }

    public static Deck shuffle(Deck deck){
        for (int i = 0; i < 52; i++){
            int rand = (int)(Math.random()*(i + 1));
            **Deck[] temp = deck[i];
            deck[i] = deck[rand];
            deck[rand] = deck[temp];**
        }
        return deck;
    }

    public static Deck subDeck(Deck deck, int low, int high) {
        Deck sub = new Deck (high-low+1);
        for (int i = 0; i < sub.cards.length; i++) {
            sub.cards[i] = deck.cards[low+i];
        }
        return sub;
    }

    public static void printDeck (Deck hand) {
        for (int i = 0; i < hand.cards.length; i++) {
            Card.printCard (hand.cards[i]);
        }
    }

    public static boolean isFlush(Card[] x, int y) {
        int count = 0;
        for(int index = 0; index < y; index++){
            boolean comp = compareIfFlush(x[index], x[index + 1]);
            if (comp = true){
                count++;
            }
        }
        if (count >= 5){
            System.out.println("Congratulations! You have a flush!");
            return true;
        }
        else{
            System.out.println("Sorry, you do not have a flush.");
            return false;
        }
    }

    public static boolean compareIfFlush(Card c1, Card c2){
        if (c1.suit != c2.suit){
            return false;
        }
        return true;
    }
}

I put in bold the part I'm having trouble with. I get the error: "array required, but exercise125poker.Deck found". I need a work around for this error because I don't know how to fix it so I'm stuck. Can anyone help?

回答1:

Your Deck type isn't an array type. It has an array in it, but you can't use it like one - which is what's generating your error. Consider this... what if your Deck had two Card[52]s in it? then if you called deck[25] it wouldn't make any sense. Typically when you're hiding an array inside an object you'll want to expose the array with accessor methods like public Card get(int i) to retrieve elements out of an inner array.



回答2:

In order to solve your problem, change the lines of code that you highlighted to:

Card temp = deck.cards[i];
deck.cards[i] = deck.cards[rand];
deck.cards[rand] = temp;

There were 2 problems with the code.

  • The first was that Deck is not an array, and you wanted to access the cards array within it(Hence adding .cards to the arrays).
  • Also the Deck[] expects to be given an array of decks, instead what you were trying giving it, a Card.

Hope this helps,

Lee



标签: java poker