Created
May 8, 2018 02:32
-
-
Save z-rui/84c2cb0f35dc118bf2945e5471a63113 to your computer and use it in GitHub Desktop.
The Moo game written in TeX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\def\writeln#{\immediate\write16} | |
\def\readln#1{{\catcode`\^^M=9 \global\read16 to#1}} | |
\newcount\seed \seed=1 | |
\def\rand{\the\seed\randnext} | |
\def\modulo#1#2{{\count255=#1\divide\count255 by#2\multiply\count255 by#2% | |
\global\advance#1by-\count255}} | |
\def\randnext{{% | |
\global\multiply\seed by 23333 | |
\global\advance\seed by 12345 | |
\modulo\seed{32768}}} | |
\seed=\time | |
\newcount\n \newcount\i | |
\randnext \randnext | |
\def\digits{} | |
\i=0 \loop | |
\csname newif\expandafter\endcsname\csname ifhas\the\i\endcsname | |
\csname newif\expandafter\endcsname\csname ifhad\the\i\endcsname | |
\ifnum\i<10 \advance\i by 1 \repeat | |
\newif\ifagain \newif\ifhas | |
\i=1 \loop | |
{\loop \n=\rand \modulo\n{10} | |
\expandafter\let\csname ifagain\expandafter\endcsname | |
\csname ifhas\the\n\endcsname | |
\ifagain \repeat} | |
\csname has\the\n true\endcsname | |
\edef\digits{\digits\the\n} | |
\ifnum\i<4 \advance\i by 1 \repeat | |
\newcount\A \newcount\B | |
\def\getone#1to#2{\expandafter\dogetone\expandafter#1\expandafter#2#1\relax} | |
\def\dogetone#1#2#3#4\relax{\def#2{#3}\def#1{#4}} | |
\def\calcAB{\global\A=0 \global\B=0 | |
{\loop | |
\ifx\digits\empty \againfalse | |
\else \againtrue | |
\getone\digits to\c \getone\guess to\d | |
\ifx\c\d\relax\relax | |
\global\advance\A by 1 | |
\else | |
\expandafter\let\csname ifhas\expandafter\endcsname | |
\csname ifhas\d\endcsname | |
\ifhas \global\advance\B by 1 \fi | |
\fi | |
\fi | |
\ifagain \repeat}} | |
\newif\ifvalid | |
\def\checkvalid{\global\validtrue{\n=0 \loop | |
\ifx\guess\empty \againfalse | |
\else \advance\n by 1 \againtrue | |
\getone\guess to\d | |
\expandafter\count255\expandafter`\d \relax | |
\ifnum\count255<`0 \global\validfalse \fi | |
\ifnum\count255>`9 \global\validfalse \fi | |
\ifvalid | |
\expandafter\let\csname ifhas\expandafter\endcsname | |
\csname ifhad\d\endcsname | |
\ifhas \global\validfalse \fi | |
\csname had\d true\endcsname | |
\fi | |
\ifvalid\else \againfalse \fi | |
\fi | |
\ifagain \repeat \ifnum\n=4\else \global\validfalse \fi}} | |
\loop \readln\guess | |
\checkvalid | |
\ifvalid | |
\calcAB \writeln{\the\A A\the\B B} | |
\else | |
\writeln{invalid} | |
\fi | |
\ifnum\A<4 \repeat | |
\bye |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment