Contents
- ./Card.java
- ./Deal.java
- ./Deck.java
./Card.java 1/3
[top][prev][next]
package cards;
/**
* Demonstrates use of "enum"
*/
public class Card {
/**
* Represents the ranks in a deck of playing cards
*
*/
public enum Rank {
DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE;
}
/**
* Represents the suits in a deck of playing cards
*
*/
public enum Suit {
CLUBS, DIAMONDS, HEARTS, SPADES;
}
private final Rank rank;
private final Suit suit;
public Card(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
public Rank rank() {
return rank;
}
public Suit suit() {
return suit;
}
/**
* 10, J, Q, K: 10 points A: 15 points all others: 5 points
*
* @return the value of the card in the game of Rummy
*/
public int getRummyValue() {
int rummyValue = 0;
switch (rank) {
case ACE:
rummyValue = 15;
break;
case KING:
case QUEEN:
case JACK:
case TEN:
rummyValue = 10;
break;
default:
rummyValue = 5;
}
return rummyValue;
}
/**
*
* @param c
* another Card to compare
* @return true iff the cards have the same suit
*/
public boolean sameSuit(Card c) {
return c.suit.equals(suit);
// return c.suit == suit; // also works because they're enums
}
/**
* leverages toString() methods of Rank and Suit
*/
public String toString() {
return rank + " of " + suit;
}
public static void main(String args[]) {
Card jackOfDiamonds = new Card(Rank.JACK, Suit.DIAMONDS);
Card aceOfDiamonds = new Card(Rank.ACE, Suit.DIAMONDS);
System.out.println(jackOfDiamonds);
if (jackOfDiamonds.sameSuit(aceOfDiamonds)) {
System.out.println(jackOfDiamonds + " and " + aceOfDiamonds
+ " are the same suit.");
}
System.out.println("The rummyValue of " + jackOfDiamonds + " is "
+ jackOfDiamonds.getRummyValue());
}
}
./Deal.java 2/3
[top][prev][next]
package cards;
import java.util.*;
/**
*
*/
class Deal {
/**
* A little different dealing than before... Does not specify the type of
* objects in the List. Doesn't need to--still works.
*
* @param deck
* @param n
* @return
*/
public static List dealHand(List deck, int n) {
int deckSize = deck.size();
List handView = deck.subList(deckSize - n, deckSize);
List hand = new ArrayList(handView);
handView.clear();
return hand;
}
public static void main(String[] args) {
int numHands = Integer.parseInt(args[0]);
int cardsPerHand = Integer.parseInt(args[1]);
// Make a normal 52-card deck.
String[] suit = new String[] { "Spades", "Hearts", "Diamonds", "Clubs" };
String[] rank = new String[] { "Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King" };
List<String> deck = new ArrayList<String>();
// initialize the deck
for (int i = 0; i < suit.length; i++)
for (int j = 0; j < rank.length; j++)
deck.add(rank[j] + " of " + suit[i]);
Collections.shuffle(deck);
for (int i = 0; i < numHands; i++)
System.out.println(dealHand(deck, cardsPerHand));
System.out.println("\n Changing decks ... \n");
List<Card> cardDeck = new ArrayList<Card>();
for (Card.Suit s : Card.Suit.values()) {
for (Card.Rank r : Card.Rank.values()) {
cardDeck.add( new Card(r, s) );
}
}
Collections.shuffle(cardDeck);
for (int i = 0; i < numHands; i++)
System.out.println(dealHand(cardDeck, cardsPerHand));
}
}
./Deck.java 3/3
[top][prev][next]
package cards;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Represents a deck of playing cards
*
* @author Sara Sprenkle and CS209
*
*/
public class Deck {
// define our variable as an interface variable, List.
// Note that only Card objects can be put into the list or taken out of the
// list.
/** a list of Card objects */
private List<Card> deck;
/**
* Creates a new, shuffled deck of cards
*/
public Deck() {
// Assign the List variable to the ArrayList implementation.
deck = new ArrayList<Card>();
restart(true);
}
/**
* Creates a new deck of cards
* @param isShuffled - true if the cards should be shuffled
*/
public Deck(boolean isShuffled) {
deck = new ArrayList<Card>();
restart(isShuffled);
}
/**
* Restart the deck from the beginning.
* @param isShuffled - true if the cards should be shuffled
*/
public void restart(boolean isShuffled) {
// removes all the Cards from the deck
deck.clear();
// Use the enums defined in the Card class
for (Card.Suit suit : Card.Suit.values()) {
for (Card.Rank rank : Card.Rank.values()) {
Card c = new Card(rank, suit);
deck.add(c);
}
}
if( isShuffled ) {
shuffle();
}
}
/**
* Display the contents of the deck.
*/
public void display() {
// System.out.println(deck);
for (Card c : deck) {
System.out.println(c);
}
}
/**
*
*/
public void shuffle() {
Collections.shuffle(deck);
}
public Card draw() {
return deck.remove(0);
}
public List<Card> deal(int numCards) {
List<Card> hand = new ArrayList<Card>();
for( int i=0; i < numCards; i++ ) {
hand.add(draw());
}
return hand;
}
/**
* @param args
*/
public static void main(String[] args) {
Deck d = new Deck();
d.display();
}
}
Generated by GNU enscript 1.6.4.