MATLAB [Dice Game Please Help!]

Thread Starter

Judas543

Joined Jan 26, 2010
60
This script looks easy but from staring at it for countless times and referring to a book, I can't figure it out...I'm really stressing out :(

Anyways these are the instructions...So you need 1 main script and then a function script

1.You are playing a game where you roll a die 12 times. In Matlab, you simulate each result by generating a random number between [1...6] (1 point)

2.If you run a “ONE” or a “FIVE” at least six times, you win 2 dollars, or if you run “THREE” three times you win 1 dollar. (2 points)


3.Create a Matlab script that calls a function “diceGame” (1 point) that takes in a vector representing the 12 dice values and return the amount of money won.



I know I have to use this to generate the 6 values ceil(rand * 6);
need a for loop maybe and a counter.

Could someone provide the script to this please
 

Thread Starter

Judas543

Joined Jan 26, 2010
60
here some info that I think will help


% Dice Games Problems:
% generate random numbers between 0-6
% Use a For-Loop or a While-Loop to generate a certain number of results
% Store the results in a vector


% Cast a die 10 times:

Results = [ ]

for k = 1:10

TempResult = floor(7r * rand(1,1))

Results = [Results TempResult]

end

Results

% When all the results are stored in the vector “Results”
% Use a for loop to process each element of “Results”
For k = 1 : length(Results)
Do something with Results(k)
End
 

johndoe45

Joined Jan 30, 2010
364
i solved it

use for loop

if statements

the x=round(rand(1,12)*5+1) like i said earlier

start with this

this is function file which must be titled dicegame.m in same folder as script file (desktop probably)

function [money_won]=dicegame(rolls)

x=round(rand(1,rolls)*5+1)

this is script file

rolls=12

[money_won]=dicegame(rolls)
 
Last edited:

Thread Starter

Judas543

Joined Jan 26, 2010
60
so would step 2 just be a for loop with if statements? Also is step 3 done?


Hmm also I get an error when I call the function from my main script. Should I change x to money_won since that is the output


heres the error
Error in ==> diceGame at 3
x=round(rand(1,rolls)*5+1)
??? Output argument "money_won" (and maybe others) not assigned during call
 

johndoe45

Joined Jan 30, 2010
364
is function filename dicegame

can't use money_won yet because that is part you have to make with "for" loop with "if" statement inside, but did it generate the random numbers

function [money_won]=dicegame(rolls)

x=round(rand(1,rolls)*5+1)
count_a=0;

for i=1:length(x)


just to get you started
and in "if" statement will use count_a=count_a+1;
 
Last edited:

Thread Starter

Judas543

Joined Jan 26, 2010
60
yes it did generate the numbers.

hmm so if i am rolling the die 12 times then I should start out with
for i = 1:12
then the if statements
end;


Am i right?

Also is the function file finished now? And I can conentrate on the main script body
 

Thread Starter

Judas543

Joined Jan 26, 2010
60
hmm alright thanks!

So after I finish the for loop and the if statements

+how do I finish up the function file?
"takes in a vector representing the 12 dice values and return the amount of money won."

+also could you explain what count does? I read it in the matlab book but it doesnt sit with me still. Does it help keep track what numbers are which
 

johndoe45

Joined Jan 30, 2010
364
it is not a function. i just made it up
so at beginning

count=0

now in if statement say

if x==1 || x==5
count=count+1
end

therefore the count will be 0 at beginning and when a number is either 1 or 5 it will add 1 each time

then money_won=2*count cause said its 2 dollars
 
Last edited:
Top