Page 1 of 1

a simple guess game

Posted: Thu Apr 28, 2016 1:01 pm
by enrico-sorichetti
 Just found it cleaning up my PCs

Code: Select all

#!/usr/bin/env rexx

-- length of target string
_ln_ = 5

-- chars that can compose the target string

_ch_ = "abcdefghijklmnopqrstuvwxyz"
_ch_ = "0123456789"
if  ( _ln_ > length(_ch_) ) then do
    say "***** oh s**t!"
    exit
end

say "***** let' s guess a string of length "_ln_
say "***** with chars chosen in >"_ch_"<"
say "***** with no repeating chars"

say "***** a bull is a right char in the right place"
say "***** a cow  is a right char in the wrong place"
say

-- build the target string
targt = ""
do  p = 1 to _ln_
    i = random(1, length(_ch_))
    c = substr(_ch_, i, 1)
    do  while ( pos(c, targt) \= 0 )
        i += 1
        c = substr(_ch_, (i-1)//length(_ch_)+1, 1)
    end
    targt = targt || c
end

-- comment for the real thing
say "*****" targt "<<<<< to cheat or not to cheat ?"
say

do  count = 1
    say "***** enter Your guess" copies(".",_ln_) "or just enter to give up"
    parse pull guess
    if guess = "" then do
        say "***** game ended per user request after" count-1 "attempts"
        exit
    end

    if  \isgood(guess) then do
        say "***** invalid guess string" guess
        iterate
    end

    bulls = 0
    cows  = 0
    do i = 1 to _ln_
        do  j = 1 to _ln_
            if  ( substr(guess,i,1) = substr(targt,j,1) ) then do
                if  i = j then ,
                    bulls += 1
                else ,
                    cows += 1
            end
        end
    end

    if bulls = _ln_ then do
        say "***** Congratulations! You guessed right after "count "attempts"
        exit
    end

    say "***** bulls" bulls "cows" cows "for attempt" count
    say

end

exit

isgood:
    parse arg ws
    if  length(ws) \= _ln_ then ,
        return 0
    if  verify(ws, _ch_) \= 0 then ,
        return 0
    do i = 1 to _ln_-1
        do  j = i+1 to _ln_
            if  ( substr(ws,i,1) = substr(ws,j,1) ) then ,
                return 0
        end
    end
    return 1
written for Open Object Rexx
should work with minimal changes under any other Rexx implementation ( Rexx TSO/CMS included )
just watch the NOT symbol

Re: a simple guess game

Posted: Thu Apr 28, 2016 2:51 pm
by Leena
Hi,

How to play this game?

Re: a simple guess game

Posted: Thu Apr 28, 2016 3:36 pm
by Anuj Dhawan
First install Open Object Rexx. Copy this code, paste in the editor of OORexx and try your luck! ;)

Re: a simple guess game

Posted: Thu Apr 28, 2016 4:21 pm
by enrico-sorichetti
apart the comments 
should work everywhere 

Re: a simple guess game

Posted: Thu Apr 28, 2016 9:01 pm
by zprogrammer
I think this was a enhanced version of code from what I started :)

Re: a simple guess game

Posted: Thu Apr 28, 2016 9:38 pm
by enrico-sorichetti
I think this was a enhanced version of code from what I started
NOPE... 
it was a repost of what I had already posted in the original topic ( forgot about it )

Re: a simple guess game

Posted: Thu May 05, 2016 2:10 pm
by Leena
Thanks. Playing with it on OORexx. :)