0

I am trying to write some code that creates two arrays, one with suits, one with values, of a deck of cards. It then shuffles the cards randomly and prints out the cards that each of the four players is dealt. If anyone could give me some ideas on how to proceed from what I have I would be grateful. The main problems I'm having is how to switch the arrays around (shuffle) and then how to show what the players get (deal).

public class BridgeHands 

{

private card[] deck;
private int cardsUsed;

public static void dealHands() 
{
    final int SUITS = 4;                    // number of suits in a standard deck
    final int CARDS_PER_SUIT = 13;          // number of cards in each suit
    final int CARDS = SUITS*CARDS_PER_SUIT; // number of cards in a standard deck

    deck = new Card[CARDS];
    int cardCt = 0;
    for(int suit = 0; suit < SUITS; suit++)
    {
        for(int value = 1; value < CARDS_PER_SUIT; value++)
        {
            deck[cardCt] = new Card(value,suit);
            cardCt++;
        }
        cards used = 0;

    }
}

public void shuffle()
{
    for(int i = deck.length-1; i > 0; i--)
    {
        int rand = (int)(Math.random()*(i+1));
        Card temp = deck[i];
        deck[i] = deck[rand];
        deck [rand] = temp;
    }
    cards used = 0;
}

public int cardsLeft() 
{
    int cardsUsed = 0;
    return deck.length - cardsUsed;
}

public Card dealCard()
{
    if(cardsUsed == deck.length)
        throw new IllegalStateException("No cards are left in the deck.");
    cardsUsed++;
    return deck[cardsUsed - 1];
}

final int HANDS = 4;
final int CARDS_PER_HAND = CARDS/HANDS;

public static String cardName(int suit, int value)
{
    final String[] CARD_NAMES =
        {"not used", "ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king"};
    final String[] SUIT_NAMES = 
        {"spades", "clubs", "diamonds", "hearts"};

    return CARD_NAMES[value] + " of " + SUIT_NAMES[suit];
}

}

civitas
  • 15
  • 1
  • 5
  • Shuffling an array isn't difficult, see [this](http://stackoverflow.com/a/3732080/3755692) answer. And what do you mean by _"how to show what the players get (deal)"_? – msrd0 Sep 20 '14 at 09:42
  • And you miss a `;` at this line: `count = count - 1`. BTW I would replace this with `count--;` – msrd0 Sep 20 '14 at 09:43
  • IIWY I'd create `Card` and `Deck` classes and `SUITE` and `VALUE` enums. Then everything's together; no need for tracking a bunch of stuff when shuffling. – ChiefTwoPencils Sep 20 '14 at 09:44
  • It seems like your code misses some `{}`, your `for` loop doesn't do anything, your `while` loop uses an not existing variable and will never terminate – msrd0 Sep 20 '14 at 09:46
  • If I use the array structure of the this answer link it uses for loops and I'm using while. Then there is some code to translate the numbers into card names but it's not linked. I don't get it I am a library major but now library degrees are it degrees so there are core units in programming. The only time programming has come in useful at work was when I understood why a dialog. Box was saying string not found. Argh. – civitas Sep 20 '14 at 11:36

1 Answers1

-1

i'm struggling with the same question as you, we're probably in the same unit. I've done a little more than you for the shuffling part, I've got:

int cards = 52;
    int card1, card2
    while (cards > 1)

            for( int k=0; k<100; k++ ) 
        { 
            card1 = rand.nextInt(52); 
            card2 = rand.nextInt(52); 
            int keepSuit = dk[card1]; 
            dk[card1] = dk[card2]; 
            dk[card2] = keepSuit; 
        }  

Maybe this helps? Not sure if it's right however, sorry.

emily
  • 1