Game of Craps using matlab

17 vues (au cours des 30 derniers jours)
Matthew Ravizza
Matthew Ravizza le 29 Mai 2016
Commenté : Steven Lord le 1 Fév 2017
"A player rolls two dice. Each die has six faces.
These faces contain 1, 2, 3, 4, 5 and 6 spots. When the dice have come to rest, the sum of the spots on the two upward faces is calculated.
(i) If the sum is 7 or 11 on the first throw, the player wins.
(ii) If the sum is 2, 3, or 12 on the first throw, the play er loses.
(iii) If the sum is 4, 5, 6, 8, 9 or 10 on the first throw, the sum becomes the player’s point. To win, you must continue rolling the dice until you make your point. However, the player loses by rolling a 7 before making the point"
i have this but all it outputs n omatter the number is "won on first roll" please help.
n = ceil(6*rand)+ceil(6*rand);
c=1;
if n == 7||11
fprintf('Won on first roll')
elseif n == 2||3||12
fprintf('Lost on first roll')
else
wn = n;
fprintf('Winning number is %d! keep rolling! \n', wn)
while n ~= wn||7
n = ceil(6*rand)+ceil(6*rand);
c = c + 1;
end
if n == wn
fprintf('Player won with a %d having rolled %d times', n, c)
else
fprintf('Player lost with a %d having rolled %d times', n, c)
end
end

Réponses (2)

Steven Lord
Steven Lord le 29 Mai 2016
This:
if n == 7||11
is NOT satisfied if either n is equal to 7 or n is equal to 11. It is always true, regardless of the value of n, because the or operator | returns true if either or both of its inputs are nonzero.
If n is equal to 7, (n == 7) is true and the statement becomes true | 11. Both true and 11 are nonzero, so the result is true and the body of the if executes.
If n is not equal to 7, (n == 7) is false and the statement becomes false | 11. While false is 0, 11 is nonzero so the result is true and the body of the if executes.
What you want instead is:
if (n == 7) | (n == 11)
Or use the ismember function if you want to check if n is one of a larger set of values.
  1 commentaire
Matthew Ravizza
Matthew Ravizza le 29 Mai 2016
Thankyou!! :D

Connectez-vous pour commenter.


M.Saud Khan
M.Saud Khan le 1 Fév 2017
For me, the above code didn't work properly. So I used "switch-case-otherwise" commands. The remaining code is based on the code of Matthew Ravizza. Here is my code:
x = 1 + round((6-1)*rand(2,1));
sumx = sum(x);
switch sumx
case 7
result = true;
case 11
result = true;
case 2
result = false;
case 3
result = false;
case 12
result = false;
otherwise
carry_over = sumx;
a(1) = sumx;
sumx = 0;
i = 1;
while 1
x = 1 + round((6-1)*rand(2,1));
sumx = sum(x);
a(i+1) = sumx;
if sumx == carry_over
break
elseif sumx == 7
break
else
carry_over = sumx;
end
i = i+1;
end
if sumx == carry_over
result = true;
else
result = false;
end
end
If someone wishes, he/she can create a function to generate the random dice numbers.
  1 commentaire
Steven Lord
Steven Lord le 1 Fév 2017
Rather than calling round on rand, I recommend using the randi function.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Historical Contests dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by