a simple guess game

Share a quote, a general thought, jokes or one liners here.
Post Reply
enrico-sorichetti
Global Moderator
Global Moderator
Posts: 825
Joined: Wed Sep 11, 2013 3:57 pm

a simple guess game

Post 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
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort 8-)
Leena
Registered Member
Posts: 30
Joined: Fri Aug 09, 2013 10:17 pm

Re: a simple guess game

Post by Leena »

Hi,

How to play this game?
User avatar
Anuj Dhawan
Founder
Posts: 2799
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Contact:
India

Re: a simple guess game

Post by Anuj Dhawan »

First install Open Object Rexx. Copy this code, paste in the editor of OORexx and try your luck! ;)
Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
enrico-sorichetti
Global Moderator
Global Moderator
Posts: 825
Joined: Wed Sep 11, 2013 3:57 pm

Re: a simple guess game

Post by enrico-sorichetti »

apart the comments 
should work everywhere 
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort 8-)
zprogrammer
Global Moderator
Global Moderator
Posts: 588
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars

Re: a simple guess game

Post by zprogrammer »

I think this was a enhanced version of code from what I started :)
zprogrammer
enrico-sorichetti
Global Moderator
Global Moderator
Posts: 825
Joined: Wed Sep 11, 2013 3:57 pm

Re: a simple guess game

Post 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 )
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort 8-)
Leena
Registered Member
Posts: 30
Joined: Fri Aug 09, 2013 10:17 pm

Re: a simple guess game

Post by Leena »

Thanks. Playing with it on OORexx. :)
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “Thought of the Day, General Talk & Jokes.”