public class DeckOfCards { private Card c1; private Card c2; ... private Card c52; // 52 Card variables !!! } |
public class DeckOfCards { private Card[] deckOfCards; // Used to store all 52 cards } |
We can use a constructor method to initialize the deck of card to contain the right cards.
public class DeckOfCards
{
public static final int NCARDS = 52;
private Card[] deckOfCards; // Contains all 52 cards
/* ---------------------------------------------------
The constructor method: make 52 cards in a deck
--------------------------------------------------- */
public DeckOfCards( )
{
/* =================================================================
First: create the array
================================================================= */
deckOfCards = new Card[ NCARDS ]; // Very important !!!
// We must crate the array first !
/* =================================================================
Next: initialize all 52 card objects in the newly created array
================================================================= */
int i = 0;
for ( int suit = Card.DIAMOND; suit <= Card.SPADE; suit++ )
for ( int rank = 1; rank <= 13; rank++ )
deckOfCards[i++] = new Card(suit, rank); // Put card in
// position i
}
}
|
Explanation:
|
We will return 13 cards on 1 line
(We use the newline character \n to separate the lines)
public String toString() { String s = ""; int k; k = 0; for ( int i = 0; i < 4; i++ ) { for ( int j = 1; j <= 13; j++ ) s += ( deckOfCards[k++] + " " ); s += "\n"; // Add NEWLINE after 13 cards } return ( s ); } |
Shuffle up and... | Deal !!! |
|
|
What is the result of dealing some cards:
Result = information about what cards has been dealt
We can represent this hand (= a collection of cards) in a computer program with the following 5 simulated card objects (= information):
3h 6c 8h Js Jh |
|
How do we simulate this ???
Answer:
|
|
We can simulate "dealing" a card from a deck using an "current card" index variable:
|
public class DeckOfCards { public static final int NCARDS = 52; private Card[] deckOfCards; // Contains all 52 cards private int currentCard; // deal THIS card in deck public DeckOfCards( ) { deckOfCards = new Card[ NCARDS ]; int i = 0; for ( int suit = Card.SPADE; suit <= Card.DIAMOND; suit++ ) for ( int rank = 1; rank <= 13; rank++ ) deckOfCards[i++] = new Card(suit, rank); currentCard = 0; // Fresh deck of card... } public String toString() { String s = ""; int k; k = 0; for ( int i = 0; i < 4; i++ ) { for ( int j = 1; j <= 13; j++ ) s += (deckOfCards[k++] + " "); s += "\n"; } return ( s ); } } |
Let's shuffle up and deal !!!!
|
|
|
// No parameters // Returns a "Card" object public Card deal() { .... } |
|
The deal() method:
public class DeckOfCards
{
public static final int NCARDS = 52;
private Card[] deckOfCards; // Contains all 52 cards
private int currentCard; // deal THIS card in deck
public DeckOfCards( )
{
deckOfCards = new Card[ NCARDS ];
int i = 0;
for ( int suit = Card.SPADE; suit <= Card.DIAMOND; suit++ )
for ( int rank = 1; rank <= 13; rank++ )
deckOfCards[i++] = new Card(suit, rank);
currentCard = 0; // Fresh deck of card...
}
/* ---------------------------------------------------------
deal(): deal the next card in the deck
i.e. deal deckOfCards[currentCard] out
--------------------------------------------------------- */
public Card deal()
{
if ( currentCard < NCARDS )
{
return ( deckOfCards[ currentCard++ ] );
}
else
{
System.out.println("Out of cards error");
return ( null ); // Error;
}
}
public String toString()
{
String s = "";
int k;
k = 0;
for ( int i = 0; i < 4; i++ )
{
for ( int j = 1; j <= 13; j++ )
s += (deckOfCards[k++] + " ");
s += "\n";
}
return ( s );
}
}
|
|
|
repeat for many times { select 2 random object in the array exchange the selected objects } |
Example: (shuffling an array of integers)
|
shuffle |
|
|
// Input: n = # exchange operations performed // Returns nothing.... public void shuffle(int n) { .... } |
Pseudo code:
// Input: n = # exchange operations performed // Returns nothing.... public void shuffle(int n) { for ( k = 1, 2, 3, ..., n ) { i = a random integer number between [0 .. 51]; j = another random integer number between [0 .. 51]; exchange: deckOfCard[i] and deckOfCard[j]; } } |
|
|
|
/* ------------------------------------------------ shuffle(n): shuffle the deck using n exchanges ------------------------------------------------ */ public void shuffle(int n) { int i, j, k; for ( k = 0; k < n; k++ ) { i = (int) ( NCARDS * Math.random() ); // Pick 2 random cards j = (int) ( NCARDS * Math.random() ); // in the deck /* --------------------------------- Swap these randomly picked cards --------------------------------- */ Card tmp = deckOfCards[i]; deckOfCards[i] = deckOfCards[j]; deckOfCards[j] = tmp; } currentCard = 0; // Reset current card to deal from top of deck } |
It can simulate a deck of 52 playing cards:
|
/* ----------------------------------------------------- Deck: a deck of cards ----------------------------------------------------- */ public class DeckOfCards { public static final int NCARDS = 52; private Card[] deckOfCards; // Contains all 52 cards private int currentCard; // deal THIS card in deck public DeckOfCards( ) // Constructor { deckOfCards = new Card[ NCARDS ]; int i = 0; for ( int suit = Card.SPADE; suit <= Card.DIAMOND; suit++ ) for ( int rank = 1; rank <= 13; rank++ ) deckOfCards[i++] = new Card(suit, rank); currentCard = 0; } /* --------------------------------- shuffle(n): shuffle the deck --------------------------------- */ public void shuffle(int n) { int i, j, k; for ( k = 0; k < n; k++ ) { i = (int) ( NCARDS * Math.random() ); // Pick 2 random cards j = (int) ( NCARDS * Math.random() ); // in the deck /* --------------------------------- swap these randomly picked cards --------------------------------- */ Card tmp = deckOfCards[i]; deckOfCards[i] = deckOfCards[j]; deckOfCards[j] = tmp;; } currentCard = 0; // Reset current card to deal } /* ------------------------------------------- deal(): deal deckOfCards[currentCard] out ------------------------------------------- */ public Card deal() { if ( currentCard < NCARDS ) { return ( deckOfCards[ currentCard++ ] ); } else { System.out.println("Out of cards error"); return ( null ); // Error; } } public String toString() { String s = ""; int k; k = 0; for ( int i = 0; i < 4; i++ ) { for ( int j = 1; j <= 13; j++ ) s += (deckOfCards[k++] + " "); s += "\n"; } return ( s ); } } |