Shuffling a deck in Java

I have another exercise that I have to do, and I really need help. I don’t even know if my isFlush method works, because, apparently, my deck is not shuffling and does not hold a hand, and I am completely stuck. Can someone help me or point me in the right direction or something else? Here is an exercise:

Exercise 12.5 The purpose of this exercise is to create a program that generates random poker hands and classifies them so that we can assess the likelihood of different poker hands. Do not worry if you do not play poker; I will tell you everything you need to know.

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

b. Write a method called isFlush that takes a deck as a parameter and returns boolean to indicate whether the pen contains a flash.

with. Write a method called isThreeKind that takes a hand and returns a boolean indicating whether the hand contains Three types.

e. Write a cycle that generates several thousand hands and checks whether it contains a flash or three kinds. Estimate the probability of getting one of these hands.

e. Write down methods that other poker hands test. Some of them are lighter than others. You might find it helpful to write some helper helper methods that can do for more than one test.

f. In some poker games, players receive seven cards each, and they form a hand with the top five of seven. Change your program to create seven-card hands and recalculate 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;
    }
}

, . : " , exercise125poker.Deck found". , , , . - ?

+3
2

Deck . , - , . ... , Deck Card[52] ? , deck[25], . , , , public Card get(int i), .

+3

, :

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

2 .

  • , Deck , (, .cards ).
  • Deck [] , , , Card.

, ,

+1

All Articles