MATLAB [Dice Game Please Help!]

Thread Starter

Judas543

Joined Jan 26, 2010
60
alright this is what I have for the main script. What Im not too sure if I'm doing it the way the instructions is telling me to do.

%Dice Game

clc %clear command
clear %clear workspace

%Initiate Counters
one_count = 0;
five_count = 0;
three_count = 0;

%Array Of Rolls
rolls = 12;

%Generates a random number between [1...6]
x=round(rand(1,rolls)*5+1)

for i = 1:12

%Generate Counts
if i==1 || i==5 %If i = 1 or i = 5 then start counter and give money back
one_count = one_count+1
five_count = five_count+1
money_won1_5=2*one_count + five_count
else if i ==3 %Else if i = 3 then start counter and give back money
three_count = three_count+1
money_won3=three_count
end
end
end
 

johndoe45

Joined Jan 30, 2010
364
you tried all that matters

here is dicegame.m

function [money_won]=dicegame(rolls)

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

count_a=0;

for i=1:length(x)
x(i);
if x(i)==1 || x(i)==5
count_a=count_a+1;
end
end

count_a;

money_won=count_a*2;
 

Thread Starter

Judas543

Joined Jan 26, 2010
60
yea i had a problem with that ..I didn't know how to check if it rolled 6 times for either 1 or 5 and check if I rolled a three , three times to get the money
 

johndoe45

Joined Jan 30, 2010
364
made program but testing.

have not gotten a 1 & 5 more than six times plus 3 more than three times

other words have not won 3 dollars yet :mad:
 

johndoe45

Joined Jan 30, 2010
364
problem solved. just increased number of rolls

it works. any questions just let me know

function [money_won]=dicegame(rolls)

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

count_a=0;
count_b=0;
count_c=0;
count_d=0;

for i=1:length(x)
x(i);
if x(i)==1 || x(i)==5
count_a=count_a+1;
elseif x(i)==3
count_b=count_b+1;
end
end

if
count_a >= 6
count_c=count_c+1;
end
if
count_b >= 3
count_d=count_d+1;
end

money_won=count_d+count_c*2;
 

Thread Starter

Judas543

Joined Jan 26, 2010
60
function [money_won]=dicegame(rolls)

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

count_a=0;
count_b=0;
count_c=0;
count_d=0;

for i=1:length(x)
x(i);
if x(i)==1 || x(i)==5
count_a=count_a+1;
elseif x(i)==3
count_b=count_b+1;
end
end

if
count_a >= 6
count_c=count_c+1;
end
if
count_b >= 3
count_d=count_d+1;
end

money_won=count_d+count_c*2;
Is that whole part the function file?? If so what was I suppose to do in the main body of the script? Since Im suppose to have 1 .m file that acts like the main script, and call another matlab function .m file. So two matlab scripts in all
 

johndoe45

Joined Jan 30, 2010
364
run with this script moneywon.m
other one in post above must be labeled dicegame.m cause that is the name of the function

clc % if want to delete everything in command window
clear
all
close all

%otherwise just use what is under here

rolls=10

[money_won]=dicegame(rolls)
 

Thread Starter

Judas543

Joined Jan 26, 2010
60
wow... I wasn't even close to solving this problem myself it seems, I think I only got parts of it.. If I may ask, how do you get so good at matlab? Do you have a lot of books to read off of if so what, or is it by doing a lot of examples?
 

johndoe45

Joined Jan 30, 2010
364
examples. our teacher makes us do a bunch. haven't even read the book yet. all you need to do is understand "for" loops, "while" loops, "if" statements and he said that is basically all you need to know in engineering applications.

this is all we covered. and last lecture was how to make a function file like one we just made.

MAKE SURE CHANGE ROLLS TO 12 SO DON'T GET POINTS TAKEN OFF in script file moneywon.m
 
Last edited:

Thread Starter

Judas543

Joined Jan 26, 2010
60
where do you get your examples from or can you share them? By the way, thanks for all your help and time. Hopefully I didn't take away your time if you had to do something else. I really appreciate it!

I'll have to make sure I understand those concepts completely. Is it me or matlab can be a little too time consuming and tedious some times...

+also thanks for the revision, can't believe it didn't caught my eye
 

johndoe45

Joined Jan 30, 2010
364
your welcome

no it can be tedious. spent forever on this upcoming homework and still can't get last problem

when get time will pm you examples
 
Last edited:
Top