In: Statistics and Probability
using matlab
Write a script that simulates a card game that works as follows: A dealer places 5 cards face down on the table and flips the first card. The player goes down the line, one at a time, and guesses if the next card is higher or lower than the card displayed, and then the next card is revealed. In the end, the player is awarded a point for each correct guess.
In terms of coding, your script should follow this basic outline (with deviations as you decide how to code it):
1) Your script should "shuffle" and randomly order 5 cards, listed from 1-5. Note: there should be only one of each card (this is a permutation, not a combination). You should NOT display the cards (though you may want to look at them while starting to test if your code is right).
2) "Flip" the first card and display it to the user.
3) Prompt the user to guess if the next card is higher or lower. You can do this a number of ways, but I'd suggest using the "input" built-in function and certain numbers to indicate lower or higher (e.g. 'input pi for lower and tau for higher' but maybe not pi and tau).
4) Flip the next card. Assign one point to the user if they are correct, and no points if they are incorrect.
5) Repeat steps 3 & 4 until all 4 cards have been displayed and the 5th card has been revealed to be lower or higher.
6) Display the number of points the user has received, either by unsuppressing a variable name or using the "disp", "sprintf" or "fprintf" MATLAB built-in functions.
Answer:
Given taht,
Write a script that simulates a card game that works as follows: A dealer places 5 cards face down on the table and flips the first card.
The player goes down the line, one at a time, and guesses if the next card is higher or lower than the card displayed, and then the next card is revealed. In the end, the player is awarded a point for each correct guess.
In terms of coding, your script should follow this basic outline (with deviations as you decide how to code it):
Code:
% Matlab script to simulate a card game in which the user
guesses whether
% the next card is higher or lower than the current card and if the
guess
% if correct then increment the points by 1. This continues for all
the
% remaining cards
% generate a vector containing a random permutation of the
integers 1:5
cards = randperm(5);
% set points to 0 at the start
points = 0;
% display the first card
fprintf('Card 1: %d\n',cards(1));
% loop from 2 to length of cards
for i=2:length(cards)
% input 1 or 2 from user
choice = input('Enter:\n1 if next card is lower than this card\n2
if next card is higher than this card\n > ');
% display the next card
fprintf('Card %d: %d\n',i,cards(i));
if((choice == 1) && (cards(i) < cards(i-1)))
% if user selected 1 and this card < previous card, increment
the
% points by 1
points = points + 1;
elseif((choice == 2) && (cards(i) >
cards(i-1)))
% if user selected 2 and this card > previous card, increment
the
% points by 1
points = points + 1;
end
end
% display the total points received by the user
fprintf('Total points: %d\n', points)
%end of script
Output: