15.6k views
3 votes
Using arrays and other techniques as needed, create a deck of 52 playing cards. Then devise and test a method for dealing three cards at random. After each card is dealt, the deck should have some way of tracking if the card was dealt. Demonstrate how to tell if a card is still in the deck or not.

Need a hint on this one? Check out this crude but effective solution:

var cards = ["Ace of Spades", "2 of Spades", "3 of Spades", "4 of Spades","5 of Spades","6 of Spades","7 of Spades","8 of Spades","9 of Spades", "10 of Spades","Jack of Spades","Queen of Spades", "King of Spades", "Ace of Diamonds", "2 of Diamonds", "3 of Diamonds", "4 of Diamonds","5 of Diamonds","6 of Diamonds","7 of Diamonds","8 of Diamonds","9 of Diamonds","10 of Diamonds","Jack of Diamonds","Queen of Diamonds", "King of Diamonds","Ace of Hearts", "2 of Hearts", "3 of Hearts", "4 of Hearts","5 of Hearts","6 of Hearts","7 of Hearts", "8 of Hearts","9 of Hearts","10 of Hearts","Jack of Hearts","Queen of Hearts",
"King of Hearts", "Ace of Clubs", "2 of Clubs", "3 of Clubs", "4 of Clubs", "5 of Clubs","6 of Clubs","7 of Clubs","8 of Clubs","9 of Clubs","10 of Clubs","Jack of Clubs","Queen of Clubs", "King of Clubs"];

//empty array for the hand
var hand = ["empty", "empty", "empty"];

var i = 0; //counts cards for the hand

while (hand[2] == "empty"){ //draw till 3 cards are dealt

// pick a card
var picked = Math.floor(Math.random()*52);

//check for duplications, then deal
if (cards[picked] != "dealt") {
hand[i] = cards[picked];
cards[picked] = "dealt";
i++;
} // if body
} // while body

//test code
document.write(hand.toString());

1 Answer

4 votes

Final answer:

To create and deal a deck of 52 playing cards, define an array with all card strings and use random selection to deal cards, marking each card as 'dealt' after it is selected to ensure no duplicates are dealt and to track the deck's state.

Step-by-step explanation:

Creating and Dealing Playing Cards in Programming

To create a deck of 52 playing cards using arrays, define an array with all the cards as strings. Ensure that the deck includes all four suits (clubs, diamonds, hearts, and spades) and all ranks (Ace through King). After creating the deck, cards can be dealt using a random number generator to select indices, marking each card as 'dealt' after it's been selected. Ensure the random selection does not pick already dealt cards. This process is referred to as sampling without replacement and allows us to track which cards are still available in the deck.

Dealing Three Random Cards

To deal three cards, employ a while loop that runs until three unique cards are dealt. Use a variable to count the number of cards dealt, and another array to represent the dealt hand. Each time a card is dealt, check if it has been already marked as 'dealt' in the deck array. If not, add it to the hand and mark it as 'dealt' in the deck array. This approach ensures that each card is dealt with only once.
Checking if a Card is Still in the Deck

To determine if a card has been dealt, simply check the corresponding index in the deck array; if it contains the original card string, it has not been dealt, but if it has been replaced with the string 'dealt', it indicates that the card has been drawn and is no longer available in the deck.

User Tomas Bruckner
by
7.7k points

Related questions

Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.