MATLAB [Dice Game Please Help!]

Discussion in 'Programmer's Corner' started by Judas543, Mar 21, 2010.

  1. Judas543

    Thread Starter Member

    Jan 26, 2010
    60
    0
    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
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    If I solve this do I get the credits;)
     
  3. Judas543

    Thread Starter Member

    Jan 26, 2010
    60
    0
    of course you do
     
  4. Judas543

    Thread Starter Member

    Jan 26, 2010
    60
    0
    has anyone solve this yet X_X
     
  5. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    When's it due?
     
  6. Judas543

    Thread Starter Member

    Jan 26, 2010
    60
    0
    its due this wednesday. So if anyone can get the answer to this script that would be most helpful
     
  7. johndoe45

    Member

    Jan 30, 2010
    364
    1
    got this part wrong

    its rand*5+1
     
  8. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    What have you done so far? Post the code you've written.
     
  9. Judas543

    Thread Starter Member

    Jan 26, 2010
    60
    0
    actually I don't really know where to start...
     
  10. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    Start by reading your Matlab manual. They would have sample code in there. Your instructor must have given you some guidance.
     
  11. Judas543

    Thread Starter Member

    Jan 26, 2010
    60
    0
    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
     
  12. johndoe45

    Member

    Jan 30, 2010
    364
    1
    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: Mar 21, 2010
  13. Judas543

    Thread Starter Member

    Jan 26, 2010
    60
    0
    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
     
  14. johndoe45

    Member

    Jan 30, 2010
    364
    1
    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: Mar 22, 2010
  15. Judas543

    Thread Starter Member

    Jan 26, 2010
    60
    0
    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
     
  16. johndoe45

    Member

    Jan 30, 2010
    364
    1
    let me post an example close to it

    at end says plot(t,x)

    || means or and && means and

    so like when x=2 and when x=3

    matlab syntax
    x==2 && x==3
     
  17. Judas543

    Thread Starter Member

    Jan 26, 2010
    60
    0
    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
     
  18. johndoe45

    Member

    Jan 30, 2010
    364
    1
    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: Mar 21, 2010
  19. Judas543

    Thread Starter Member

    Jan 26, 2010
    60
    0
    alright Ill see what I can get done and post what I have done. Thanks for your time, I really appreciate it !! ^_^
     
  20. johndoe45

    Member

    Jan 30, 2010
    364
    1
    no problem
    i'm actually taking the class now too

    numerical methods
     
Loading...