I debated adding this to my original obfuscated Mastermind perl, but it is a different script now which I worked hard to re-write, and despite changing the functionality pretty significantly, I was able to do it in only three characters more.
sub q{$;=1;$q='';$q.=int(rand(9))for(5..8)}sub _{print@_}&q;for(;;){$:
+=$~=0;_
"$/$; ";chop($_=<>);if(/^\d{4}$/){$_.=q,_._,.$q;$:++while+s/(.)(.{6})\
+1/;$2,/
;$~++while+s/(.)(...+)\1/;$2$1/;_"$~W$:R ";++$;;$:==4|$;>10&&_($q)&&&q
+}}
(Though it was my idea to use substitution to evaluate the guess, thanks to
diotalevi for showing me how to implement my idea:
Re: Replace zero-width grouping?),
This game follows the official
Mastermind Instructions with the only difference being that I use 0-9, instead of 6 colors, to comprise the "code".
Added - How to play:
Consult the
Mastermind Instructions if you are unfamiliar with how the original game is played. This demonstration shows how my script is used to play the game:
perl mind.pl
1 1234
Program prompts with '1', which indicates the first guess. Player guesses '1234'.
1W0R
Program responds with 1 white peg and no red pegs.
2 5678
1W0R
3 9900
0W0R
4 1122
0W2R
5 1155
0W0R
6 6622
0W2R
7 7722
0W2R
8 8822
0W4R 8822
4 red pegs followed by the answer shows the player guessed the code correctly before his ten tries were exceeded.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.