0

I have a program that generates a deck of playing cards and displays seven random cards. I tried to pass the seven cards that were selected into a string, however I only know how to set the entire array to a string, not just the selected 7 cards.

public class PlayedCards{        
      public static void main(String[] args){     
        int[] deck = new int[52];     
        String[] suits = {"Spades", "Clubs", "Diamonds", "Hearts"};    
        String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};    
        for (int a = 0; a < deck.length; a++){
                deck[a]= a;
        }

        for (int a = 0; a <deck.length; a++){
            int order = (int)(Math.random() * deck.length);
            int temp = deck[a];
            deck[a] = deck[order];
            deck[order] = temp;
        }

        for (int a = 0; a < 7; a++){
            String suit = suits[deck[a] / 13];
            String rank = ranks[deck[a] % 13];
            System.out.println(rank + " of " + suit);
        }

        System.out.println(rank + " of " + suit);
     }
}

My questions are:

Are the cards that are displayed the values of 0-6 in my array?

If so, how would I set those values for the array to a separate string that i could then recall for the user as being played?

3
  • 5
    Can you add an example of the String you want? Commented Feb 12, 2014 at 0:59
  • You could use a StringBuilder to concatenate whatever you want inside the loop, and then call toString() to generate a string for you. Commented Feb 12, 2014 at 1:01
  • I want to be able to tell the user ("The cards that have been played are" + firstHand* + ) firstHand would be the first seven cards that were played in the program. Commented Feb 12, 2014 at 1:04

3 Answers 3

1

If you just want to create a String that represents what was played for the player, either directly concatenate each card type and number to a resulting String (implicitly creating a new String on each concatenation) or, better yet, use a StringBuilder to do that and convert to a String at the end. So like this:

StringBuilder sb = new StringBuilder();
for (int a = 0; a < 7; a++) {
...
sb.append(a+1).append(": ").append(rank).append(" of ").append(suit).append("\n");
...
String playedCards = sb.toString();
System.out.println(playedCards);
Sign up to request clarification or add additional context in comments.

Comments

0

Better create a immutable card class

class Card
{
     private final int m_suit;
     private final int m_rank;

     public Card( int suit, int rank )
     {
         m_suit = suit;
         m_rank = rank;
     }

     public int getSuit()
     {
          return m_suit;
     }

     public int getRank()
     {
          return m_rank;
     }

}

Now you can create the card instance and pass it around or store it in a ArrayList. For e.g.

Card card1 = new Card( deck[a] / 13, deck[a] % 13);
System.out.println( rank[card1.getRank()] + " of " + suit[card1.getSuit()]);

Comments

0
public class PlayCards {
public static void main(String[] args) {
    int[] deck = new int[52];
    String[] suits = { "Spades", "Clubs", "Diamonds", "Hearts" };
    String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
            "Jack", "Queen", "King" };
    for (int a = 0; a < deck.length; a++) {
        deck[a] = a;
    }

    for (int a = 0; a < deck.length; a++) {
        int order = (int) (Math.random() * deck.length);
        int temp = deck[a];
        deck[a] = deck[order];
        deck[order] = temp;
    }

    Card card = null;
    Card[] cards = new Card[7];
    for (int a = 0; a < 7; a++) {
        card = new Card(suits[deck[a] / 13], ranks[deck[a] % 13]);
        cards[a] = card;
    }

    Player player = new Player();
    player.showCards(cards);
}
}

class Card {
private String suit;
private String rank;

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

public String toString() {
    StringBuilder card = new StringBuilder();
    card.append(this.rank);
    card.append(',');
    card.append(this.suit);
    return card.toString();
}
}

class Player {
public void showCards(Card[] cards) {
    for (int a = 0; a < 7; a++) {
        System.out.println(cards[a].toString());
    }
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.