Question: Program 1 3 : The Card Game 2 5 Four players take a deck of 5 2 playing cards and have the card shuffled sufficiently.

Program 13: The Card Game 25
Four players take a deck of 52 playing cards and have the card shuffled sufficiently. The deck is dealt out so that each player has a stack of 13 random cards, face down. The game is played by each player taking a turn, flipping over the top card from their personal stack, and placing the revealed card in the center of the table. Each card has a signed numerical value based on the suite's color and value (ace =1, jack =11, queen =12, king =13, and red = negative values, black = positive values).
At each turn, the revealed numerical value is added or subtracted from the current tally. Hearts and clubs suites are added to the sum, and diamonds and spades are subtracted from the sum. When the total value reaches exactly 25, the game ends, and the player placing the last card is the winner. If the sequence of turns exhaust the players stacks and the sum never reaches 25, then the game ends with no winners.
Your programming task is to implement the game. The following points should guide your code.
At the beginning of the program, use Fisher-Yates shuffle algorithm that uses the random number generator (from Program 7 of Lab 5) of the Linux kernel, to shuffle a digital representation of the 52-card deck. The Fisher-Yates algorithm is described below. The deck can be represented as an array of 52 card elements.
Deal the shuffled deck to the four players. Each player's pile of cards can be represented as an array of 13 cards. You may divide and distribute the cards in may different ways, such as 13 card elements at a time to each player.
Reset the sum register to zero. Set up an inner for-loop and an outter for-loop to read the cards elements from each player and work through the 13 card elements in the players' piles. At each iteration (game turn) read the next card element and add or subtract the positive or negative value against the sum.
After updating of the sum, compare with 25. On not equal, continue the loop iterations. On equal, print the winning player and exit the program. If all of the card elements are processed with no winner, print "No Winner" and exit the program.
Recommended structure for representing the 52 playing cards:
Use a byte of memory to represent each card. Let the byte be subdivided into an upper and lower nibble (4 bits). Let the upper nibble encode the suite and color. Let the lower nibble encode the face value of the card (1 through 13).
It is not too critical how suite and color are mapped to the bit patterns. There are just 4 types, and they can be encoded using 2 bits. We can let one bit represent addition and subtraction, and let the other bit represent positive and negative values. Use an if-then code segment to test a bit. One of the if-then code segments switches between add and subtract. The other if-then code segment switches between no action and taking the 2's complement of the face value.
Use the AND operand to mask off the bits from the card element byte, not being used, before copying the value or testing a bit. Move the face value nibble to another W or X register before preforming the 2's complement negation.
The Fisher-Yates shuffle algorithm is a for-looped two step process. In the first step, a random number is assigned or associated to card element. Then in the second step, a swap takes place between the card and another card at the index enumerated by the random number. While moving the random numbers around, the associated card elements are also moved in like arrangement. At the end, the card elements are randomly ordered.
Linux provides a system call for generating a random number. The call number is 278 for the X8 register. The syscall uses three arguments:
X0= memory address of a buffer of n-bytes,
X1= size n of the buffer in bytes, and
X2= control flags (set to 0).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!