Cards Game Java Program
Java Code:
public class card {
public final static int SPADES = 0; // Codes for the 4 suits
public final static int HEARTS = 1;
public final static int DIAMONDS = 2;
public final static int CLUBS = 3;
public final static int ACE = 1; // Codes for the non-numeric cards.
public final static int JACK = 11; //
Cards 2 through 10 have their
public final static int QUEEN = 12; //
numerical values for their codes.
public final static int KING = 13;
private final int suit;
private final int value;
public card() {
suit = SPADES;
value = 1;
}
public card(int theValue, int theSuit) {
value = theValue;
suit = theSuit;
}
public String
getSuitAsString() {
switch ( suit ) {
case SPADES: return "Spades";
case HEARTS: return "Hearts";
case DIAMONDS: return "Diamonds";
case CLUBS: return "Clubs";
default: return "spades";
}
}
public String
getValueAsString() {
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 getValueAsString() +
"
of "
+ getSuitAsString();
}
}
public class Deck {
private card[] deck;
private int cardsUsed;
public Deck() {
this(false); // Just call the other constructor in this
class.
}
public Deck(boolean include) {
deck = new card[52];
int cardCt = 0; // How many cards
have been created so far.
for ( int suit = 0; suit <=
3; suit++ ) {
for ( int value = 1; value <= 13; value++ ) {
deck[cardCt] = new card(value,suit);
cardCt++;
}
}
cardsUsed = 0;
}
public void shuffle() {
for ( int i = deck.length-1; i > 0; i-- ) {
int rand = (int)(Math.random()*(i+1));
card temp = deck[i];
deck[i] = deck[rand];
deck[rand] = temp;
}
cardsUsed = 0;
}
public card dealCard() {
cardsUsed++;
return deck[cardsUsed - 1];
}
}
import java.util.ArrayList;
import java.util.Arrays;
public class CardPlayer {
private ArrayList Hand; // The cards in the hand.
public CardPlayer() {
Hand = new ArrayList();
}
public void addCard(card c) {
Hand.add(c);
}
public void removeCard(card c) {
Hand.remove(c);
}
public int display() {
for(int i=0; i<Hand.size();i++){
System.out.print(Hand.get(i)+ ",");
}
return 0;
}
}
public class main {
public static void main(String[]
args) {
Deck deck = new Deck(); // Get a new deck of cards, and
CardPlayer cp=new CardPlayer();
card firstCard; // The card, which
the user.
card secondCard;
card thirdCard;
card forthCard;
card fifthCard;
deck.shuffle(); // Shuffle the deck
firstCard = deck.dealCard();
System.out.println("The first card is the " + firstCard);
cp.addCard(firstCard);
secondCard = deck.dealCard();
System.out.println("The second card is the " + secondCard);
cp.addCard(secondCard);
thirdCard = deck.dealCard();
System.out.println("The third card is the " + thirdCard);
cp.addCard(thirdCard);
forthCard = deck.dealCard();
System.out.println("The forth card is the " + forthCard);
cp.addCard(forthCard);
fifthCard = deck.dealCard();
System.out.println("The fifth card is the " + fifthCard);
cp.addCard(fifthCard);
System.out.println(cp.display());
}
}
No comments:
Post a Comment