|
|
|
|
|
|
DEMO: demo/10-classes/12-card/Demo.java + Card.java
|
|
DEMO: demo/10-classes/13-card/Demo.java + Card.java
|
We must first define a class that represents a deck of cards...
|
|
|
|
|
Based on the properties and actions, we must add the variable dealPosition to the DeckOfCards class:
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
}
}
|
Next: implement the constructor(s) and methods...
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");
deck[2] = new Card( "Spades", "3");
deck[3] = new Card( "Spades", "4");
deck[4] = new Card( "Spades", "5");
...
and so on
Uses 52 assignment statements !!!
How can we code this easier ??
}
}
|
The naive approach is very tedious to code -- we can simplify the coding work with loops
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"};
}
}
|
The suit[] array contains all possible suits and The rank[] array contains all possible ranks of the cards
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 store it in deck[k]
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++;
}
}
}
|
The index i go through all the suits and the index j go through all the ranks
Before we write the shuffle() method, we review how to swap 2 variables:
Before we write the shuffle() method, we review 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
}
}
|
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;
}
}
|
Go through every card i in the deck:
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++)
{
}
dealPosition = 0;
}
}
|
Pick a random card j to swap with the card i:
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);
}
dealPosition = 0;
}
}
|
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 dealPosition to point to the top card in the deck:
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 to the top
}
}
|
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; // Variable to store the next card dealt
}
}
|
If the deck has some cards left, (1) save the next card in nextCard and (2) 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]; // (1) save the next card dealt
dealPosition++; // (2) advance to next card in deck
}
}
}
|
Otherwise (the deck has no cards left), return null to indicate an error:
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]; // (1) save the next card dealt
dealPosition++; // (2) advance to next card in deck
}
else
nextCard = null; // null is a special value to indicate error in Java
}
}
|
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; // null is a special value to indicate error in Java
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/10-classes/14-card/Demo.java + DeckOfCards.java + Card.java
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 !!!