/****** Deck Of Cards ************************************************
* A class that can be used by any game that requires a deck of cards. *
* It is a standard 52 card deck. If jokers are required, for example, *
* you can subclass this and override or overload the default *
* constructor to make a deck with the required number of cards. *
* Member functions: - DeckOfCards() *
* - shuffle() *
* - deal() *
* - discard(int) *
************************************************************************/
public class DeckOfCards {
/**** Constants **************************************************/
static final int EMPTY = -99999; //*** designates no cards left ****
/**** private member data ****************************************
* deck - an array of integers that represents the cards *
* 1 = ace of diamonds *
* 2 = two of diamonds *
* 13 = king of diamonds *
* 14 = ace of hearts *
* 27 = ace of clubs *
* 40 = ace of spades *
* remain - how many cards are left in the deck (may want to *
* subclass and add a function that returns this) *
*********************************************************************/
int deck[], remain;
/**** DeckOfCards - default constructor ***************************
* Allocates space for the deck array and initializes the deck by *
* shuffling it *
*********************************************************************/
public DeckOfCards () {
deck = new int[53];
shuffle();
}
/**** shuffle *****************************************************
* Puts all the cards in the deck in a random order. The first card *
* to be dealt is the last one in the array (index of 52). The last *
* card is the first one actually in the array (index of 1). Note *
* that the real first element in the array (index of 0) is unused *
*********************************************************************/
public void shuffle() {
/**** local variables *******************************************
* available - an array of integers that is initially in order *
* from 1 to 52. The integers get moved to the *
* front of the array as they are used *
* toGo - how many cards are left to be inserted into the deck *
* temp - used to store the most recent random number *
* i - a loop control variable *
******************************************************************/
int available[], toGo, temp, i;
remain = deck.length - 1; // all the cards remain in the deck
available = new int[deck.length];
for (i = 1; i <= deck.length - 1; i++)
available[i] = i;
/*** choose a random number from 1 to how many are toGo and put ***
*** the available card in the deck, repeating for all cards ***/
for (toGo = deck.length - 1; toGo > 0; toGo--) {
temp = (int)(Math.random() * toGo) + 1;
deck[toGo] = available[temp];
/*** remove the card just added from the available array ***/
for (i = temp; i < toGo; i++)
available[i] = available[i + 1];
}
}
/**** deal *********************************************************
* Pops the top card off the deck and returns it if there is one. *
* Otherwise it returns EMPTY *
*********************************************************************/
public int deal() {
if (remain == 0)
return EMPTY;
else
return deck[remain--];
}
/**** discard ******************************************************
* Puts the card it is passed on the bottom of the deck. Note: it *
* assumes a valid card is passed. This will be the case if you *
* only ever pass it something that was dealt in the first place. *
* Ideally we shouldn't be making that assumption. *
*********************************************************************/
public void discard (int card) {
/*** move all the other cards one over in the array ***/
for (int i = remain; i > 0; i--)
deck[i + 1] = deck[i];
/*** add the card to be discarded to the bottom of the deck ***/
deck [1] = card;
remain++; //*** there is now 1 more card in the deck
}
}
|