-3

How do you initialize a boolean array for the suits in a deck of cards and then make it so that a random number will turn the suit to true? For initializing an array, I have

    boolean[] suitsSelected = {"spades","clubs","diamonds", "hearts"};

and then declare a random number

    Random myRandom = newRandom();
    int card = myRandom.nextInt(51);

and then turn the boolean suit to true once the card has been selected?

    spade = (card >= 0 && card < 14);
    heart = (card >= 14 && card < 27);
    club = (card >= 27 && card < 39);
    diamond = (card >= 40 && card < 52);
Joshua
  • 19
  • 1
  • 3
  • 1
    Shouldn't `nextInt(51)` be 52 instead? I think there are 52 cards in a standard deck. – markspace Feb 27 '18 at 18:08
  • 6
    why is your boolean array filled with strings? – Tim Feb 27 '18 at 18:10
  • 1
    How do you intend to represent the four suits with a binary type? – Elliott Frisch Feb 27 '18 at 18:11
  • Yeah that's just... weird. Please show us some code that compiles. (at Tim's comment.) Overall, I don't like this approach. It can be done, but if you're struggling with it I think a more standard approach which declares the cards as individual objects and then moves them around in a collection would be easier and clearer too. – markspace Feb 27 '18 at 18:11
  • Why do you have 14 spades, 13 hearts, 12 clubs, and 13 diamonds in you deck? – Andreas Feb 27 '18 at 18:13
  • @the_storyteller Check the docs on that method. The number 51 is not included in the return value. – markspace Feb 27 '18 at 18:14
  • 1
    @the_storyteller He *is* using 0-based numbering, and `nextInt(51)` only returns 51 distinct values (0-50 inclusive), which isn't right when there are 52 cards. – Andreas Feb 27 '18 at 18:14
  • Can you post a more complete example of your code (one that actually compiles, if possible)? Maybe tell us a little bit about why you made the architectural decisions that you did? Consider doing some further research and trying a few more things, maybe even chat with a colleague or instructor. As it stands addressing what you have is going to require a discussion format that SO does not really support. – Brandon McKenzie Feb 27 '18 at 18:22

1 Answers1

1

You could do it with booleans, like

final int SPADES_INDEX = 0;
final int CLUBS_INDEX = 1;
final int HEARTS_INDEX = 2;
final int DIAMONDS_INDEX = 3;

final boolean[] suits = new boolean[4];

// helper function
void switchToSuit(int suitIndex) {
    for(int i = 0; i < suits.length; i++) {
        suits[i] = (suitIndex == i);
    }
}

// later
if(card >= 0 && card <= 14) switchToSuit(SPADES_INDEX);

But that's a bit wonky. You could use ints much simpler than that, if you want.

final int SPADES_INDEX = 0;
final int CLUBS_INDEX = 1;
final int HEARTS_INDEX = 2;
final int DIAMONDS_INDEX = 3;

int suit = card / 13; // 0-12 are 0, 13-25 are 1, 26-38 are 2, and 39-51 are 3

An even better approach would be to use enum

import java.util.*; 

public class Main {
    static Random rand = new Random();
    static enum Suit {
        SPADES,
        CLUBS,
        HEARTS,
        DIAMONDS;
        public String toString() {
            return name().substring(0, 1);
        }
    }
    static enum Rank {
        ACE("A"), TWO("2"), THREE("3"), FOUR("4"),
            FIVE("5"), SIX("6"), SEVEN("7"), EIGHT("8"),
            NINE("9"), TEN("T"), JACK("J"), QUEEN("Q"), KING("K");
        final String key;
        Rank(String key) {
            this.key = key;
        }
        public String toString() {
            return key;
        }
    }
    public static void main(String[] args) {

        List <String> pocket = new ArrayList<>();
        List <String> board = new ArrayList<>();
        int[] deck = new int[52];
        for (int i = 0; i < 52; i++) {
            deck[i] = i;
        }
        shuffleArray(deck);
        for (int i = 0; i < 2; i++) {
            int card = deck[i];
            Suit suit = Suit.values()[card / 13];
            Rank rank = Rank.values()[card % 13];
            pocket.add("" + rank + suit);
        }
        for (int i = 0; i < 5; i++) {
            int card = deck[i + 2];
            Suit suit = Suit.values()[card / 13];
            Rank rank = Rank.values()[card % 13];
            board.add("" + rank + suit);
        }
        System.out.println("Hold'em pocket: " + pocket);
        System.out.println("Hold'em board: " + board);

    }

    // Fisher–Yates shuffle - https://stackoverflow.com/a/1520212/330057
    static void shuffleArray(int[] ar) {
        // If running on Java 6 or older, use `new Random()` on RHS here
        for (int i = ar.length - 1; i > 0; i--) {
            int index = rand.nextInt(i + 1);
            // Simple swap
            int a = ar[index];
            ar[index] = ar[i];
            ar[i] = a;
        }
    }
}
corsiKa
  • 81,495
  • 25
  • 153
  • 204