Can someone please take a look at this for me. I have to create a program that models a simple card game. I have to shuffle the card and deal 1 card at a time and display the card that I have dealt. Not sure how to do this. The code I have is returning 00. I also have to declair a winner. I guess I need to do a nested for loop to check against suit and face of card right.(havent tried it yet). Looking for suggestions or directions in the correct place.
Thanks.
cassyjack 0 Light Poster
/**
* Write a description of class Card here.
*
* @author ()
* @version (test)
*/
public class Card
{
// instance variables - replace the example below with your own
public static final int SPADE = 0;
public static final int HEART = 1;
public static final int DIAMOND = 2;
public static final int CLUB = 3;
public static int suit;//should be private
public static int face;// should be private cards number 2-10 Ace = 1, jack = 11, queen = 12, king = 13
/**
* Constructor for objects of class Card
*/
public Card(int f, int s)
{
// initialise instance variables
face = f;
suit = SPADE;
}
//No Arg Constructor
public Card()
{
// initialise instance variables
face = 1;
suit = 0;
}
/**
* Allows changes to suit and value
*
* @param newS Suit and param newV new value
* @return return suit and value
*/
public int getSuit()
{
// put your code here
return suit;
}
public int getFace()
{
// put your code here
return face;
}
public void setSuit(int newS)
{
suit = newS;
}
public void setFace(int newF)
{
face = newF;
}
/**
* toString method
*
*/
public String toString()
{
String suitAsString = "Spade";
if (suit == HEART)
suitAsString = "Heart";
else if (suit == DIAMOND)
suitAsString = "Diamond";
else if (suit == CLUB)
suitAsString ="Club";
return ("Card " + face + suit);
}
/**
* Checks to see if cards are equasl
* cards are equal when face and suit are the same.
*/
// public boolean equals(Card yourCard)
//{
// if your
// return
//}
}
import java.util.*;
/**
* Write a description of class Deck here.
*
* @author ()
* @version (test)
*/
public class Deck
{
private Card[] myDeck;//mabe 51 since counting starts at 0
private int dealt;//card that is dealt
// private int numOfFace;
// private int numOfSuits;
/**
* Constructor for objects of class Card
*/
public Deck(int size)
{
// initialise instance variables
myDeck = new Card[size];
// numOfFace = 13;
// numOfSuits = 4;
// numOfCards = 0;
}
//No Arg Constructor
public Deck()
{
// initialise instance variablesCard[] myDeck = new Card[52];
myDeck = new Card[52];
int numCard =0;
for (int suit = 0; suit <=3; suit++){
for (int face = 1; face <=12; face++){
myDeck[numCard] = new Card(face, suit);
numCard++;
}
}
dealt = 0;
}
/**
* Methods to Deal, Shuffle and Equals method to determine if two decks
* are equal
*/
public Card deal(){
if(dealt == 52)
shuffle();
dealt++;
return myDeck[dealt -1];
}
public void shuffle()
{
Collections.shuffle(Arrays.asList(myDeck));
}
/** public boolean equals(Deck yourDeck)
{
return (suit = yourDeck.getSuit() && face= yourDeck.getFace());
}
*/
}
import javax.swing.*;
import java.util.*;
/**
* Write a description of class Driver here.
*
* @author ()
* @version (test)
*/
public class Driver
{
//Variables for Driver class
private static Deck inDeck;
/**
* The main method of the program. This program
*/
public static void main(String[]arg)
{
inDeck = new Deck(52);
JOptionPane.showMessageDialog(null, "Lets Play Some Cards", "Card Game", JOptionPane.PLAIN_MESSAGE);
//instantiate the deck of cards
playCards();
//if statement that calls a task on the choice
System.exit(0);
}
private static void playCards()
{
Object[] choices = {"Shuffle", "Deal 1st", "Deal 2nd","Quit"};
Object selected = JOptionPane.showInputDialog(null, "Please Select Shuffle to Start Game, Then Deal 1st and 2nd Cards",
"Lets Play",JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
if
(choices[0].equals((String) selected)){
inDeck.shuffle();
playCards();}
else if
(choices[1].equals((String) selected))
{
inDeck.deal();
int card = Card.suit + Card.face;
JOptionPane.showMessageDialog(null, "Your Card: " + Card.face + Card.suit, "Card Game", JOptionPane.PLAIN_MESSAGE);
playCards();
}
else if
(choices[2].equals((String) selected)){
inDeck.deal();
JOptionPane.showMessageDialog(null, "Your Card: " + Card.face + Card.suit, "Card Game", JOptionPane.PLAIN_MESSAGE);}
// winner();
else if
(choices[3].equals((String) selected))
System.exit(0);
}
// private void winner()
// {}
}
peter_budo 2,532 Code tags enforcer Team Colleague Featured Poster
I did few small changes to your files
CARDS change in card constructor
public Card(int f, int s)
{
// initialise instance variables
face = f;
suit = s; //you had suit = SPADE
}
DECK change your constructor public Deck() (with 2 for loops) to a method to initialize cards, I call it public void setCards()
DRIVER use the new method setCards() before you suffle cards
if (choices[0].equals((String) selected))
{
inDeck.setCards();
inDeck.shuffle();
System.out.println("card mixed");
playCards();}
I would create random function to get my set of cards and check if these cards hasn't been dealt. So dealt cards should be store somewhere :?:
cassyjack 0 Light Poster
I'm now able to see the card. But it will not display different cards. I keep getting 12 3
peter_budo 2,532 Code tags enforcer Team Colleague Featured Poster
Did you tried a random function as I sugested???
cassyjack 0 Light Poster
Not sure what you mean by creating a random fucntion. I moved the intialization of dealt to the deal method. Is it because I needs some kind of equals function?
peter_budo 2,532 Code tags enforcer Team Colleague Featured Poster
I think I poineted you in wrong direction, you don't need random function that is useful dice games. Sorry my mistake. ;)
But you may want to create another array of type card to store data about cards which been used and should not be dealth again
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.