
-----------------------------------
pokerface
Wed Sep 29, 2004 8:24 pm

please help
-----------------------------------
hello, im making a program at school where we have to roll the die. 
i got as far as:

******************************************
var roll : int
var first := 0
var die := 0
var die1, die2, die3, die4, die5, die6 := 0
var answer : string (1)

loop
    put "Would you like to roll the die? " ..
    getch (answer)

    if answer = "y" or answer = "Y" then
        put "Yes"
        put ""
        put ""

        randint (die, 1, 6)

        case die of
            label 1 :
                put "You rolled a ", die
                die1 := die1 + 1

            label 2 :
                put "You rolled a ", die
                die2 := die2 + 1

            label 3 :
                put "You rolled a ", die
                die3 := die3 + 1

            label 4 :
                put "You rolled a ", die
                die4 := die4 + 1

            label 5 :
                put "You rolled a ", die
                die5 := die5 + 1

            label 6 :
                put "You rolled a ", die
                die6 := die6 + 1

        end case
        put ""

    elsif answer = "n" or answer = "N" then
        put "Exiting"
        exit
        delay (100)
    else
        put ""
    end if

end loop

******************************
i want this program to be able to remember the first roll but continue rolling till the first roll is rolled again? then i want it to say how many rolls it took to get to  the first roll! 

eg>

(you rolled a 5
you rolled a 1
you rolled a 6
you rolled a 2
you rolled a 3
you rolled a 5

It took you 6 rolls)

i understand that this question is pretty easy but im just learning turing now! so im REALLY new to turing!

-----------------------------------
Andy
Wed Sep 29, 2004 8:29 pm


-----------------------------------
hmmm since ur new, i wont flame u, but nxt time plz dont put a topic thats more specific... and as for ur problem, just put the rolling part in a loop and have an if statement that exits the loop if the die number has a value of 1

-----------------------------------
pokerface
Wed Sep 29, 2004 8:35 pm


-----------------------------------
what do u mean put a if statement that exits the loop if the die number has a value of 1. i dont want it to exit i want it to count how many rolls it took before getting the first roll again. could u give me an example then?

-----------------------------------
wtd
Wed Sep 29, 2004 8:40 pm


-----------------------------------
If I may suggest one improvement...

Use an array to keep track of how many times each side was rolled.

Instead of:

var die1, die2, die3, die4, die5, die6 := 0

Use:

var timesRolled : array 1 .. 6 of int := init(0, 0, 0, 0, 0, 0)

Now:

 case die of
   label 1 :
      put "You rolled a ", die
      die1 := die1 + 1
   label 2 :
      put "You rolled a ", die
      die2 := die2 + 1
   label 3 :
      put "You rolled a ", die
      die3 := die3 + 1
   label 4 :
      put "You rolled a ", die
      die4 := die4 + 1
   label 5 :
      put "You rolled a ", die
      die5 := die5 + 1
   label 6 :
      put "You rolled a ", die
      die6 := die6 + 1
end case

Can become:

put "You rolled a ", die
timesRolled(die) += 1

As for keeping track of the first roll... just start a counter outside the loop.

var counter := 1
var firstRolled : int

loop
   % do your normal stuff

   if counter = 1 then
      firstRolled = die
   elsif firstRolled = die then
      % yay!  The same number was rolled again
   end

   counter += 1
end loop

-----------------------------------
pokerface
Wed Sep 29, 2004 8:46 pm


-----------------------------------
i have to keep it in case construct! is there anything else that would do it while using case construct?

-----------------------------------
wtd
Wed Sep 29, 2004 8:49 pm


-----------------------------------
i have to keep it in case construct! is there anything else that would do it while using case construct?

The first part of my post was independent of the second part.  Your use of six different variables and the case statement will work, but it's way too much work.  :)

My suggestions for the counter and how to detect if the roll is the same as the first roll will still work.

-----------------------------------
Dan
Wed Sep 29, 2004 8:51 pm


-----------------------------------

Dang, you all posted b4 me and b4 i saw u post anything. Oh well tho my awser is not bad even tho he whonts to case:? 


Well 1st thing you should make more meaningfull subject titles for your posts. If you read the things that side read this befor asking for help you whould know not to say "i need help" or "pleas help" as the topic for the post. You are post in turing help so we know u need help  :wink: 

Any how, you are not far off from what you have posted so far. You just need to sotre the 1st dice roll then comper all the others. now to do this we need to know if it is the 1st roll or not so i whould make a boolean var to be ture if it is the 1st roll and flase other wise:


var firstRoll: boolean := true


Then we need to put some ifs in the area that runs when they enter y for whonting to roll agaen. the 1st if will check and see if it is the 1st time they rolled and if it is set the value of first to that roll and then set the value of firstRoll to false. If it is not the firstRoll we then need to check the value that was rolled and see if it was the same as the first roll:


if firstRoll then
first := die
firstRoll := false
else
if first = die then
put "that is the same as the 1st roll"
exit
end if
end if


now to make your code better u could get ride of all that case stuff and replace it with a simple:


put "You rolled a ", die


b/c you do not need to keep track or how many rolls of each die but just how many rolls total.

to do the total rolls you could simpley set roll to 0 to start with then add a line to add one to roll each time you roll after the 1st time, like so:


%%%%%%at top%%%%%%
var roll : int := 0

.......

%%%%%%in the else of if firstRolll%%%%%
roll+=1

........



Also that dealy you have at the end will never be ran since u exit 1st, you may as well get ride of it. Affter all that your code will look somting like this:



var roll : int := 0
var first := 0
var firstRoll : boolean := true
var die := 0
var answer : string (1)

loop
    put "Would you like to roll the die? " ..
    getch (answer)

    if answer = "y" or answer = "Y" then
        put "Yes"
        put ""
        put ""

        randint (die, 1, 6)

        put "You rolled a ", die

        if firstRoll then
            first := die
            firstRoll := false
        else
            roll += 1
            if first = die then
                put "that is the same as the 1st roll"
                exit
            end if
        end if


    elsif answer = "n" or answer = "N" then
        put "Exiting"
        exit
    else
        put ""
    end if

end loop

put "it toke ", roll, " trys"


And if you whont to make it even better you could probly get ride of the boolean var and check the value of roll insted but i think this way is easy to understand

-----------------------------------
wtd
Wed Sep 29, 2004 8:57 pm


-----------------------------------
In your example, the "roll += 1" should be outside the conditional.  You'll want the number of rolls to be incremented by one even if it's the first roll.

Also...

put "it toke ", roll, " trys"

put "It took ", roll, " tries."  

;)

-----------------------------------
Dan
Wed Sep 29, 2004 9:04 pm


-----------------------------------
In your example, the "roll += 1" should be outside the conditional.  You'll want the number of rolls to be incremented by one even if it's the first roll.

Also...

put "it toke ", roll, " trys"

put "It took ", roll, " tries."  

;)

i thought he whonted it the other way? b/c how could u be trying to roll the 1st roll if u have not rolled it yet?

-----------------------------------
pokerface
Wed Sep 29, 2004 9:07 pm


-----------------------------------
how would it work if its case thou? im understanding what u did but i dont want to show the teacher that i did something she hadnt though yet!
eg true, false never learn't that !

sorry i just found out it dont have to be case, it could be an if statement but i still dont want to show her stuff that i never learned before!

-----------------------------------
wtd
Wed Sep 29, 2004 9:17 pm


-----------------------------------
The five minute version, with (I think) more clear variable names.

var rolls : int := 0
var firstRoll : int

loop
    var thisRoll : int
    var answer : string

    put "Would you like to roll the die? " ..
    getch (answer)

    if answer = "y" or answer = "Y" then
        put "Yes"
        put ""
        put ""

        randint (thisRoll, 1, 6)

        put "You rolled a ", thisRoll

        if rolls = 0 then
            firstRoll := thisRoll
        elsif first = die then
                put "That is the same as the 1st roll"
                put "It took ", rolls, " rolls."
                exit
        end if

        rolls += 1
    elsif answer = "n" or answer = "N" then
        put "Exiting"
        exit
    else
        put ""
    end if
end loop

-----------------------------------
Dan
Wed Sep 29, 2004 9:18 pm


-----------------------------------

Could i ever post 1st for once, grrrrr. dang you wtd and your fast posting!!!!

how would it work if its case thou? im understanding what u did but i dont want to show the teacher that i did something she hadnt though yet!
eg true, false never learn't that !

sorry i just found out it dont have to be case, it could be an if statement but i still dont want to show her stuff that i never learned before!

But if you do understand it from what we posted you have learned it ;)

Just tell her you did some resherch on your own. There is no reason to write bad code b/c the avg person in your class dose not know how to write good code :p

Any how if u do not like the boolean stuff you could do what wtd side and put the roll += 1 out side and under the if and check to see if roll = 0 rather then  checking the boolean var in my method. 

Then at the end if u whont the times that were rolled after the 1st dice witch is how i interpited the question you whould just take 1 away from roll.

-----------------------------------
wtd
Wed Sep 29, 2004 9:18 pm


-----------------------------------
how would it work if its case thou?

Then show here that you understand it.  That's great.  However, using it here doesn't serve any purpose, and part of being a good programmer is knowing when not to use certain techniques.

-----------------------------------
pokerface
Wed Sep 29, 2004 9:29 pm


-----------------------------------
ty guys/gurls (not sure ur gender) i learned alot. thanks for your time!

-----------------------------------
rizzix
Wed Sep 29, 2004 9:37 pm


-----------------------------------
pfft we are all girls sheesh.. i thought it was quite obvious  :lol:

-----------------------------------
Andy
Thu Sep 30, 2004 1:04 pm


-----------------------------------
especially me, thats me on my avatar :D

-----------------------------------
Mazer
Thu Sep 30, 2004 2:39 pm


-----------------------------------
*tee-hee*
Yea, we're girls. Face it, computer science is a woman dominated field.

-----------------------------------
the_short1
Thu Sep 30, 2004 6:30 pm


-----------------------------------
WTD: so far i have seen u try to hhelp ppl in turing 3 times 