Question

In: Computer Science

** USING MATLAB TO PROGRAM The main objective of this lab is to create a game...

** USING MATLAB TO PROGRAM

The main objective of this lab is to create a game that involves betting on the sum of two dice. The player will start out with some initial total amount of money. During each round, the player can bet some money that the sum of the two dice will be equal to a certain number. If the player wins the bet, that player adds the amount of the bet to his or her current total. If the player loses the bet, that player subtracts the amount of the bet from his or her current total. The game ends when the player decides to quit or when the player is out of money. After the game has finished, the main program should display a pie chart that shows the percentage of times that the sum of the dice equaled each of the possible sum values.

We can use this to calculate the probabilities of each sum.

P(sum=2) = 1/36 = 0.0278

P(sum=3) = 2/36 = 0.0556

P(sum=4) = 3/36 = 0.0833

P(sum=5) = 4/36 = 0.1111

P(sum=6) = 5/36 = 0.1389

P(sum=7) = 6/36 = 0.1667

P(sum=8) = 5/36 = 0.1389

P(sum=9) = 4/36 = 0.1111

P(sum=10) = 3/36 = 0.0833

P(sum=11) = 2/36 = 0.0556

P(sum=12) = 1/36 = 0.0278

We can use these theoretical probabilities as a basis of comparison when looking at the observed dice sum outcomes when playing the game.

Helpful Code

We can use a single while loop to continue to play the game until a certain condition is met. The code below shows a while loop that checks the value of a single variable called running. The code inside of the while loop will continue to execute as long as the running variable evaluates to true. In this case, when count is less than or equal to zero, we stop the main loop. We can use a similar approach to set up our main game loop.

  count = 100;

  running = true;

  while(running)

      count = count – 10;

      if count <= 0

          running = false;

      end

  end

Rolling dice can be done many ways, in this case, since we are looking for random rolls, we can use the randi() function. An example of this can be implemented with the following code:

  my_roll = randi(6);

Implementation

Here's an approach that will work for implementing the game:

  1. Set up the following variables to keep track of the game state:
    • sum_count_array: an array to keep track of the number of times that each dice sum has occurred. There should be 11 elements in this array since there are 11 possible sum outcomes. The first element indicates the number of times a sum of 2 has occurred; the second element indicates the number of times a sum of 3 has occurred; and so on. Each of the elements in this array should be initialized to 0.
    • money: a variable to keep track of the amount of money the player currently has. This variable should be initialized to 100.
    • round: a variable to keep track of the game round. This variable should be initialized to 1.
    • keep_playing: a variable that keeps track of whether or not the player would like to continue playing. This variable should be initialized to true.
  2. Set up the main loop to run the game as long as the keep_playing variable is true. The rest of the game logic, which does not include the pie chart, will go inside this loop.
  3. Print out the round number and the amount of money the player currently has.
  4. Ask the player for an amount of money he or she would like to bet and store the result in a variable called current_bet.
  5. Ask the player what value he or she would like to bet on for the sum of the two dice and store the result in a variable called current_guess.
  6. Roll two dice and store the resulting number for each die. Store the sum of the two dice in a variable called current_sum. Add to the count for the sum in the appropriate index of sum_count_array. Display the result for each die and the sum of the two dice.
  7. Check to see if the player wins or loses his or her bet. Adjust the money variable accordingly, and tell the user whether or not they won the bet. Display the updated amount of money that the player has.
  8. If the player does not have any more money, print out a relevant message and set keep_playing to false.
  9. If the player does have more money, ask the player whether or not he or she would like to continue playing the game.
    • If the player enters ‘y’, increment the round number.
    • If the player enters ‘n’, set keep_playing to false.
  1. Once the main game loop has ended, display the percentage of times that each dice roll sum occurred in a pie chart. Give the chart an appropriate title.

Solutions

Expert Solution


%%%%%%%%%% code %%%%%%

clc;

close all;

sum_count_array =zeros(11,1);
disp(sum_count_array);
money=100;
round=1;
keep_playing = true;

count = 100;
while(keep_playing)
    disp('round number');
    disp(round);
    disp('money: ');
    disp(money);
    current_bet = input('Enter amount of money you like to bet: ');
    if(current_bet>money)
        disp('bet u have entered is more than your money');
        continue;
    end
    current_guess = input('Enter your guess: ');
    dice1= randi(6);
    dice2= randi(6);
    dice_sum =dice1+dice2;
    sum_count_array(dice_sum-1,1)=sum_count_array(dice_sum-1,1)+1;
    disp('current dice value for round is ');
    disp(dice_sum);
    disp('1st dice');
    disp(dice1);
    disp('2nd dice');
    disp(dice2);
    if(current_guess == dice_sum)
      
        disp('congrats you have won the bet');
        money=money + current_bet;
    else
        disp('you have loss the bet');
        money =money-current_bet;
    end
  
    if(money<=0)
        disp('Your account balance became zero');
        keep_playing =false;
    else
        disp('do you want to continue? ');
        option=input('press y to continue press n to quit\n   ','s');
        if(option == 'y')
            round =round+1;
        else
            keep_playing=false;
        end
          
          
    end

    
end


labels={'2','3','4','5','6','7','8','9','10','11','12'};
pie(sum_count_array,labels);


Related Solutions

MATLAB is program Respond fast please a quiz Create a MATLAB script. Using nested for loops,...
MATLAB is program Respond fast please a quiz Create a MATLAB script. Using nested for loops, evaluate the multivariable function: z = sin ⁡ ( x ) cos ⁡ ( y ) for x between -4 and 4 in steps of 1 y between -2 and 2 in steps of 1 Display the matrix z Cut and paste the following into a word doc and submit to this question. Your script code Your input Your output
Having some trouble with this Java Lab (Array.java) Objective: This lab is designed to create an...
Having some trouble with this Java Lab (Array.java) Objective: This lab is designed to create an array of variable length and insert unique numbers into it. The tasks in this lab include: Create and Initialize an integer array Create an add method to insert a unique number into the list Use the break command to exit a loop Create a toString method to display the elements of the array Task 1: Create a class called Array, which contains an integer...
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops,...
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops, if loops, while loops, arrays and conditional execution, as well as creating an image of the game board. The list below must be considered in the code. - Selecting a word from a dictionary (a text file) - Reading a single letter from the user - Building a character array showing the letters matched so far - Keeping count of the number of guessed...
Create a Linear Program solver using Matlab or Python. Prompt variable and constraint entry.
Create a Linear Program solver using Matlab or Python. Prompt variable and constraint entry.
Write a program in Matlab where the program plays a hot and cold game. The user...
Write a program in Matlab where the program plays a hot and cold game. The user answers two inputs: x=input('what is the x location of the object?') y=input('what is the y location of the object?') You write the program so that the computer plays the game. Pick a starting point. Program Calculates the distance to the object. Program picks another point, calculates the distance to the object. Program knows its at the right spot when the distance is less than...
Write a Matlab program to create specialized plot animation with 16 frames by using fast Fourier...
Write a Matlab program to create specialized plot animation with 16 frames by using fast Fourier transforms of complex matrices
Using the main function’s arguments, create a program that takes in a person’s name, their home...
Using the main function’s arguments, create a program that takes in a person’s name, their home town/location, and cell number then prints out the following message: Sample run 1: Java lab01_task03 Sililo Uis 0819876543 Output:   Contact successfully saved !! Contact Name : Sililo @ Uis Home number: 0819876543
Create a Matlab program that can evaluate Green Theorem
Create a Matlab program that can evaluate Green Theorem
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
Create a program using Matlab that can solve Three Dimensional Space; Vectors, Partial Derivatives or any...
Create a program using Matlab that can solve Three Dimensional Space; Vectors, Partial Derivatives or any of the topic in calculus 3; show 5 examples please thankyou!! just use matlab for the solution like the codes thankyou
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT