10 Replies - 12468 Views - Last Post: 03 February 2011 - 09:27 PM Rate Topic: -----

#1 anas2omari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-February 11

Create Deck of Cards

Posted 02 February 2011 - 09:40 PM

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.
Is This A Good Question/Topic? 0
  • +

Replies To: Create Deck of Cards

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

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.
String[] suits = new String[]{"Clubs","Hearts","Diamonds","Spades"};
String[] values = new String[]{"Ace", "2", "3", "4", ... , "Q", "K"};


Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: Create Deck of Cards

Posted 02 February 2011 - 09:50 PM

:) macosxnerd101 is advocating parallel arrays...

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.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

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. :)
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: Create Deck of Cards

Posted 02 February 2011 - 09:56 PM

*Facepalm* Time for me to sleep now...messing up stuff like that... :)
Was This Post Helpful? 0
  • +
  • -

#6 anas2omari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-February 11

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;
	}*/


}


Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Create Deck of Cards

Posted 03 February 2011 - 12:27 PM

What problems or errors are you encountering? Please be specific.
Was This Post Helpful? 0
  • +
  • -

#8 anas2omari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-February 11

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.
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Create Deck of Cards

Posted 03 February 2011 - 05:04 PM

So what problems or errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

#10 anas2omari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-February 11

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.
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Create Deck of Cards

Posted 03 February 2011 - 09:27 PM

View Postmacosxnerd101, 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>.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1