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]);
}
}
}

New Topic/Question
Reply



MultiQuote






|