In: Computer Science
** 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:
%%%%%%%%%% 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);