6 Replies - 1795 Views - Last Post: 18 April 2009 - 03:31 PM Rate Topic: -----

#1 swim_fan08  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 19-February 09

Shuffling deck of cards

Posted 15 April 2009 - 08:06 PM

I am designing a class where I have to shuffle the cards and I am trying to get the cards to shuffle. How would I use a loop to change the cards at least 999999 pairs of cards? An example would help me out.

This post has been edited by pbl: 18 April 2009 - 03:33 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Shuffling deck of cards

#2 sl4ck3r  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 285
  • Joined: 22-September 07

Re: Shuffling deck of cards

Posted 15 April 2009 - 08:47 PM

Dream in code has a policy of not doing your homework for you. You show us some code we show you some help.
Was This Post Helpful? 0
  • +
  • -

#3 swim_fan08  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 19-February 09

Re: Shuffling deck of cards

Posted 15 April 2009 - 08:58 PM

Sorry I forgot my code

	import java.util.Random;

	public class DeckOfCards
	{
		private Card [] deck;
		private int cardsDelt=0;


		//------------------------------------------
		//Constructor (Instantiates a deck)
		//Cards are unshuffled
		//------------------------------------------

		public DeckOfCards ()
		{
			deck=new Card[52];
			int cardCount = 0;

				   for ( int suit = 0; suit <= 3; suit++ )
				   {
					  for ( int value = 1; value <= 13; value++ )
					  {
						 deck[cardCount] = new Card(value,suit);
						 cardCount++;
						 cardsDelt = 0;

					  }
				   }

			   }

			   //-------------------------------------
			   //Shuffle the deck of 52 cards.
			   //-------------------------------------

				 public void shuffle()
				 {

					   for ( int index = 51; index > 0; index-- )
					   {
						   int rand = (int)(Math.random()*(index+1));
						   Card temp = deck[index];
						   deck[index] = deck[rand];
						   deck[rand] = temp;

						   cardsDelt = 0;

					   }

				   }

				   //----------------------------
				   //Deals a card from the deck
				   	//----------------------------
				   		public Card dealCard()
				   		{

				   			if (cardsDelt == 52)
				   			shuffle();
				   			cardsDelt++;
							return deck[cardsDelt - 1];
						}

				//---------------------------
				//Cards left in the deck
				//---------------------------
				public int cardsLeft()
				{

			return 52 - cardsDelt;
			}
			 public String toString()
			{
			 return cardsLeft () + "cards left in the deck";
			}

		}



Was This Post Helpful? 0
  • +
  • -

#4 A2ZOMG  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-March 09

Re: Shuffling deck of cards

Posted 15 April 2009 - 09:49 PM

Do you have a class called "Card"? As far as I know, you kinda need to have a Card object defined before you can go about Shuffling those around.

At least, this is the way I have it working.

This post has been edited by A2ZOMG: 15 April 2009 - 09:49 PM

Was This Post Helpful? 0
  • +
  • -

#5 swim_fan08  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 19-February 09

Re: Shuffling deck of cards

Posted 16 April 2009 - 10:40 AM

Yes, I have a "Card" class.
Was This Post Helpful? 0
  • +
  • -

#6 swim_fan08  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 19-February 09

Re: Shuffling deck of cards

Posted 17 April 2009 - 09:53 PM

I would appreciate it if I got some help correcting my errors. For the incompatible errors, I tried changing temp to Card, but my errors didnt get fix.


Errors I get:

java:29: cannot find symbol
symbol : variable index
location: class DeckOfCards
for ( int cardCount = 0; cardCount < index; cardCount++ )

java:31: cannot find symbol
symbol : constructor Card(java.lang.String,java.lang.String)
location: class Card
deck[ cardCount ] = new Card( faces[ cardCount % 13 ], suits[ cardCount / 13 ])

java:54: temp is already defined in shuffle()
Card temp = deck[num1];

java:54: incompatible types
found : int
required: Card
Card temp = deck[num1]

java:56: incompatible types
found : Card
required: int
deck[num2] = temp;



import javax.swing.*;

public class DeckOfCardsDriver
{
	
	public static void main (String[] args)
	{

		DeckOfCards cards = new DeckOfCards ();

		cards.shuffle();
		cards.dealCard();

	  for ( int index = 0; index < 12; index++ )
	  {

	
		for (int index = 0; index < 5; index++)
		{
		faceValue = deck[index] % 13 + 1;
		suit = deck[index] / 13;

}
}
}
}



import java.util.Random;

		public class DeckOfCards
		{
			private Card [] deck;
			private int cardsDealt=0;
			private int SWAPS=999999;

			

			public DeckOfCards ()
			{
				String faces[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six",
						 "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
				  String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" };

				deck=new Card[52];
				shuffle();

				for ( int cardCount = 0; cardCount < index; cardCount++ )
				{
				deck[ cardCount ] = new Card( faces[ cardCount % 13 ], suits[ cardCount / 13 ]);
			}
				int cardCount = 0;



		}
	

					 public void shuffle()
					 {
						 Random generator = new Random();
						 int deck[],temp;

						   for ( int index = 51; index <= SWAPS; index++ )


public class Card
{
   public final static int ACE   = 1;
   public final static int TWO   = 2;
   public final static int THREE = 3;
   public final static int FOUR  = 4;
   public final static int FIVE  = 5;
   public final static int SIX   = 6;
   public final static int SEVEN = 7;
   public final static int EIGHT = 8;
   public final static int NINE  = 9;
   public final static int TEN   = 10;
   public final static int JACK  = 11;
   public final static int QUEEN = 12;
   public final static int KING  = 13;

   public final static int CLUBS	= 1;
   public final static int DIAMONDS = 2;
   public final static int HEARTS   = 3;
   public final static int SPADES   = 4;

   private final static int NUM_FACES = 13;
   private final static int NUM_SUITS = 4;

   private int face, suit;
   private String faceName, suitName;

 
   public Card ()
   {
	  face = (int) (Math.random() * NUM_FACES) + 1;
	  setFaceName();

	  suit = (int) (Math.random() * NUM_SUITS) + 1;
	  setSuitName();
   }

 
   public Card (int faceValue, int suitValue)
   {
	  face = faceValue;
	  setFaceName();

	  suit = suitValue;
	  setSuitName();
   }

   
   private void setFaceName()
   {
	  switch (face)
	  {
		 case ACE:
			faceName = "Ace";
			break;
		 case TWO:
			faceName = "Two";
			break;
		 case THREE:
			faceName = "Three";
			break;
		 case FOUR:
			faceName = "Four";
			break;
		 case FIVE:
			faceName = "Five";
			break;
		 case SIX:
			faceName = "Six";
			break;
		 case SEVEN:
			faceName = "Seven";
			break;
		 case EIGHT:
			faceName = "Eight";
			break;
		 case NINE:
			faceName = "Nine";
			break;
		 case TEN:
			faceName = "Ten";
			break;
		 case JACK:
			faceName = "Jack";
			break;
		 case QUEEN:
			faceName = "Queen";
			break;
		 case KING:
			faceName = "King";
			break;
	  }
   }


   private void setSuitName()
   {
	  switch (suit)
	  {
		 case CLUBS:
			suitName = "Clubs";
			break;
		 case DIAMONDS:
			suitName = "Diamonds";
			break;
		 case HEARTS:
			suitName = "Hearts";
			break;
		 case SPADES:
			suitName = "Spades";
			break;
	  }
   }

 
   public boolean isHigherThan (Card card2, boolean aceHigh)
   {
	  boolean result = false;

	  if (face == card2.getFace())
	  {
		 if (suit > card2.getSuit())
			result = true;
	  }
	  else
	  {
		 if (aceHigh && face == ACE)
			result = true;
		 else
			if (face > card2.getFace())
			   result = true;
	  }

	  return result;
   }


   public boolean isHigherThan (Card card2)
   {
	  return isHigherThan (card2, true);
   }


   public int getFace ()
   {
	  return face;
   }

 
   public int getSuit ()
   {
	  return suit;
   }


   public String getFaceName ()
   {
	  return faceName;
   }


   public String getSuitName ()
   {
	  return suitName;
   }

  
   public String toString ()
   {
	  return faceName + " of " + suitName;
   }
}


Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Shuffling deck of cards

Posted 18 April 2009 - 03:31 PM

Asked so many times... I wrote a snippet about it.
Click on "My contributions" under my name
There is a Deck shuffle class there

Topic title changed to me more descriptive

This post has been edited by pbl: 18 April 2009 - 03:32 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1