Computer Science Canada

Keeping Some Numbers, But Not Others

Author:  SwAnK [ Sun Apr 24, 2005 11:35 pm ]
Post subject:  Keeping Some Numbers, But Not Others

hey, I'm trying to get my dice program, so that when you roll, you can select what dice you wish to keep and which you'd like to roll again.
Here is my code so far. I'm confused about how I can get which dice the user would like to keep. Each of the Rand.Int are already assigned values are they not, in my code, 1..5.??

code:
var roll : array 1 .. 5 of int
var reroll : int
var dice : int

procedure rolling
    for i : 1 .. 5
        roll (i) := Rand.Int (1, 6)
        put "Rolled ", roll (i)
    end for
end rolling


rolling
put "how many dice do you want to reroll"
get reroll
put "what dice do u want to reroll?"
get WHAT DO I PUT HERE???????

for i : 1 .. reroll
    roll (i) := Rand.Int (1, 6)
    put "Rolled", roll (i)
end for


oh, and also it would be cool, so that when they rerolled the dice the orginal dice rolled, actually rerolled. For mine it would display the numbers, then show the rerolls seprately. if you get rid of the one get you'd see what i mean. Thanx again!

Author:  shlitiouse [ Mon Apr 25, 2005 1:49 am ]
Post subject: 

Hmmm, I haven't really used arrays at all, but I've read a basic tutorial on them, so I'm not completely lost here... First off, there's a couple ways that you could do this.

var a:string
var dice1,dice2,dice3:string:="not"
var dicenum1.dicenum2.dicenum3 %for purposes of this example, I'll just do this as if there's only 3 dice being rolled

code:

loop
   dice1:="not"
   dice2:="not"%setting all dice variables to an off position
   dice3:="not"
       for i : 1 ..3
           randint(num,1,6)
           put "Rolled ", roll (i)
           if i=1 then %counts what dice is being rolled and stores the                  variable into the dicenum variable.
              dicenum1=num
           elsif i=2 then
              dicenum2=num
           elsif i=3 then
              dicenum3=num
           end if
       end for


   drawbox (10,10,20,20,black)
   drawbox (25,25,35,35,black)%these are the buttons representing the   dice
   drawbox (40,40,50,50,black)
   put "how many dice do you want to reroll"
   get reroll
   if reroll>0 then
      put "what dice do u want to reroll?"
     loop 
        mousewhere(mx,my,b)
%these lines make it so that if a button is clicked while over on of the mouse buttons, then it will change that dice's variable to "selected"
        if b=1 mx>=10 and mx <=20 and my>=10 and my<=20 then
           dice1:="selected"
        elsif b=1 mx>=25 and mx<=35 and my>=25 and my<=35 then
           dice2:="selected"
        elsif b=1 mx>=40 and mx<=50 and my>=40 and my<=50 then
           dice3:="selected"
        end if
        put "Are you sure? (Y/N)
        get a
        exit when a="y"
     end loop
   end if

%These lines make it so that all the dice with the variable set to "selected" will be rerolled, you may want to put the number rerolled to be displayed somewhere.
   if dice1="selected" then
      randint (num,1,6)
      dicenum1=num
   elsif dice2="selected" then
      randint (num,1,6)
      dicenum2=num
   elsif dice3="selected" then
      randint (num1,6)
      dicenum3=num
   end if
end loop

Alright, this is quite a bit different than what you were doing. But what I've done here is that I used 3 dice, just to keep things clear. 3 boxes will be drawn on the screen, you can label them or whatever hower you want, when they're clicked then their variable is set to "selected" and it then asks if the user is sure that they want to reroll those dice. It then goes into a series of if/elsif to re-roll each of the dice who's variable is set to "selected". I haven't run this through turing, so it's all been entered off the top of my head, but in theory when I run it through my head it should work. It's not the most basic way to do it, though I find this nice because it allows for more interactivity with the mouse.


: