Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Mastermind in 2 lines

by jsrn (Sexton)
on Jul 21, 2003 at 17:15 UTC ( [id://276359]=obfuscated: print w/replies, xml ) Need Help??

This one isn't so much about obfuscation, but more about brevity:
#!/usr/bin/perl -l $c.=1+int rand 6for 6..9;while(<>){/^\d{4}$|!/||next;$_.=$c;4while s+(.)(.{4})\1+?$2*+s;4while s-(\d)(.*\n.*)\1-+$2-;print/[*+]/g;/\*{4}| +!/&&die}
Game rules:
The program generates a secret four-digit number, each digit ranging from 1 to 6. The object of the game is to guess this number. In order to achieve this, the player enters a four-digit number consisting of digits from 1 to 6. Now the program does its output:
'*' for a digit that has correct value and position
'+' for a digit that has correct value and wrong position
If the correct number is entered, the game is over and the program quits. Entering '!' also quits the game.
Enjoy!

Replies are listed 'Best First'.
Re: Mastermind in 2 lines
by chimni (Pilgrim) on Jul 29, 2003 at 10:42 UTC
    Hi,
    can you dissect this for me
     I am new at this .How does the pattern work
      1 while s!(.)(.{4})\1!?$2*!s;  # Match in right pos
    regards,
    rajdeep
    
      It's cute... Since the "secret" is concatenated with what the user entered, this will match if you get the same number separated by 4 characters (that's 3 digits and the \n in some combination).

      It replaces that with a ? for the first position, and a * for the 2nd, with $2 (the 4 characters in between) in the middle. Then, the substitution is repeated as long as it succeeds, so if you have all 4 characters right, you get 4 *'s.

      The 2nd substitution puts in +'s for any numbers that appear in both lines after the first substitutionwas done, which will get only numbers in the wrong place since all the ones in the right spots are already replaced with '*'.

      The last print will print out any * or + in the string, telling you how many you have right or nearly right.

      Very nice script... I like the cool uses of s/// and the print of the matches.
      --
      Mike

Re: Mastermind in 2 lines
by YAFZ (Pilgrim) on Jul 23, 2003 at 13:26 UTC
    ++ for such a nice and cool implementation. These kind of programs motivate me to learn Perl better. The beauty in simplicity is impressive indeed!
      Of course, the fact that this is even possible gives rise to the occasional assertion that Perl is a "write only" language. It's great to be able to get such brevity, should brevity be something you desire in a particular situation, but it's pretty mean to force such brevity onto an unwitting maintainer. :-)
        Here's a version run through Perltidy - not too hard to understand if you know Perl regular expressions. The $c is the secret, $_ is the guess, and the code depends on matching the guess concatenated with the secret. Interesting code, and not a bad example of Perl really - it's just that the traditional scrunched-up formatting makes it look like line noise. I also made some minor changes for understandability/legibility. Of course, I realise this is entirely against the idea of obfuscated code :)
        #!/usr/bin/perl -l $c .= 1 + int rand 6 for 1 .. 4; # Build up the secret while (<>) { /^\d{4}$|!/ || next; # Check user entered 4 digits $_ .= $c; 1 while s!(.)(.{4})\1!?$2*!s; # Match in right position 1 while s!(\d)(.*\n.*)\1!+$2!; # Match somewhere print /[*+]/g; # Print matches /\*{4}|!/ && exit; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: obfuscated [id://276359]
Approved by sschneid
Front-paged by sauoq
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-24 22:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found