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
should work with minimal changes under any other Rexx implementation ( Rexx TSO/CMS included )
just watch the NOT symbol