1 Replies - 17711 Views - Last Post: 16 February 2009 - 03:35 PM Rate Topic: -----

#1 confuzzeld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 30-September 08

dealing a deck of cards

Post icon  Posted 16 February 2009 - 01:13 PM

hi guys, im in need of some help with this program i have going. there are 2 problems i am having.
1) i cant get my deal cards working right
2) i need to know how to print my methods out

here is my code:
card class

import java.util.*;
import javax.swing.JOptionPane;
/**
 * Determines the suit and rank of the card
 
 */
public class Card
{
		public int face; // the card's face value, between 1 and 13 inclusive
		public int suit; // the card's suit; one of the class constants

		// class constants for the suits
		public static final int SPADES = 1;
		public static final int HEARTS = 2;
		public static final int CLUBS = 3;
		public static final int DIAMONDS = 4;
		
		/**
		 * Constructs a Card object with the given face value and suit value.
		 * The face value of the card is set to 1 if an invalid face value is
		 * given; the suit of the card is set to HEARTS if an invalid suit is
		 * given.
		 *
		 * @param face  the face value of the card
		 * @param suit  the suit of the card
		 */
		public Card(int face, int suit) 
		{
				this.face = face;
				if (face < 1 || face > 13) {
						errorFaceValue();
						this.face = 1;
				}

				this.suit = suit;
				if (suit != HEARTS && suit != DIAMONDS && suit != CLUBS
								&& suit != SPADES) {
						errorSuitValue();
						this.suit = HEARTS;
				}
		}
		/**
		 * Prints an error message if the face value is invalid
		 */
		public static void errorFaceValue()
		{
			System.out.println("Please enter in valid face value\nMust be within 1 - 13");
		}
		/**
		 * Prints an error message if the suit value is invalid
		 */
		public static void errorSuitValue()
		{
			JOptionPane.showMessageDialog(null, "Please enter in valid Suit value, Must be within 1 - 4", "Invalid Suit Value Error", JOptionPane.ERROR_MESSAGE);
		}

		/**
		 * Returns the face value of the card.
		 *
		 * @return face value of the card (between 1 and 13, inclusive)
		 */
		public int getFace() {
				return face;
		}

		/**
		 * Returns the suit of the card.
		 *
		 * @return suit of the card (HEARTS, DIAMONDS, CLUBS, or SPADES)
		 */
		public int getSuit() {
				return suit;
		}

		/**
		 * Returns a String representation of a Card object. The string has the
		 * format "ffs" where ff is the face value (padded with a space, if
		 * necessary) and s is the suit.
		 *
		 * @return a String representation of a Card object
		 */
		public String toString() 
		{
				String value = "";
				switch (face) {
						case 1:		 value += " Ace of";
										break;
						case 2:		 value += " Two of";
										break;
						case 3:		 value += " Three of";
										break;
						case 4:		 value += " Four of";
										break;
						case 5:		 value += " Five of";
										break;
						case 6:		 value += " Six of";
										break;
						case 7:		 value += " Seven of";
										break;
						case 8:		 value += " Eight of";
										break;
						case 9:		 value += " Nine of";
										break;
						case 10:		value += " Ten of";
										break;
						case 11:		value += " Jack of";
										break;
						case 12:		value += " Queen of";
										break;
						case 13:		value += " King of";
										break;
						default:		value += " " + face;
										break;
				}

				switch (suit) {
						case HEARTS:	value += " Hearts";
										break;
						case DIAMONDS:  value += " Diamonds";
										break;
						case CLUBS:	 value += " Clubs";
										break;
						case SPADES:	value += " Spades";
										break;
				}

				return value;
		}
		
//		 public boolean equals(Card Card)
//		 {
//			 return (this.value.equals(Card.value));
//		 }
}


deck class
import javax.swing.JOptionPane;
import java.util.*;
/**
 * Write a description of class Deck here.
 * 
 */
public class Deck
{
		private static Random r = new Random();
		ArrayList <Card> cards = new ArrayList <Card>();
		Scanner kbd = new Scanner(System.in);
		
		public Deck() 
		{
			createCards();
			for(Card c: cards) 
			{
//				System.out.println(c.toString());
			}

		}
		
		public void createCards()
		{
			for (int i = 0; i < 13; i++) 
			{
				cards.add(new Card(i+1, Card.SPADES));
			}
			for (int i = 0; i < 13; i++) 
			{
				cards.add(new Card(i+1, Card.HEARTS));
			}
			for (int i = 0; i < 13; i++) 
			{
				cards.add(new Card(i+1, Card.CLUBS));
			}
			for (int i = 0; i < 13; i++) 
			{
				cards.add(new Card(i+1, Card.DIAMONDS));
			}
		}
		
		public void printCard(Card c)
		{
			
		}
		public void printDeck()
		{
			for(Card c: cards) 
			{
				System.out.println(c.toString());
			}
		}
		
		public void reset()
		{
			cards.clear();
			createCards();
			printDeck();
		}
		public void shuffle()
		{
			Collections.shuffle(cards);
			System.out.println(cards);
		}

		public Card drawTopCard()
		{
			return cards.remove(0);
		}

		public void cutDeck()
		{
			 int temp = r.nextInt(51);
			 int count = 0;
			 for(int i = temp; i < 51; i++)
			 {
				 cards.add(0 + count, cards.remove(i));
				 count++;
			 }

		}
		/**
		 * Prints an error message if there is a invalid input of number of cards for a hand
		 */
		public static void invalidCardError()
		{
			JOptionPane.showMessageDialog(null, "Please enter in valid Number of Cards, Must be within 1 - 26", "Invalid number Error", JOptionPane.ERROR_MESSAGE);
		}
		/**
		 * Prompts user to enter in Number of Cards to be dealt
		 * 
		 */
		public void deal()
		{
			String howMany = "";
			int number = Integer.parseInt(howMany);
//			
			if(!(number > 0 && number < 52))
			{
				invalidCardError();
				howMany = JOptionPane.showInputDialog("How many cards would you like? ");
			}
			else
						   
			for(int i = 0; i < number; i++)
			{
				drawTopCard();
			}
//		return 
		}


}


mainProgram class
import javax.swing.JOptionPane;
import java.util.*;
/**
 * Write a description of class mainMenu here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MainProgram
{
	public static String welcomeMessage()
	{
		String input = "";				
		input = JOptionPane.showInputDialog("Welcome To The Card Game \n Please enter you choice: \n N - New Deck\n S - Shuffle Deck\n T - Top Card\n C - Cut the Deck\n D - Deal Hands\n Q - Quit");
		return input;
	}
	public static void main(String[] args)
	{
		String input = "";
		Deck deck = new Deck();
		input = welcomeMessage();

		if(input.equalsIgnoreCase("N"))
		{
			deck.reset();
		}
		else if (input.equalsIgnoreCase("S"))
		{
			deck.shuffle();
		}
		else if (input.equalsIgnoreCase("T"))
		{
		   deck.drawTopCard();
		}
		else if (input.equalsIgnoreCase("C"))
		{
		   deck.cutDeck();
		}
		else if (input.equalsIgnoreCase("D"))
		{
			String howMany = "";
//			ArrayList <Card> deal throws Exception{};
//			throw new Exception("There are not enough cards in the deck to deal this hand.");
			
			howMany = JOptionPane.showInputDialog("How many cards would you like? ");
			try{int number = Integer.parseInt(howMany);
				Deck.deal();
			}
			catch(Exception e){ 
				JOptionPane.showMessageDialog(null, e.getMessage(Deck.invalidCardError())); 
			}			
		}

		else if (input.equalsIgnoreCase("Q"))
		{
			
		}
		else
			input = JOptionPane.showInputDialog("Welcome To The Card Game \n Please enter you choice: \n N - New Deck\n S - Shuffle Deck\n T - Top Card\n C - Cut the Deck\n D - Deal Hands\n Q - Quit");
			
	}
	public static void isThereAnythingElse()
	{
		int selection = JOptionPane.showOptionDialog(null, "Is there anything else you would like to do?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
		if (selection == JOptionPane.YES_OPTION)
		{
			welcomeMessage();
		}
		else{
			System.exit(0);
		}
	}
}



Please help me i've been struggling for days with this

Thanks in advance

This post has been edited by confuzzeld: 16 February 2009 - 01:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: dealing a deck of cards

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5940
  • View blog
  • Posts: 12,868
  • Joined: 16-October 07

Re: dealing a deck of cards

Posted 16 February 2009 - 03:35 PM

View Postconfuzzeld, on 16 Feb, 2009 - 02:13 PM, said:

1) i cant get my deal cards working right


Your drawTopCard looks fine, though you can protect it:
public Card drawTopCard() {
	if (cards.isEmpty()) { return null; }
	return cards.remove(0);
}



When you deal:
for(int i = 0; i < number; i++) {
	// this is all fine
	// you're drawing cards
	// but what the hell are you doing with them
	//drawTopCard();
	Card c = drawTopCard();
	// now what?  You have a card.  
	// What do you do with it?  Deal to where?
}



View Postconfuzzeld, on 16 Feb, 2009 - 02:13 PM, said:

2) i need to know how to print my methods out


Your card implements toString. Your code even makes use of it here:
public void printDeck() {
	for(Card c: cards) {
		 System.out.println(c.toString());
	}
}



If you got that far, then this should be obvious:
public void printCard(Card c)
	System.out.println(c.toString());
}



I suspect this isn't your code. If you understood what you're already doing, you wouldn't have asked that last question. Please try to understand what you have already.

Good luck.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1