Question

In: Statistics and Probability

using matlab Write a script that simulates a card game that works as follows: A dealer...

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.

Solutions

Expert Solution

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:


Related Solutions

Using MATLAB or Octave, Write a script that prompts the user for the coordinates of three...
Using MATLAB or Octave, Write a script that prompts the user for the coordinates of three points A, B, and C, namely (xA, yA), (xB, yB), (xC, yC), forming a triangle, storing each in a variable (for a total of 6 variables). The script should then calculate the centroid G = (xG, yG) using xG = xA+xB+xC 3 and yG = yA+yB+yC 3 , storing each in a variable. Finally, the script should print all four points. Sample output: Enter...
Using MATLAB or Octave, Write a script that prompts the user for a multiplier, storing it...
Using MATLAB or Octave, Write a script that prompts the user for a multiplier, storing it in the variable fMultiplier. The script should them prompt the user for a number, storing it in the variabl fIn. The script should then calculate the product of these two variables, saving the result to another variable fOut, and printing it. The script should then repeat the prompt for fIn, calculation, and output twice more, using the same variable fIn and fOut all three...
Using MATLAB or Octave, Write a script that prompts the user for a minimum and maximum...
Using MATLAB or Octave, Write a script that prompts the user for a minimum and maximum real number, then generates and prints a random number in the requested range. The script should then do the same for an integer range. Sample output: Enter a minimum real value: 0.5 Enter a maximum real value: 30 A random number in the range ( 0.5000, 30.0000 ) is 1.7851 Enter a minimum integer value: -10 Enter a maximum integer value: 20 A random...
MATLAB: Write as a script in the editor window of matlab. Quadratic roots. Write a program,...
MATLAB: Write as a script in the editor window of matlab. Quadratic roots. Write a program, quadroots.m, for finding the roots of the second- order polynomial ax2 + bx + c. Use the quadratic equation. The inputs are the coefficients a,b, and c and the outputs are z1 and z2. The program should produce (exactly) the following output in the Command window when run with (a, b, c) = (1, 2, −3):
(HTML) Write a script that plays a “guess the number” game as follows: Your program chooses...
(HTML) Write a script that plays a “guess the number” game as follows: Your program chooses the number to be guessed by selecting a random integer in the range 1 to 1000. The script displays the prompt Guess a number between 1 and 1000 next to a text field. The player types a first guess into the text field and clicks a button to submit the guess to the script. If the player's guess is incorrect, your program should display...
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for...
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for a minimum and maximum real number, then generates and prints a random number in the requested range. The script should then do the same for an integer range. Sample output: Enter a minimum real value: 0.5 Enter a maximum real value: 30 A random number in the range ( 0.5000, 30.0000 ) is 1.7851 Enter a minimum integer value: -10 Enter a maximum integer...
Write a python program that simulates a simple dice gambling game. The game is played as...
Write a python program that simulates a simple dice gambling game. The game is played as follows: Roll a six sided die. If you roll a 1, 2 or a 3, the game is over. If you roll a 4, 5, or 6, you win that many dollars ($4, $5, or $6), and then roll again. With each additional roll, you have the chance to win more money, or you might roll a game-ending 1, 2, or 3, at which...
Write a MATLAB script file that will give the value of f(x) using method of least...
Write a MATLAB script file that will give the value of f(x) using method of least squares and asks the user to enter values of x, y, n (curve fit), and x for f(x).
Write a MATLAB script file that will give the value of f(x) using method of least...
Write a MATLAB script file that will give the value of f(x) using method of least squares and asks the user to enter values of x, y, n (linear, quadratic, or cubic), and x for f(x).
write an awk script that works as wc command.
write an awk script that works as wc command.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT