Page 1 of 1

How to repeat one part of a loop

Posted: Fri Feb 04, 2011 3:09 am
by dburdan
Hello, I am trying to repeat one part of a loop. For example:

Code: Select all

repeat with i = 1 to 4
  put random(1,52) into x
  if NoRepeatList contains x then
   ::Repeat this part of the loop::
  end if
  add x to NoRepeatList
end repeat
If that makes any sense I want it to check if one if the numbers is already in "NoRepeatList". If it is, I want it to retry that part of the loop again without starting the whole loop over. Does anybody know how I could go about doing this?

Thanks in advance

Re: How to repeat one part of a loop

Posted: Fri Feb 04, 2011 1:07 pm
by BvG
repeat until x is not among the lines of NoRepeatList
put random(52) into x
end repeat
put x & return after NoRepeaList

Re: How to repeat one part of a loop

Posted: Fri Feb 04, 2011 7:06 pm
by dburdan
That unfortunately doesn't work for my situation. I'm trying to generate 4 cards from a deck without generating the same card twice. This is what I have so far that works great but will occasionally generate the same card. Later on in the game I will need to generate 9 cards the same way.

Code: Select all

on generateAllCards
   repeat with i = 1 to 4
      put randomNum(1,52) into y
   if y >9 then
      put "50" & y into x
    else
       put "500" & y into x
    end if
    
    if i = 1 then
       put x into vCard1
       end if
    if i = 2 then
       put x into vCard2
    end if
     if i = 3 then
       put x into vCard3
    end if
    if i = 4 then
       put x into vCard4
    end if
       set the icon of button ("card" & i) to x
       put GetValue(value(vCard1)) into field "vCard1"
       put GetValue(value(vCard2)) into field "vCard2"
       put GetValue(value(vCard3)) into field "vCard3"
       put GetValue(value(vCard4)) into field "vCard4"
   end repeat
end generateAllCards

Re: How to repeat one part of a loop

Posted: Fri Feb 04, 2011 8:01 pm
by BvG
the code does what you want, but of course you need to add it into your existing code, duh.

Re: How to repeat one part of a loop

Posted: Fri Feb 04, 2011 8:11 pm
by dburdan
BvG wrote:the code does what you want, but of course you need to add it into your existing code, duh.
Then how would I implement it into the code above. I only want it to run 4 times & i don't want it to stop looping until it gets to 4. I don't want it to keep repeating until it generates the same number twice. I just want it generate a new number if the number it tried to generate has already been generated.

Re: How to repeat one part of a loop

Posted: Sat Feb 05, 2011 1:23 am
by BvG
exactly. you want to repeat trying to generate the random number, until it is not among your existing numbers. then you do that four times.