|
|
|
|
Sample program that uses the Card class:
public class myProg { public static void main(String[] args) { Card c1 = new Card("Spades", "Ace"); Card c2 = new Card("Hearts", "10"); System.out.println( c1 ); System.out.println( c2 ); } } |
DEMO: demo/03-classes/12-card/
You will see output like this: Card@c72318e
I like to show you how to make the System.out.println( ) method output look nicer first...
|
Run the same program that uses the Card class:
public class myProg { public static void main(String[] args) { Card c1 = new Card("Spades", "Ace"); Card c2 = new Card("Hearts", "10"); System.out.println( c1 ); System.out.println( c2 ); } } |
DEMO: demo/03-classes/13-card
The output is now: Ace of Spades !
|
We must first define a class that represents a deck of cards...
|
|
|
|
|
Based on the properties and actions, the skeleton code for the DeckOfCards class is as follows:
public class DeckOfCards { private Card[] deck; // Hold 52 cards (to be created) private int dealPosition; // Location of the top card in deck DeckOfCards() // Constructor to make a deck of cards { .... } public void shuffle() // Shuffle this deck of cards { perform a shuffle on the deck of cards } public Card deal() // Deal the next card from this deck { returns the dealt card } } |
Constructor: making a deck of cards with 52 specific cards: (naive approach)
public class DeckOfCards { private Card[] deck; private int dealPosition; DeckOfCards() // Constructor to make a deck of cards { deck = new Card[52]; // Make a holder for 52 cards deck[0] = new Card( "Spades", "Ace"); deck[1] = new Card( "Spades", "2"); ... and so on (52 assignment statements !!!) (How can we code this easily ??) } ... } |
Constructor: making a deck of cards with 52 specific cards:
public class DeckOfCards { private Card[] deck; private int dealPosition; DeckOfCards() // Constructor to make a deck of cards { deck = new Card[52]; // Make a holder for 52 cards // Define 2 arrays with all the suits and ranks String[] suit = {"Spades", "Hearts", "Diamonds", "Clubs"}; String[] rank = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}; } } |
Constructor: making a deck of cards with 52 specific cards:
public class DeckOfCards { private Card[] deck; private int dealPosition; DeckOfCards() // Constructor to make a deck of cards { deck = new Card[52]; // Make a holder for 52 cards String[] suit = {"Spades", "Hearts", "Diamonds", "Clubs"}; String[] rank = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}; int k = 0; // Index into deck[ ] // Use a nested loop to make all 52 cards and assign to deck[ ] for ( int i = 0; i < suit.length; i++ ) for (int j = 0; j < rank.length; j++ ) { deck[k] = new Card( suit[i], rank[j] ); k++; } } } |
Before we write the shuffle() method, we need to discuss how to swap 2 variables:
public class MyProg
{
public static void main(String[] args)
{
int a = 1, b = 2;
// Code to swap values in a and b
// End result: a = 2, b = 1
}
}
|
Analogy to illustrate the swap programming technique:
The algorithm used to exchange the values in the variables a and b:
public class MyProg { public static void main(String[] args) { int a = 1, b = 2; int help; // Code to swap values in a and b help = a; a = b; b = help; } } |
The shuffle() method will re-arrange the cards randomly and allow all cards to be dealt:
public class DeckOfCards { private Card[] deck; private int dealPosition; DeckOfCards() ... // Done public void shuffle() // Shuffle this deck of cards { for (int i = 0; i < deck.length; i++) { // Generate an index randomly int j = (int)(Math.random() * deck.length); Card temp = deck[i]; // Swap deck[i] = deck[j]; deck[j] = temp; } dealPosition = 0; } } |
Generate random numbers j and swap card[i] and card[j]:
public class DeckOfCards { private Card[] deck; private int dealPosition; DeckOfCards() ... // Done public void shuffle() // Shuffle this deck of cards { for (int i = 0; i < deck.length; i++) { // Generate an index j randomly int j = (int)(Math.random() * deck.length); Card temp = deck[i]; // Swap card[i] and card[j] deck[i] = deck[j]; deck[j] = temp; } dealPosition = 0; } } |
Reset a dealPosition variable that marks the index of the next card that will be dealt:
public class DeckOfCards { private Card[] deck; private int dealPosition; // Marks the position of next card DeckOfCards() ... // Done public void shuffle() // Shuffle this deck of cards { for (int i = 0; i < deck.length; i++) { // Generate an index j randomly int j = (int)(Math.random() * deck.length); Card temp = deck[i]; // Swap card[i] and card[j] deck[i] = deck[j]; deck[j] = temp; } dealPosition = 0; // Reset the next deal card position } } |
The deal() method will deal out (= returns) the next card in the deck of cards
public class DeckOfCards { private Card[] deck; private int dealPosition; // Marks the position of next card DeckOfCards() ... // Done public void shuffle() ... // Done public Card deal() // deals (= returns) the next card in this deck { Card nextCard; return nextCard; } } |
If the deck has more cards, save the next card in nextCard and advance dealPosition:
public class DeckOfCards { private Card[] deck; private int dealPosition; // Marks the position of next card DeckOfCards() ... // Done public void shuffle() ... // Done public Card deal() // deals (= returns) the next card in this deck { Card nextCard; if ( dealPosition < deck.length ) { nextCard = deck[dealPosition]; dealPosition++; } return nextCard; } } |
Otherwise: return null or we can throw an exception:
public class DeckOfCards { private Card[] deck; private int dealPosition; // Marks the position of next card DeckOfCards() ... // Done public void shuffle() ... // Done public Card deal() // deals (= returns) the next card in this deck { Card nextCard; if ( dealPosition < deck.length ) { nextCard = deck[dealPosition]; dealPosition++; } else nextCard = null; // Alternately: throw exception return nextCard; } } |
Return the saved next card:
public class DeckOfCards { private Card[] deck; private int dealPosition; // Marks the position of next card DeckOfCards() ... // Done public void shuffle() ... // Done public Card deal() // deals (= returns) the next card in this deck { Card nextCard; if ( dealPosition < deck.length ) { nextCard = deck[dealPosition]; dealPosition++; } else nextCard = null; // Alternately: throw exception return nextCard; } } |
This Java program shows how to create one deck of cards and deal out some cards:
public class myProg { public static void main(String[] args) { DeckOfCards d = new DeckOfCards(); // Create 1 deck of cards for ( int i = 0; i < 53; i++ ) System.out.println( d.deal() ); // Deal out 53 cards System.out.println(); d.shuffle(); // Shuffle the deck d for ( int i = 0; i < 5; i++ ) System.out.println( d.deal() ); // Deal out 5 cards after shuffle } } |
DEMO: demo/03-classes/14-card
We can use the
DeckOfCards class
to create
any number of
decks of cards
The DeckOfCards class
will help you write
Java program that
play
card games !!!