#! /bin/sed -rf
# Usage: sed -rf sedermind.sed ou ./sedermind.sed
# Please use Gnu Sed
# Sedermind #
2 b igen
1 !b check
i\
Please enter a random word or sentence to generate a code.\
(at least 4 letters)
b end
:igen
# lowercase input
s/.*/\L&/
s/[^a-z]//g
# We need to generate a code for mastermind ([1-8]{4})
# Since sed has no random function (and can't access to /dev/random),
# we ask the user to enter a random string (at least 4 letters)
# and we generate a code from this input.
:gen
y/abcdefghijklmnopqrstuvwxyz12345678/bcdefghijklmnopqrstuvwxyz123456781/
:l
t l
s/^(.*)(.)$/\2\1/
s/^1(.)/\15/
/^[1-8]*$/ !b gen
s/(....).*/\1/
s/$/\nGuess the code (4 digits between 1 and 8) [........]/
x
i\
\
'o' is placed for each correct digit (value and position) \
'x' is placed for each digit with bad position
b end
# Tester user guess.
:check
G
s/\n/:/
s/\n.*//
/^(.*):\1$/ b vict
x
s/\./*/
/\./ !b lose
x
# Correct digits
:loop1
/([1-8]).{4}\1/ {
s/(.)(.{4})\1/ \2o/
}
t loop1
# Digits with bad position
:loop2
s/([1-8])(.*:.*)\1/\2x/
t loop2
# Clean things and display
s/.*://
s/[^ox]//g
:m
t m
s/$/ /
s/^(....).*/\1/
s/.*/ => '&'/
p
i\
b end
:vict
i\
Victory is yours.
Q
:lose
i\
Loser. \
Answer was:
x
s/.*://
q
:end
g
s/.*\n//p
s/.*//
d
### colorized by sedsed, a debugger and code formatter for sed scripts
### original script: http://laurent.le-brun.eu/pub/sedermind.sed