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 : The Card Game
Four players take a deck of playing cards and have the card shuffled sufficiently. The deck is dealt out so that each player has a stack of 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 jack queen king 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 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 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 FisherYates shuffle algorithm that uses the random number generator from Program of Lab of the Linux kernel, to shuffle a digital representation of the card deck. The FisherYates algorithm is described below. The deck can be represented as an array of card elements.
Deal the shuffled deck to the four players. Each player's pile of cards can be represented as an array of cards. You may divide and distribute the cards in may different ways, such as card elements at a time to each player.
Reset the sum register to zero. Set up an inner forloop and an outter forloop to read the cards elements from each player and work through the 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 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 playing cards:
Use a byte of memory to represent each card. Let the byte be subdivided into an upper and lower nibble bits Let the upper nibble encode the suite and color. Let the lower nibble encode the face value of the card through
It is not too critical how suite and color are mapped to the bit patterns. There are just types, and they can be encoded using bits. We can let one bit represent addition and subtraction, and let the other bit represent positive and negative values. Use an ifthen code segment to test a bit. One of the ifthen code segments switches between add and subtract. The other ifthen code segment switches between no action and taking the 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 s complement negation.
The FisherYates shuffle algorithm is a forlooped 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 for the X register. The syscall uses three arguments:
X memory address of a buffer of nbytes,
X size n of the buffer in bytes, and
X control flags set to
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
