4 Replies - 268 Views - Last Post: 14 October 2011 - 01:16 PM Rate Topic: -----

#1 mike28  Icon User is offline

  • D.I.C Head

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

Problem printing the card's value and suit

Posted 14 October 2011 - 09:23 AM

[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()
	{
		face = 1;
		value = 1;
		shuffleValue();
		shuffleFace();
		//pick();
	}

	public Card(int f, int v)
	{
		face = f;
		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 String toString()
	{
		return "Value: " +getValue()+ ", Face: " +getFace();
	}
}]

[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.

    public Deck() {
           // 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(value,face);
             cardCt++;

          }
          System.out.println(Collection[cardCt]);
       }
       cardsUsed = 0;
       
    }

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

[import java.lang.*;
import java.io.*;
public class Table
{
	public static void main(String[] args)
	{

	Deck[] deck = new Deck[25];
	

	}
}]

This post has been edited by modi123_1: 14 October 2011 - 10:57 AM
Reason for edit:: please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Problem printing the card's value and suit

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Problem printing the card's value and suit

Posted 14 October 2011 - 10:41 AM

Please :code:

what errors are you getting? What way do you want it to print?
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 printing the card's value and suit

Posted 14 October 2011 - 10:47 AM

I dont get an error i just want to print the whole deck which is 52 cards

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

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Problem printing the card's value and suit

Posted 14 October 2011 - 10:50 AM

make a for loop to cycle through the array and call each objects toString method.

ie. System.out.println(Deck[x].toString());
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 printing the card's value and suit

Posted 14 October 2011 - 01:16 PM

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

Page 1 of 1