2

I am trying to create a program that would deal out two cards out of a deck of 52 cards. So far, I have used a switch statement to assign card values to a number, Ace of Spades = 0, Seven of Clubs = 26 for example. However, in order to have two cards, they must be unique and I'm not sure how to go about that.

import java.lang.Object;
import java.util.Scanner;
import java.util.Random;

public class Deck {

public static void main(String[] args) {

    Random rnd = new Random();
    int cardNumber = rnd.nextInt(52);
    String toCard = null;
    switch (cardNumber){
    case 0: toCard = "Ace of Spades";
            break;
    case 1: toCard = "Ace of Hearts";
            break;      
    case 2: toCard = "Ace of Clubs";
            break;
    case 3: toCard = "Ace of Diamonds";
            break;      
    case 4: toCard = "Two of Spades";
            break;
    case 5: toCard = "Two of Hearts";
            break;      
    case 6: toCard = "Two of Clubs";
            break;      
    case 7: toCard = "Two of Diamonds";
            break;      
    case 8: toCard = "Three of Spades";
            break;      
    case 9: toCard = "Three of Hearts";
            break;      
    case 10: toCard = "Three of Clubs";
            break;      
    case 11: toCard = "Three of Diamonds";
            break;  
    case 12: toCard = "Four of Spades";
            break;      
    case 13: toCard = "Four of Hearts";
            break;      
    case 14: toCard = "Four of Clubs";
            break;      
    case 15: toCard = "Four of Diamonds";
            break;  
    case 16: toCard = "Five of Spades";
            break;      
    case 17: toCard = "Five of Hearts";
            break;      
    case 18: toCard = "Five of Clubs";
            break;      
    case 19: toCard = "Five of Diamonds";
            break;
    case 20: toCard = "Six of Spades";
            break;      
    case 21: toCard = "Six of Hearts";
            break;      
    case 22: toCard = "Six of Clubs";
            break;      
    case 23: toCard = "Six of Diamonds";
            break;
    case 24: toCard = "Seven of Spades";
            break;      
    case 25: toCard = "Seven of Hearts";
            break;      
    case 26: toCard = "Seven of Clubs";
            break;      
    case 27: toCard = "Seven of Diamonds";
            break;
    case 28: toCard = "Eight of Spades";
            break;      
    case 29: toCard = "Eight of Hearts";
            break;      
    case 30: toCard = "Eight of Clubs";
            break;      
    case 31: toCard = "Eight of Diamonds";
            break;
    case 32: toCard = "Nine of Spades";
            break;      
    case 33: toCard = "Nine of Hearts";
            break;      
    case 34: toCard = "Nine of Clubs";
            break;      
    case 35: toCard = "Nine of Diamonds";
            break;
    case 36: toCard = "Ten of Spades";
            break;      
    case 37: toCard = "Ten of Hearts";
            break;      
    case 38: toCard = "Ten of Clubs";
            break;      
    case 39: toCard = "Ten of Diamonds";
            break;
    case 40: toCard = "Jack of Spades";
            break;      
    case 41: toCard = "Jack of Hearts";
            break;      
    case 42: toCard = "Jack of Clubs";
            break;      
    case 43: toCard = "Jack of Diamonds";
            break;
    case 44: toCard = "Queen of Spades";
            break;      
    case 45: toCard = "Queen of Hearts";
            break;      
    case 46: toCard = "Queen of Clubs";
            break;      
    case 47: toCard = "Queen of Diamonds";
            break;
    case 48: toCard = "King of Spades";
            break;      
    case 49: toCard = "King of Hearts";
            break;      
    case 50: toCard = "King of Clubs";
            break;      
    case 51: toCard = "King of Diamonds";
            break;
    }


System.out.println(cardNumber + " " + toCard);

}

}

I know that there's probably many more efficient ways of approaching this but I'm relatively new to coding in general. I want to assign each of the cases to an index in an array so that I can call say cardArray[0] and return Ace of Spades, and eventually call two random numbers from the cardArray and have them be unique cards.

Any help is appreciated.

Dylan Ray
  • 21
  • 2
  • Either shuffle the array, or keep randomizing until you find a new card. – shmosel Feb 06 '17 at 02:53
  • Instead of using a switch statement, make a String[] array and put all of your cards in there. Call `rnd.nextInt(52);` once to get your first card, then you can use a while loop like `while(true){ num=rnd.nextInt(52); if(num!=firstCard) break; }`. This will keep randomizing a new number until you get one that wasn't the same as your original card. – Jessie Feb 06 '17 at 02:53
  • I should mention that if you are only drawing 2 cards then repeating the randomization is fine. But if you want to do it for more cards you should definitely shuffle the deck – Jessie Feb 06 '17 at 02:55
  • Maybe this will help - http://stackoverflow.com/questions/1519736/random-shuffling-of-an-array – Siddharth Tyagi Feb 06 '17 at 04:57
  • I hope this helped – Jan Hanson Feb 06 '17 at 14:15

3 Answers3

1

In order to deal two unique cards to players, your class design should be looking similar to this:

Player should have an array of cards.

public class Player {
    private String[] cards;

    public String[] getCards() {
        return cards;
    }

    public void setCards(String[] cards) {
        this.cards = cards;
    }

    @Override
    public String toString() {
        return "Player [cards=" + Arrays.toString(cards) + "]";
    }
}

CardDealer should be having a deck of cards and a deal(Player... players) method which deals cards to the given players.

public class CardDealer {
    private static Random rand;
    private static final String[] DECK = { "Ace of Spades",
            "Ace of Hearts",
            "Ace of Clubs",
            "Ace of Diamonds",
            "Two of Spades",
            "Two of Hearts",
            "Two of Clubs",
            "Two of Diamonds",
            "Three of Spades",
            //etc.
    };
    //Use a java.util.Random object to generate random numbers.
    public CardDealer(){
        rand = new Random();
    }
    /**
      * This method accepts variable number of players
      * and for each player, it picks up two unique cards
      * from the deck, creates an array of those two cards
      * and assigns that array to the players
      */
    public void deal(Player... players){
        for(Player eachPlayer : players){
            // Create an array of 2 cards for each player
            String[] cards = new String[2];
            // Pick first card randomly from deck.
            int cardIndex1 = rand.nextInt(52);
            String card1 = DECK[cardIndex1];

            // Pick second card randomly from deck
            int cardIndex2 = rand.nextInt(52);
            // If the second picked index is equal to first, pick again
            while(cardIndex1 == cardIndex2){
                cardIndex2 = rand.nextInt(52);
            }
            String card2 = DECK[cardIndex2];

            //Give each player his cards.
            cards[0] = card1;
            cards[1] = card2;

            eachPlayer.setCards(cards);
        }
    }
}

Now, if you run a test like this:

        CardDealer dealer = new CardDealer();
        Player sam = new Player();
        Player joe = new Player();

        dealer.deal(sam,joe);

        System.out.println("Sam -> " + sam);
        System.out.println("Joe -> " + joe);

It should produce the result like this:

Sam -> Player [cards=[Ten of Spades, Two of Spades]]
Joe -> Player [cards=[Two of Diamonds, King of Hearts]]

I hope this gives you some idea to carry it on further.

Shyam Baitmangalkar
  • 1,075
  • 12
  • 18
0

you can use a Set to make sure that your values are unique:

public ArrayList<String> drawCards(int noOfCardsToDraw)
{
    //set up an array of strings that represent your cards
    //and can be indexed 0-51 i.e. 52 cards total.
    String[] deck = {
                  "Ace of spades",
                  "two of spades",
                  "three of spades",
                  "for of spades"
                  //etc
                    };
    //a set of integers ensures that you have no duplicate
    //card values
    Set<Integer> cardValues = new Hashset<Integer>();

    //pick your random card values and add them to the set,
    //if there happen to be two identical random numbers
    //then inserting it into the set does nothing, so this
    //will loop until your chosen number of cards have been
    //picked.
    while(result.size < noOfCardsToDraw)
    {
        cardValues.add(deck[rnd.nextInt(51)]);
    }

    //and arraylist of strings to hold the card designations
    ArrayList<String> result = new ArrayList<String>();

    //loop through the set of card values adding the corresponding
    //string at that index to the arraylist.
    for(int card : cardValues)
    {
     result.add(deck[card]);
    }

    //return the arraylist to do with as you please
    return result;
}

This should keep on picking random cards until the set has the same size as the number you pass into the method. So if you call it like:

ArrayList<String> cards = drawCards(2);

Which you can then retrieve values by using:

cards.get(0) //for first element
cards.get(1) //for second element

then you should have two unique random cards.

Jan Hanson
  • 194
  • 5
  • 13
0
static final String[] cards = {"Ace","Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
static final String[] colors ={"Spades", "Hearts", "Clubs", "Diamonds"};

public static String getAcard(int num) {
    String card = cards[num%cards.length];
    String color = colors[num/cards.length];
    return card + " of " + color;
}

public static void main(String[] args) {

    int deckSize = cards.length * colors.length;

    List<String> deck = new ArrayList<>();

    for (int i=0; i<deckSize; i++) {
        deck.add(getAcard(i));
    }

    Collections.shuffle(deck);
    List<String> pair = deck.subList(0, 2);
    pair.forEach( p-> System.out.println(p));

}
Dariusz
  • 21,561
  • 9
  • 74
  • 114