Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

looping - path problem

 [1/6] from: rcm:formix:au at: 25-Apr-2006 15:03


Hello Rebol-Org, I am having a problem trying to "shrink" some code. These are the initial values - random/seed now digits: [1 2 3 4 5 6 7 8 9 0] target: copy [] ;;; then target becomes a fairly random 4-digit number target: to-string random/secure digits remove/part target 6 I want to compare the target with the user's answer, but I can't make a for or repeat loop work because I get invalid path errors. Even though it returns approptiate results, what follows is obviously inefficient so I would be very glad to know the way to make it leaner. Regards, Rosemary get-answer: does [ ans: to-string num/text answer: copy [] bulls: shots: 0 if find target ans/1 [ either ans/1 == target/1 [bulls: bulls + 1] [shots: shots + 1]] if find target ans/2 [ either ans/2 == target/2 [bulls: bulls + 1][shots: shots + 1]] if find target ans/3 [ either ans/3 == target/3 [bulls: bulls + 1][shots: shots + 1]] if find target ans/4 [ either ans/4 == target/4 [bulls: bulls + 1][shots: shots + 1]] tries: tries + 1 either ( tries <= 9 ) [answer: rejoin ["0" tries ": " ans " " bulls " " shots]] [answer: rejoin [tries ": " ans " " bulls " " shots]] ] -- Formix Australia - www.formix.com.au 613 9354 9585 PO Box 261 Nth Carlton Vic 3054

 [2/6] from: tomc:cs:uoregon at: 24-Apr-2006 22:16


hi Rosemary, have you used the :word notation for the variable in your path (think of it as getting the value out of the word instead of putting it in) repeat i 4[ either equal? ans/:i target/:i [bulls: bulls + 1] [shots: shots + 1] ] Rosemary de Dear wrote:
> Hello Rebol-Org, > I am having a problem trying to "shrink" some code. These are the initial
<<quoted lines omitted: 39>>
> 613 9354 9585 > PO Box 261 Nth Carlton Vic 3054
-- ... nice weather eh tomc-cs.uoregon.edu

 [3/6] from: pwawood:g:mail at: 25-Apr-2006 14:05


You could use foreach to iterate through one series to find the number of bulls-eyes (but you have to handle the iteration of the other series yourself) and then use intersect of the two series to determine the number of shots: foreach attempt ans [ if attempt = first target [bulls: bulls + 1] ans: next ans ] shots: shots + length? intersect head ans target Regards Peter On Tuesday, Apr 25, 2006, at 13:03 Asia/Kuala_Lumpur, Rosemary de Dear wrote:

 [4/6] from: tomc::cs::uoregon::edu at: 24-Apr-2006 23:06


Hi Peter, I had looked at the much cleaner 'intersect as well but rejected it because it treats it's arguments as 'sets' in the mathematical sense... that is, order does not mater. which I do not think ia appropiate in this case Peter Wood wrote:
> You could use foreach to iterate through one series to find the number > of bulls-eyes (but you have to handle the iteration of the other series
<<quoted lines omitted: 78>>
>>lists at rebol.com with unsubscribe as the subject. >>
-- ... nice weather eh tomc-cs.uoregon.edu

 [5/6] from: pwawood:gma:il at: 25-Apr-2006 14:39


Hi Tom That's why I used foreach to get the position dependent bulls-eyes and intersect to get the position independent shots. (I hope I understood the scoring system properly). It would have helped if there wasn't an error in my code. Here is the revised version: foreach attempt ans [ if attempt = first target [bulls: bulls + 1] target: next target ] shots: shots + length? intersect ans target: head target Actually, I suspect there may be a better solution using parse. Regards Peter On Tuesday, Apr 25, 2006, at 14:06 Asia/Kuala_Lumpur, Tom Conlin wrote:

 [6/6] from: rcm::formix::com::au at: 25-Apr-2006 19:40


Dear Tom and Peter, Thank you both very much for your suggestions and code fragments. I had tried something like either ans/:i == target/:i but must have got it wrong, and had thought of parsing as my next option. I still might, but for now ... success!!! So here is "Bull and Shots" a little game we played at school when things got dull. Thank you again, Rosemary rebol [Title: "Bulls and Shots" File: %bulls-and-shots.r] random/seed now digits: [1 2 3 4 5 6 7 8 9 0] target: copy [] main: layout [ origin 30x30 backdrop water effect [gradient 0x1] box "Bulls and Shots" 360x30 font-size 20 water white edge [size: 1x1 color: pewter] at 50x100 guide btn 100x25 tan "New Game" #"n" [new-game] num: field 100x25 font-size 16 center middle btn 100x25 tan "Guess" #"g" [get-answer show-answer] btn 100x25 tan "Quit" #"q" [quit] at 30x230 guide text as-is white bold { H O W T O P L A Y * Hit New Game or "n" * Enter four digits 0 - 9 * Hit Guess or "g" * Maximum tries = 12 * There are no repeat digits * B U L L - right digit, right place * S H O T - right digit, wrong place} at 220x75 guide h4 "Try: Guess Bulls Shots" 165x20 snow water pad 0x-8 results: area 165x240 font-size 17 wrap pad 80x10 box "Peek" 80x25 font-size 16 water white edge [size: 1x1 color: tan] feel [over: func [face act pos] [ face/text: either act [copy target]["Peek"] show face ]] ] new-game: does [ clear results/text results/line-list: none show results tries: 0 target: copy [] target: to-string random/secure digits remove/part target 6 focus num ] get-answer: does [ ans: to-string num/text answer: copy [] bulls: shots: 0 repeat i 4 [ if find target ans/:i [ either ans/:i == target/:i [bulls: bulls + 1] [shots: shots + 1] ]] tries: tries + 1 either ( tries <= 9 ) [answer: rejoin ["0" tries ": " ans " " bulls " " shots]] [answer: rejoin [tries ": " ans " " bulls " " shots]] ] show-answer: does [ append results/text answer append results/text newline show results if bulls = 4 [alert form ["Bravo - you guessed it !"]] if tries = 12 [alert form ["Game over - no guesses left."]] clear num/text focus num ] center-face main view main do-events -- Formix Australia - www.formix.com.au 613 9354 9585 PO Box 261 Nth Carlton Vic 3054

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted