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

New Topic/Question
Reply




MultiQuote





|