|
|
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 );
}
}
|
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 );
}
}
|
The output is now: Ace of Spades !
|
|
|
|
|
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
}
}
|
Making a deck of cards consists of making 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
deck[0] = new Card( "Spades", "Ace");
deck[1] = new Card( "Spades", "2");
...
and so on
(How can we code this easily ??)
}
...
}
|
Making a deck of cards consists of making 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"};
}
}
|
Making a deck of cards consists of making 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++;
}
}
}
|
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() ...
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() ...
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() ...
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() ...
public void shuffle() ...
public void deal() // deals (= returns) the next card in this deck
{
Card result = deck[dealPosition];
dealPosition++;
return result;
}
}
|
Save the next card in the deck in the variable nextCard:
public class DeckOfCards
{
private Card[] deck;
private int dealPosition; // Marks the position of next card
DeckOfCards() ...
public void shuffle() ...
public void deal() // deals (= returns) the next card in this deck
{
Card nextCard = deck[dealPosition];
dealPosition++;
return nextCard;
}
}
|
Advance the dealPosition by 1 card (so we don't deal the same card out again):
public class DeckOfCards
{
private Card[] deck;
private int dealPosition; // Marks the position of next card
DeckOfCards() ...
public void shuffle() ...
public void deal() // deals (= returns) the next card in this deck
{
Card nextCard = deck[dealPosition];
dealPosition++;
return nextCard;
}
}
|
Return the saved next card:
public class DeckOfCards
{
private Card[] deck;
private int dealPosition; // Marks the position of next card
DeckOfCards() ...
public void shuffle() ...
public void deal() // deals (= returns) the next card in this deck
{
Card nextCard = deck[dealPosition];
dealPosition++;
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 < 5; i++ )
System.out.println( d.deal() ); // Deal out 5 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
}
}
|
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 !!!