expand icon
book Programming Logic and Design, Introductory 7th Edition by Joyce Farrell cover

Programming Logic and Design, Introductory 7th Edition by Joyce Farrell

Edition 7ISBN: 1285225562
book Programming Logic and Design, Introductory 7th Edition by Joyce Farrell cover

Programming Logic and Design, Introductory 7th Edition by Joyce Farrell

Edition 7ISBN: 1285225562
Exercise 35

Create two parallel arrays that represent a standard deck of 52 playing cards. One array is numeric and holds the values 1 through 13 (representing Ace, 2 through 10, Jack, Queen, and King). The other array is a string array that holds suits (Clubs, Diamonds, Hearts, and Spades). Create the arrays so that all 52 cards are represented. Then, create aWar card game that randomly selects two cards (one for the player and one for the computer) and declares a winner or a tie based on the numeric value of the two cards. The game should last for 26 rounds and use a full deck with no repeated cards. For this game, assume that the lowest card is the Ace. Display the values of the player’s and computer’s cards, compare their values, and determine the winner. When all the cards in the deck are exhausted, display a count of the number of times the player wins, the number of times the computer wins, and the number of ties. Here are some hints:

• Start by creating an array of all 52 playing cards.

• Select a random number for the deck position of the player’s first card and assign the card at that array position to the player.

• Move every higher-positioned card in the deck “down” one to fill in the gap. In other words, if the player’s first random number is 49, select the card at position 49 (both the numeric value and the string), move the card that was in position 50 to position 49, and move the card that was in position 51 to position 50. Only 51 cards remain in the deck after the player’s first card is dealt, so the available-card array is smaller by one.

• In the same way, randomly select a card for the computer and “remove” the card from the deck

Step-by-step solution
Verified
like image
like image

Step 1 of 6

Pseudo code:

The below is the pseudo code which displays the number of times computer won, player won and ties.

start

Declarations

num a

num b

num range

num ind

num pCard

num pCardNum

num cCard

num cCardNum

num pWin = 0

num cWin = 0

num tie = 0

string pCardSuit

string cCardSuit

num SIZE = 52

num TYPES = 4

num CROUNDS = 26

num BOUNDS[TYPES] = 0, 13, 26, 39

num cards[SIZE] = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,

40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52

string CSUITS[TYPES] = “Clubs”,“Diamonds”, “Hearts”,“Spades”

start()

while b < CROUNDS

loop()

endwhile

endMethod()

stop


Step 2 of 6


Step 3 of 6


Step 4 of 6


Step 5 of 6


Step 6 of 6

close menu
Programming Logic and Design, Introductory 7th Edition by Joyce Farrell
cross icon