Hello guys I need some help, I have an assignment that I have to print a whole deck of cards which means each number is going to repeat 4 times but with different suit. My problem is how can I make it repeat the number only 4 times no more than that or less. and im using random. please someone help ASAP.
Create Deck of Cards
Page 1 of 110 Replies - 7461 Views - Last Post: 03 February 2011 - 09:27 PM
Replies To: Create Deck of Cards
#2
Re: Create Deck of Cards
Posted 02 February 2011 - 09:45 PM
Topic split. Please avoid necroposting.
I would create two arrays with the suits and values, then use nested loops to build your deck. The outer loop should traverse one array, and the inner loop should traverse the other. Then use a Random number generator to pick 4 cards. If you use a List<Card>, you can remove() the random Card you select, not allowing for repetition.
I would create two arrays with the suits and values, then use nested loops to build your deck. The outer loop should traverse one array, and the inner loop should traverse the other. Then use a Random number generator to pick 4 cards. If you use a List<Card>, you can remove() the random Card you select, not allowing for repetition.
String[] suits = new String[]{"Clubs","Hearts","Diamonds","Spades"};
String[] values = new String[]{"Ace", "2", "3", "4", ... , "Q", "K"};
#3
Re: Create Deck of Cards
Posted 02 February 2011 - 09:50 PM
Anyway, if you want a more object-based system, you can follow pbl's code snippet on the subject: http://www.dreaminco...snippet3176.htm
All you'd have to do is do a Collections.shuffle() on the returned deck and then siphon them off of the collection to print.
#4
Re: Create Deck of Cards
Posted 02 February 2011 - 09:51 PM
Actually, not parallel arrays at all b/c the values in the same indices don't correspond to each other.
#5
Re: Create Deck of Cards
Posted 02 February 2011 - 09:56 PM
*Facepalm* Time for me to sleep now...messing up stuff like that...
#6
Re: Create Deck of Cards
Posted 03 February 2011 - 12:24 PM
this is what i have
import java.util.Random;
public class Prog1
{
static Random rr = new Random();
static char suit(char... letters)
{
return letters[rr.nextInt(letters.length)];
}
public static void main (String [] args)
{
int cardsDealt = 0;
int[] myDeck = new int[52];
int deck = rr.nextInt(13) + 1;
final int cardsPerRow = 8;
int cardsThisRow = 0;
int myCard;
initDeck(myDeck,deck);
System.out.println("\nHere is a shuffled deck ...\n");
while (cardsDealt < 52)
{
myCard = myDeck[cardsDealt++];
++cardsThisRow;
if (cardsThisRow <= cardsPerRow)
{
printCard(myCard);
System.out.print(" ");
}
else
{
System.out.println("");
cardsThisRow = 1;
printCard(myCard);
System.out.print(" ");
}
}
System.out .println('\n');
}
public static void initDeck(int [] myDeck, int deck)
{
for (int i = 0; i < 52; ++i)
{
deck = rr.nextInt(13) + 1;
myDeck[i] = deck;
}
}
public static void printCard(int card)
{
if (card == 1)
{
System.out.print("A" + suit('S', 'C', 'H', 'D'));
}
else if (card == 11)
{
System.out.print("J" + suit('S', 'C', 'H', 'D'));
}
else if (card == 12)
{
System.out.print("Q" + suit('S', 'C', 'H', 'D'));
}
else if (card == 13)
{
System.out.print("K" + suit('S', 'C', 'H', 'D'));
}
else
{
System.out.print(card + "" + suit('S', 'C', 'H', 'D'));
}
}
/*public static boolean duplicate(int [] myDeck, int deck)
{
for (int i = 0; i < 52; ++i)
{
if (myDeck[i] == deck)
{
return true;
}
}
return false;
}*/
}
#7
Re: Create Deck of Cards
Posted 03 February 2011 - 12:27 PM
What problems or errors are you encountering? Please be specific.
#8
Re: Create Deck of Cards
Posted 03 February 2011 - 05:01 PM
sorry for not being specific, im trying to let each number to repeat only 4 times. and each number have its suit next to it without repetition of the suit for that number.
#9
Re: Create Deck of Cards
Posted 03 February 2011 - 05:04 PM
So what problems or errors are you encountering?
#10
Re: Create Deck of Cards
Posted 03 February 2011 - 08:07 PM
there is no errors I dont know how to make the random to create each number only 4 times.
#11
Re: Create Deck of Cards
Posted 03 February 2011 - 09:27 PM
macosxnerd101, on 03 February 2011 - 01:45 AM, said:
I would create two arrays with the suits and values, then use nested loops to build your deck. The outer loop should traverse one array, and the inner loop should traverse the other. Then use a Random number generator to pick 4 cards. If you use a List<Card>, you can remove() the random Card you select, not allowing for repetition.
String[] suits = new String[]{"Clubs","Hearts","Diamonds","Spades"};
String[] values = new String[]{"Ace", "2", "3", "4", ... , "Q", "K"};
I honestly think you would be better off implementing my suggestion from my last post. Design a Card class, and store a Card[] or List<Card>.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote








|