9 Replies - 547 Views - Last Post: 16 October 2011 - 06:08 PM Rate Topic: -----

#1 mike28  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 03-October 11

Problem displaying the 52 card in deck without repeating the same num

Posted 15 October 2011 - 04:39 PM

Hi

i have Problem displaying 52 cards without repeating the same cards!
the Program does print out 52 card but but most of the cards are the same

Thank You
public class Card
{
	//hearts, diamonds, clubs or spades
	public static final int HEARTS = 0;
	public static final int DIAMONDS = 1;
	public static final int CLUBS = 2;
	public static final int SPADES = 3;
	//the card has a value (2..10, J, Q, K, A)
	public static final int Ace = 0;
	public static final int Two = 1;
	public static final int Three = 2;
	public static final int Four = 3;
	public static final int Five = 4;
	public static final int Six = 5;
	public static final int Seven = 6;
	public static final int Eight = 7;
	public static final int Nine = 8;
	public static final int Ten = 9;
	public static final int J = 11;
	public static final int Q = 12;
	public static final int K = 13;

	public int face=1;
	public int value=1;

	public Card()
	{
		this.face = 1;
		this.value = 1;
		shuffleValue();
		shuffleFace();
		//pick();
	}

	public Card(int f, int v)
	{
		this.face = f;
		this.value = v;
		shuffleValue();
		shuffleFace();
		//pick();
	}

	public void shuffleFace()
	{
		face = (int)(Math.random()*4);
	}

	public void shuffleValue()
	{
		value = (int)(Math.random()*14);
	}

	public void pick()
	{
		shuffleFace();
		shuffleValue();
	}

	public String  getValue()
	{
		if (value == Ace)
			return "Ace";
		else if (value == Two)
			return "Two";
		else if (value == Three)
			return "Three";
		else if (value == Four)
			return "Five";
		else if (value == Six)
			return "Six";
		else if (value == Seven)
			return "Seven";
		else if (value == Eight)
			return "Eight";
		else if (value == Nine)
			return "Nine";
		else if (value == Ten)
			return "Ten";
		else if (value == J)
			return "Jack";
		else if (value == Q)
			return "Queen";
		else
			return "King";
	}

	public String getFace()
	{
		if (face == HEARTS)
			return "HEARTS";
		else if (face == DIAMONDS)
			return "DIAMONDS";
		else if (face == CLUBS)
			return "CLUBS";
		else
			return "SPADES";
	}

	public int compareTo(Object rhs)
	{
    Card card = (Card) rhs;
    if (this.value < card.value)
    {
      return -1;
    }
    else if (this.value > card.value)
    {
      return 1;
    }
    else { //== values
      if (this.face < card.face)
      {
        return -1;
      }
      else if (this.face > card.face)
      {
        return 1;
      }
      else
      {
        return 0;
      }
    }
    }
	public String toString()
	{
		return "Value: " +getValue()+ ", Face: " +getFace();
	}
}


import java.util.Random;
public class Deck extends Card
{
 public Card[] Collection;   // An array of 52 Cards, representing the deck.
 public int cardsUsed; // How many cards have been dealt from the deck.
 int i;
 public Deck()
 {
	i = 52;
 	face = 1;
	value = 1;
	shuffleValue();
	shuffleFace();

	 // Create an unshuffled deck of cards.
    Collection = new Card[52];
    int cardCt = 0; // How many cards have been created so far.
    for ( int face = 0; face <= 3; face++ )
    {

       for ( int value = 1; value <= 13; value++ )
       {
		   Collection[cardCt] = new Card(face,value);
           cardCt++;

       }
	 }

   }

	public String toString()
	{
		String CardDescription = super.toString();
		return"Card "+CardDescription;
	}
}


import java.lang.*;
import java.lang.Object.*;
import java.util.Scanner;
import java.io.*;
import java.util.Random;
public class Table
{
	public static void main(String[] args)throws IOException
	{

	Deck[] deck = new Deck[52];


	System.out.println("Dealer's cards: ");
	for(int i = 0; i< 52; i++)
	{
		deck[i] = new Deck();
		System.out.println(deck[i]);
	}
}

}


Is This A Good Question/Topic? 0
  • +

Replies To: Problem displaying the 52 card in deck without repeating the same num

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Problem displaying the 52 card in deck without repeating the same num

Posted 15 October 2011 - 04:43 PM

Deck should not extend Card. You need a has-a relationship here, not an is-a relationship.

Also, this makes no sense. First, your Deck object shouldn't be storing other Decks. Second, you don't need a Deck[]- you have a Card[] in the Deck class. Lastly, the Deck class should populate the Card[], not a for loop in your main() method.
Deck[] deck = new Deck[52];
    System.out.println("Dealer's cards: ");
    for(int i = 0; i< 52; i++)
    {
        deck[i] = new Deck();


Was This Post Helpful? 0
  • +
  • -

#3 mike28  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 03-October 11

Re: Problem displaying the 52 card in deck without repeating the same num

Posted 15 October 2011 - 05:25 PM

thanks for the response but i dont really understand you
can you explain it in an easier way

thank you
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Problem displaying the 52 card in deck without repeating the same num

Posted 15 October 2011 - 05:56 PM

Here, Deck should not extend Card. It does not make sense. A Deck is not a Card. A Deck has Cards, hence the Card[] instance variable.
public class Deck extends Card 
{
    public Card[] Collection; 



In your main() method, you just need to instantiate a single Deck, not a Deck[]. Remember that a single Deck should have 52 cards in it. So you should not need 52 Decks.
Was This Post Helpful? 0
  • +
  • -

#5 mike28  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 03-October 11

Re: Problem displaying the 52 card in deck without repeating the same num

Posted 16 October 2011 - 11:24 AM

but it still doesn't work
please help

import java.util.Random;
public class Deck 
{
 public Card[] Collection;   // An array of 52 Cards, representing the deck.
 public int cardsUsed; // How many cards have been dealt from the deck.
 int i;
 public Deck()
 {
	i = 52;
 	

	 // Create an unshuffled deck of cards.
    Collection = new Card[52];
    int cardCt = 0; // How many cards have been created so far.
    for ( int face = 0; face <= 3; face++ )
    {

       for ( int value = 1; value <= 13; value++ )
       {
		   Collection[cardCt] = new Card(face,value);
           cardCt++;

       }
	 }

   }

	public String toString()
	{
		String CardDescription = super.toString();
		return"Card "+CardDescription;
	}
}


Was This Post Helpful? 0
  • +
  • -

#6 mike28  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 03-October 11

Re: Problem displaying the 52 card in deck without repeating the same num

Posted 16 October 2011 - 11:43 AM

I am posting the code again because i changed it
But my Program still repeated the same cars maybe i need to use the compareTo() which located in the class Card , but i dont know how

public class Card
{
	//hearts, diamonds, clubs or spades
	public static final int HEARTS = 0;
	public static final int DIAMONDS = 1;
	public static final int CLUBS = 2;
	public static final int SPADES = 3;
	//the card has a value (2..10, J, Q, K, A)
	public static final int Ace = 0;
	public static final int Two = 1;
	public static final int Three = 2;
	public static final int Four = 3;
	public static final int Five = 4;
	public static final int Six = 5;
	public static final int Seven = 6;
	public static final int Eight = 7;
	public static final int Nine = 8;
	public static final int Ten = 9;
	public static final int J = 11;
	public static final int Q = 12;
	public static final int K = 13;

	public int face=1;
	public int value=1;

	public Card()
	{
		this.face = 1;
		this.value = 1;
		shuffleValue();
		shuffleFace();
		//pick();
	}

	public Card(int f, int v)
	{
		this.face = f;
		this.value = v;
		shuffleValue();
		shuffleFace();
		//pick();
	}

	public void shuffleFace()
	{
		face = (int)(Math.random()*4);
	}

	public void shuffleValue()
	{
		value = (int)(Math.random()*14);
	}

	public void pick()
	{
		shuffleFace();
		shuffleValue();
	}

	public String  getValue()
	{
		if (value == Ace)
			return "Ace";
		else if (value == Two)
			return "Two";
		else if (value == Three)
			return "Three";
		else if (value == Four)
			return "Five";
		else if (value == Six)
			return "Six";
		else if (value == Seven)
			return "Seven";
		else if (value == Eight)
			return "Eight";
		else if (value == Nine)
			return "Nine";
		else if (value == Ten)
			return "Ten";
		else if (value == J)
			return "Jack";
		else if (value == Q)
			return "Queen";
		else
			return "King";
	}

	public String getFace()
	{
		if (face == HEARTS)
			return "HEARTS";
		else if (face == DIAMONDS)
			return "DIAMONDS";
		else if (face == CLUBS)
			return "CLUBS";
		else
			return "SPADES";
	}

	public int compareTo(Object rhs)
	{
    Card card = (Card) rhs;
    if (this.value < card.value)
    {
      return -1;
    }
    else if (this.value > card.value)
    {
      return 1;
    }
    else { //== values
      if (this.face < card.face)
      {
        return -1;
      }
      else if (this.face > card.face)
      {
        return 1;
      }
      else
      {
        return 0;
      }
    }
    }
	public String toString()
	{
		return "Value: " +getValue()+ ", Face: " +getFace();
	}
}


import java.util.Vector;
/**
 * This class will represent an array which shall act as a deck of 52 cards.
 *
 * @author Chad Wood
 * 3/10/2007
 */
public class Deck
{
	private Card[] Collection;
	private int cardsDealt;
	public Deck()
	   {
	   Collection = new Card[52];
	   int cardCounter = 0;
	   for ( int face = 0; face <= 3; face++ )
	   {
		  for ( int value = 1; value <= 13; value++ )
		  {
			 Collection[cardCounter] = new Card(value,face);
			 cardCounter++;
		  }
	   }
	   cardsDealt = 0;
	}

	public void shuffle()
	{
		for ( int i = 51; i > 0; i-- )
		{
			int random = (int)(Math.random()*(i+1));
			Card temp = Collection[i];
			Collection[i] = Collection[random];
			Collection[random] = temp;
			System.out.println(temp);
		}
		cardsDealt = 0;
	}
	public int cardsRemaining()
	{
	   return 52 - cardsDealt;
	}
	public Card dealCard()
	{
		if (cardsDealt == 52)
		   shuffle();
		cardsDealt++;
		return Collection[cardsDealt - 1];
	}
}


import java.lang.*;
import java.lang.Object.*;
import java.util.Scanner;
import java.io.*;
import java.util.Random;
public class Table
{
	public static void main(String[] args)throws IOException
	{

	Deck deck = new Deck();

	deck.shuffle();
        }
}

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

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

Re: Problem displaying the 52 card in deck without repeating the same num

Posted 16 October 2011 - 11:48 AM

You do not need to shuffle your Deck of cards
Simply remove randomly from it

http://www.dreaminco...snippet3176.htm
Was This Post Helpful? 0
  • +
  • -

#8 mike28  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 03-October 11

Re: Problem displaying the 52 card in deck without repeating the same num

Posted 16 October 2011 - 04:49 PM

Thank you guys i think i fixed the code and it works but my question is how do i print 2 cards for the dealer and 2 cards for the player

thank you

public class Card
{
	private final int value;
	private final int face;
//hearts, diamonds, clubs or spades
	public static final int HEARTS = 0;
	public static final int DIAMONDS = 1;
	public static final int CLUBS = 2;
	public static final int SPADES = 3;
	//the card has a value (2..10, J, Q, K, A)
	public static final int Ace = 1;
	public static final int Two = 2;
	public static final int Three = 3;
	public static final int Four = 4;
	public static final int Five = 5;
	public static final int Six = 6;
	public static final int Seven = 7;
	public static final int Eight = 8;
	public static final int Nine = 9;
	public static final int Ten = 10;
	public static final int J = 11;
	public static final int Q = 12;
	public static final int K = 13;
	public Card(int f, int v)
	{
		this.face = f;
		this.value = v;
	}
	public int Face()
	{
		return face;
	}
	public int Value()
	{
		return value;
	}

public String  getValue()
	{
		if (value == Ace)
			return "Ace";
		else if (value == Two)
			return "Two";
		else if (value == Three)
			return "Three";
		else if (value == Four)
			return "Four";
		else if (value == Five)
			return "Five";
		else if (value == Six)
			return "Six";
		else if (value == Seven)
			return "Seven";
		else if (value == Eight)
			return "Eight";
		else if (value == Nine)
			return "Nine";
		else if (value == Ten)
			return "Ten";
		else if (value == J)
			return "Jack";
		else if (value == Q)
			return "Queen";
		else if (value == K)
			return "King";
		else
			return "error";

	}
	 public String getFace()
	{
		if (face == HEARTS)
			return "HEARTS";
		else if (face == DIAMONDS)
			return "DIAMONDS";
		else if (face == CLUBS)
			return "CLUBS";
		else if (face == SPADES)
			return "SPADES";
		else
			return "error";
	}



	public String toString()
	{
		return "Value: " +getValue()+ ", Face: " +getFace();
	}


}



import java.util.Vector;
public class Deck
{
	private Card[] deck;
	private int cardsDealt;
	private Vector hand;
	public Deck()
	   {
	   hand = new Vector();
	   deck = new Card[52];
	   int cardCounter = 0;
	   for ( int face = 0; face <= 3; face++ )
	   {
		  for ( int value = 1; value <= 13; value++ )
		  {
			 deck[cardCounter] = new Card(face,value);
			 cardCounter++;

		  }
	   }
	   cardsDealt = 0;
	}
	///////////////////////////////////////////////////


	public void shuffle()
	{
		for ( int i = 51; i > 0; i-- )
		{
			int random = (int)(Math.random()*(i+1));
			Card temp = deck[i];
			deck[i] = deck[random];
			deck[random] = temp;
		}
		cardsDealt = 0;
	}
	public void print()
	{
		for(int i =0; i<52;i++)
		System.out.println(deck[i]);
	}

	public int cardsRemaining()
	{
	   return 52 - cardsDealt;
	}
	public Card dealCard()
	{
		if (cardsDealt == 52)
		   shuffle();
		cardsDealt++;
		return deck[cardsDealt - 1];
	}
}



import java.lang.*;
import java.lang.Object.*;
import java.util.Scanner;
import java.io.*;
import java.util.Random;
public class Table
{
	public static void main(String[] args)throws IOException
	{

	Deck deck = new Deck();
deck.shuffle();
deck.print();
}}

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

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

Re: Problem displaying the 52 card in deck without repeating the same num

Posted 16 October 2011 - 04:52 PM

you call dealCard() twice for the dealer and twice for the player ????????????
Was This Post Helpful? 0
  • +
  • -

#10 mike28  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 03-October 11

Re: Problem displaying the 52 card in deck without repeating the same num

Posted 16 October 2011 - 06:08 PM

Thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1