1 Replies - 668 Views - Last Post: 27 April 2010 - 12:03 AM Rate Topic: -----

#1 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 188
  • Joined: 25-March 10

Help with establishing a driver for a deck of cards program.

Posted 26 April 2010 - 11:19 PM

What i have in my cards class

public class cardsv3 {
		public final static int  CLUBS = 0, DIAMONDS = 1, HEARTS = 2, SPADES = 3, ACE = 1, JACK = 11, QUEEN = 12, KING = 13;
		private final int suit;
		private final int value;
	   
		public cardsv3 (int tVau, int tSt) {		   
			if (tSt != CLUBS && tSt != DIAMONDS && tSt != HEARTS && tSt!= SPADES)
				throw new IllegalArgumentException("Illegal playing card suit");
		      
		
			value = tVau;
			suit = tSt;
		}
		      
		public int getSuit() {
		   return suit;
		}
	   
		public int getValue() {
		   return value;
		}

		public String setSuit () {
			switch (suit) {
			case CLUBS:		return "Diamonds";
		   	case DIAMONDS:	return "Diamonds";
		   	case HEARTS:	return "Hearts";
		    default:		return "Spades";
	 	
		   	/*case CLUBS:    return "Clubs";
		   	default:       return "Joker";*/
		   	}
		}
	   
		public String setValue () {
		   switch ( value ) {
		   case 1:   return "Ace";
		   case 2:   return "2";
		   case 3:   return "3";
		   case 4:   return "4";
		   case 5:   return "5";
		   case 6:   return "6";
		   case 7:   return "7";
		   case 8:   return "8";
		   case 9:   return "9";
		   case 10:  return "10";
		   case 11:  return "Jack";
		   case 12:  return "Queen";
		   default:  return "King";
		   	}
	   }
	
	   public String toString() {
	         
		   return setValue() + " of " + setSuit();
	   }
}



my deck of cards class

import java.util.Random;
import java.util.ArrayList;

public class deckofcardsv3 {
	
	public final int MAXCards = 52;
	
	public final int CardInaStandardSuit = 13;
	public final int SuitsInaStandardDeck = 4;
	public final int CardsInaDeck = CardInaStandardSuit * SuitsInaStandardDeck;
	int drawn = 0;
	
	public static final int[] suitsCards = {0, 1, 2, 4};
	
	
	private ArrayList<cardsv3> deck = new ArrayList<cardsv3>();
	
	Random generator = new Random();
		
	public deckofcardsv3(){
		
	    for (int suit = 0; suit < CardsInaDeck; suit++) {
	        //loop through the values
	        for(int value = 1; value <= CardInaStandardSuit; value++ ) {
	          addCard(new cardsv3(value, suitsCards[suit]));
	        }
	      }
	    }
		
	  public void addCard(cardsv3 c) {
	    deck.add(c);
	  }

	  public cardsv3 getCard(int index) {
	    return (cardsv3)deck.get(index);
	  }

	  public int getSize() {
	    return deck.size();
	  }
	  
	  public void shuffle() {
		  
		  Random generator = new Random();

	        for(int i = 0; i < 100; i++)
	        {
	                int rand1 = generator.nextInt(52);
	                int rand2 = generator.nextInt(52);
	                cardsv3 buffer = null;
	                buffer = deck.get(rand1);
	                deck.add(rand1, deck.get(rand2));
	                deck.add(rand2, buffer);
	        }

	  }
	  

	  public cardsv3 dealCard() {
		
		  return deck.remove(deck.size()-1);	  }
	
	  public String toString() {
	    String mydeck = "";
	    for (int i = 1; i <= deck.size(); i++)
	      mydeck += "Card " + i + ": " + deck.get(i-1) + "\n";
	    
	    return mydeck;
	  }

}



my driver's class which seems to be giving me trouble

import java.util.Scanner;

public class cardsDriverV1 {

	public static void main(String[] args) {

		deckofcardsv3 drawit = new deckofcardsv3();
		drawit.shuffle();
		
		System.out.println ("Drawn Cards: ");
		System.out.println (drawit.toString());
		
	}
}


error message

Exception in thread "main" java.lang.IllegalArgumentException: That's not a legitimate playing card
	at Card.<init>(Card.java:12)
	at DeckOfCards.<init>(DeckOfCards.java:18)
	at DeckOfCardsDriver.main(DeckOfCardsDriver.java:9)


Is This A Good Question/Topic? 0
  • +

Replies To: Help with establishing a driver for a deck of cards program.

#2 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: Help with establishing a driver for a deck of cards program.

Posted 27 April 2010 - 12:03 AM

just curious but
public static final int[] suitsCards = {0, 1, 2, 4};


arnt your suits
CLUBS = 0, DIAMONDS = 1, HEARTS = 2, SPADES = 3


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1