Instructions

Copy and paste the below code into a compatible MUSH or MUX.

MUSHCode for Mastermind

@@ Here's the @decompile out of a MasterMind game I wrote. I hope people find it entertaining.
@@ The 'Pegs' object is a function holder, give it to the MasterMind object or otherwise store
@@ it somewhere out of the way. This code was built in TinyMUSH 3.1p4

@create Pegs
&COLORS Pegs=red|R green|G blue|B yellow|Y white|W black|X magenta|M cyan|C
&PREFIX Pegs=%xiMasterMind:%xn
&NEW_GAME Pegs=[set(%0,GAME_%1:[iter(lnum(4),[first(shuffle(%=<COLORS>))])])][set(%0/GAME_%1,no_command)]
&DSP_BOARD Pegs=[space(4)][+--GUESS--R-R+%r[space(4)]|[iter([grep(%0,GUESS_%1_*,|)],#@ - [ljust([u(DSP_ROW,[get(%0/##)])],4)]|[ljust([get(%0/RESULT_RCWS_%1_#@)],1)]|[ljust([get(%0/RESULT_RCRS_%1_#@)],1)],,|%r[space(4)]|)]|%r[space(4)]+---------C-S+]
&DSP_ROW Pegs=[iter(%0,[ansi(last(##,|),%b)],,@@)]
&CLEAR_PLAYER Pegs=[wipe(%0/GAME_%1)][wipe(%0/GUESS_%1)][wipe(%0/GUESS_%1_*)][wipe(%0/RESULT_*_%1_*)][wipe(%0/DIFFICULTY_%1)][set(%0,PLAYERS:[squish(remove([get(%0/PLAYERS)],%1))])][trigger(%0/WIN_EFFECT,%1)]
&LOSS Pegs=[set(%0,RECORD_%1:[first(setr(0,first(get(%0/RECORD_%1))),|)]|[round(fdiv(first(%q0,|),add(first(%q0,|),last(%q0,|),1)),5)]|[add(last(%q0,|),1)] [capstr(name(%1))])][u(CLEAR_PLAYER,%0,%1)][trigger(%0/LOSS_EFFECT,%1)]
&WIN Pegs=[set(%0,RECORD_%1:[add(first(setr(0,first(get(%0/RECORD_%1))),|),1)]|[round(fdiv(add(first(%q0,|),1),add(first(%q0,|),last(%q0,|),1)),5)]|[last(%q0,|)] [capstr(name(%1))])][u(CLEAR_PLAYER,%0,%1)]
&SORT_AVG Pegs=[revwords(sort(%0))]
&DSP_RECORDS Pegs=[center([first(u(prefix),:)][ifelse(t(%2),%b%2)]%xn,40)]%r[ansi(%=<BORDER_COLOR_ANSI>,[repeat(_,40)])]%r%r[ljust(Player,20)][ljust(Win,5)][ljust(Loss,5)][ljust(Average,10)]%r[iter(%0,[ljust(strtrunc(#@. [last(setr(0,get(%1/##)))],20),20)][ljust(first(first(%q0),|),5)][ljust(last(first(%q0),|),5)][ljust(extract(%q0,2,1,|),10)],,%r)]%r[ansi(%=<BORDER_COLOR_ANSI>,[repeat(_,40)])]
&DIFFICULTY Pegs=10
@set Pegs/DIFFICULTY=no_command
&MULTIDIFF Pegs=1
@set Pegs/MULTIDIFF=no_command


@create MasterMind
@set MasterMind=COMMANDS
@parent MasterMind=Pegs
&shortdesc MasterMind=The classic game of codebreaking.
&pdesc MasterMind=A grey plastic board filled with holes. There are two trays attached to the top and bottom of the board, the lower one is filled with different colored pegs while the top holds only two colors. There is a plastic shield covering four holes at the top, where the codemaster will place his code.%r%rType %xh+mm/help%xn for rules and how to play.
@desc MasterMind=A grey plastic board filled with holes. There are two trays attached to the top and bottom of the board, the lower one is filled with different colored pegs while the top holds only two colors. There is a plastic shield covering four holes at the top, where the codemaster will place his code.%r%rType %xh+mm/help%xn for rules and how to play.
&PLAY_CMD MasterMind=$+mm/play:[ifelse(t(member(%=<PLAYERS>,%#)),pemit(%#,[[u(Prefix)] You are alteady playing MasterMind, use %xh+mm/leave%xn to stop.]),[pemit(%=<players>,[u(prefix)] %N joins the game.)][ifelse(hasattr(me,PLAYERS),[set(me,PLAYERS:%#)][set(me/PLAYERS,no_command)],[set(me,PLAYERS:%=<PLAYERS> %#)])][pemit(%#,[[u(prefix)] You have joined the name.][u(NEW_GAME,me,%#)][ifelse(not(hasattr(me,RECORD_%#)),[set(me,RECORD_%#:0|0|0 %N)][set(me/RECORD_%#,no_command)])])])]
&PLAY_DIF_CMD MasterMind=$+mm/play *:[ifelse(t(member(%=<PLAYERS>,%#)),pemit(%#,[[u(prefix)] You are already playing MasterMind, use %xh+mm/leave%xn to stop.]),[ifelse(%=<MULTIDIFF>,[ifelse([t(member(newbie apprentance journeyman master guru,%0))],[set(me,DIFFICULTY_%#:[switch(%0,newbie,50,apprentance,25,journeyman,10,master,5,guru,2)])][set(me/DIFFICULTY_%#,no_command)][pemit(%=<players>,[u(prefix)] %N joins the game.)][ifelse(hasattr(me,PLAYERS),[set(me,PLAYERS:%#)][set(me/PLAYERS,no_command)],[set(me,PLAYERS:%=<PLAYERS> %#)])][pemit(%#,[[u(prefix)] You have joined the name.][u(NEW_GAME,me,%#)][ifelse(not(hasattr(me,RECORD_%#)),[set(me,RECORD_%#:0|0|0 %N)][set(me/RECORD_%#,no_command)])])],[pemit(%#,[u(prefix)] I don't understand that difficulty setting.)])],pemit(%#,[u(prefix)] This game does not allow the changing of the difficulty. Use %xh+mm/play%xn to start a game at the Journeyman \(10\) level.))])]
&LEAVE_CMD MasterMind=$+mm/leave:[ifelse([t(member(%=<PLAYERS>,%#))],[[ifelse([t(get(me/GAME_%#))],[u(LOSS,me,%#)])][pemit(%#,[u(prefix)] You leave the game.)][pemit(%=<PLAYERS>,[u(prefix)] %N leaves the game.)]],pemit(%#,[[u(prefix)] You are not playing MasterMind, use %xh+mm/play%xn to play.]))]
&GUESS_CMD MasterMind=$^(\+mm/|)g(uess|) (red|green|blue|yellow|white|black|magenta|cyan)$:[ifelse([t(member(%=<PLAYERS>,%#))],[[ifelse(hasattr(me,GUESS_%#),[setq(GUESS,[get(me/GUESS_%#)])][ifelse(hasattr(me,GUESS_%#_%q<GUESS>),[ifelse([lt(words([get(me/GUESS_%#_%q<GUESS>)]),4)],[set(me,GUESS_%#_%q<GUESS>:[get(me/GUESS_%#_%q<GUESS>)] [extract(%=<COLORS>,[match(%=<COLORS>,%3|*)],1)])][pemit(%#,[[u(prefix)] You make your guess of [ansi([last([extract(%=<COLORS>,[match(%=<COLORS>,%3|*)],1)],|)],%b)].%r[u(DSP_BOARD,me,%#)]])],[pemit(%#,[u(prefix)] You have made your four guesses. Type %xh+mm/check%xn to check your guess or %xh+mm/clear%xn to clear it and try again.)])],[set(me,GUESS_%#_%q<GUESS>:[extract(%=<COLORS>,[match(%=<COLORS>,%3|*)],1)])][set(me/GUESS_%#_%q<GUESS>,no_command)][pemit(%#,[[u(prefix)] You make your guess of [ansi([last([extract(%=<COLORS>,[match(%=<COLORS>,%3|*)],1)],|)],%b)].%r[u(DSP_BOARD,me,%#)]])])],[set(me,GUESS_%#:1)][set(me/GUESS_%#,no_command)][set(me,GUESS_%#_1:[extract(%=<COLORS>,[match(%=<COLORS>,%3|*)],1)])][pemit(%#,[[u(prefix)] You make your guess of [ansi([last([extract(%=<COLORS>,[match(%=<COLORS>,%3|*)],1)],|)],%b)].%r[u(DSP_BOARD,me,%#)]])][set(me/GUESS_%#_1,no_command)])])]],pemit(%#,[u(prefix)] You are not currently playing MasterMind.))]
@set MasterMind/guess_cmd=regexp
&CLEAR_CMD MasterMind=$+mm/clear:[ifelse([t(member(%=<PLAYERS>,%#))],[ifelse(hasattr(me,GUESS_%#_[get(me/GUESS_%#)]),[wipe(me/GUESS_%#_[get(me/GUESS_%#)])][pemit(%#,[u(prefix)] Current guess cleared.%r[u(DSP_BOARD,me,%#)])],pemit(%#,[u(prefix)] There is nothing to clear.%r[u(DSP_BOARD,me,%#)]))],pemit(%#,[u(prefix)] You are not currently playing MasterMind. Type %xh+mm/play%xn to play.))]
&CHECK_CMD MasterMind=$+mm/check:[ifelse(t(member(%=<PLAYERS>,%#)),[ifelse(eq(words(setr(0,get(me/GUESS_%#_[setr(1,get(me/GUESS_%#))]))),4),[switch(%q0,[setr(3,get(me/GAME_%#))],[[pemit(%#,[u(PREFIX)] You are correct.)][u(WIN,me,%#)]],[ [set(me,RESULT_RCWS_%#_%q1:[words([iter(%q0,[iter(%q3,[switch(itext(0),##,1)])])])])][set(me/RESULT_RCWS_%#_%q1,no_command)][set(me,RESULT_RCRS_%#_%q1:[words([iter(%q0,[switch([extract(%q3,#@,1)],##,1)])])])][set(me/RESULT_RCRS_%#_%q1,no_command)][ifelse([eq(%q1,[ifelse(hasattr(me,DIFFICULTY_%#),get(me/DIFFICULTY_%#),%=<DIFFICULTY>)])],[pemit(%#,[u(prefix)] You have failed to crack the code. The correct answer was [u(DSP_ROW,[get(me/GAME_%#)])]. Better luck next time.)][u(LOSS,me,%#)],[set(me,GUESS_%#:[add(%q1,1)])][pemit(%#,[u(prefix)] The MasterMind looks over your guess.%r[u(DSP_BOARD,me,%#)])])] ])],pemit(%#,[u(PREFIX)] You have not made your four guesses.))],pemit(%#,[u(prefix)] You are not playing MasterMind.))]
&SAY_CMD MasterMind=$^(\+mm/say (.+)|m(.+[^who]))$:[ifelse(t(member(%=<PLAYERS>,%#)),[switch(%2,who,[[pemit(%#,[center([first(u(prefix),:)]%xn,30)]%r[ansi(%=<BORDER_COLOR_ANSI>,[repeat(_,30)])]%r%r[ljust(Player,20)][ljust(Win,5)][rjust(Loss,5)]%r[iter(%=<PLAYERS>,[ljust(strtrunc([capstr(name(##))],20),20)][center(first(setr(0,[first(get(me/RECORD_##))]),|),5)][center([last(%q0,|)],5)],,%r)]%r[ansi(%=<BORDER_COLOR_ANSI>,[repeat(_,30)])]%r)]],[pemit(%=<PLAYERS>,[u(prefix)] [speak(%#,[ifelse(t(%2),%2,%3)])])])],[pemit(%#,[u(prefix)] You must me playing MasterMind to use this command.)])]
@set MasterMind/SAY_CMD=regexp
&STAT_PLAYER_CMD MasterMind=$+mm/stat *:[ifelse(t([setr(0,[grepi(me,RECORD_*,%0)])]),[pemit(%#,[u(DSP_RECORDS,%q0,me,)])],pemit(%#,[u(prefix)] No matching players found.))]
&STAT_TOP10_CMD MasterMind=$+mm/stat:[pemit(%#,[u(DSP_RECORDS,[extract([munge(Pegs/SORT_AVG,[iter([setr(0,[grep(me,RECORD_*,|)])],extract(get(me/##),2,1,|))],%q0)],1,10)],me,Top 10)])]
&STAT_ALL_CMD MasterMind=$+mm/stat/all:[pemit(%#,[u(DSP_RECORDS,[grep(me,RECORD_*,|)],me,- All)])]
&HELP_CMD MasterMind=$+mm/help:@pemit %#=[center(%xiMasterMind Help%xn,78)]%r[center([%xiEasy to learn, Easy to play, Impossible to Master%xn],78)]%r[ansi(%=<BORDER_COLOR_ANSI>,[repeat(_,78)])]%r%r[border([MasterMind is the classic game of codebreaking. In this game the codebreaker \(that's you\) attempts to figure out the code set by the codemaster \(the computer\). There are eight possible colors used in the code; red%xR%b%xn, green%xG%b%xn, blue%xB%b%xn, yellow%xY%b%xn, magenta%xM%b%xn, cyan%xC%b%xn, white%xW%b%xn and black%xX%b%xn. \(The white/black colors may be difficult to see, depending on your client configuration.\)%r%rYou have 10 chances to uncover the hidden code. But the codemaster isn't going to just watch! When you are satisfied with the code you have entered he will check it and tell you how many colors are correct, and how many are in the correct place. But not which colors or places. Simple right?%r%r[space(5)]For an explanation of the board display type %xh+mm/help2%xn%r[space(5)]For a list of all commands used type %xh+mm/help3%xn%r[space(5)]For help on a specific command type %xh+mm/help <command>%xn],78)]%r[ansi(%=<BORDER_COLOR_ANSI>,[repeat(_,78)])]
&HELP2_CMD MasterMind=$+mm/help2:@pemit %#=[center(%xiMasterMind Help%xn,78)]%r[center([%xiEasy to learn, Easy to play, Impossible to Master%xn],78)]%r[ansi(%=<BORDER_COLOR_ANSI>,[repeat(_,78)])]%r%r[border([[space(4)]+--GUESS--R-R+%r[space(4)]|1 - %xB%b%xn%xR%b%xn%xC%b%xn%xY%b%xn|3|2|%r[space(4)]+---------C-S+%r%rWhile playing this game you will see a display very similar to the one above very often, this is called your board and is how you get information about your code. Each guess you make appears on it's own row, the first part is the guess that you made. The colored boxes are your guess. The next number \(the 3 in the above example\) tell you the number of colors that are correct in the code. Note: multiple instances of the same color count for each appearence in either direction. If the code has two blues in it and you put down a blue you will see a 2 in this place. Likewise, if you place 2 blues in your guess but there is only one in the code you will also have a 2 here.%r%rThe next number tells you how many of the colors you have in the correct location. When all four numbers are correct you will be told you have cracked the code.],78)]%r%r[space(5)]For the rules of the game type %xh+mm/help%xn.%r[space(5)]For a list of all commands used type %xh+mm/help3%xn%r[space(5)]For help on a specific command type %xh+mm/help <command>%xn%r[ansi(%=<BORDER_COLOR_ANSI>,[repeat(_,78)])]
&HELP3_CMD MasterMind=$+mm/help3:@pemit %#=[center(%xiMasterMind Help%xn,78)]%r[center([%xiEasy to learn, Easy to play, Impossible to Master%xn],78)]%r[ansi(%=<BORDER_COLOR_ANSI>,[repeat(_,78)])]%r%rThe following commands are used to play:%r%r[ljust(%xh+mm/play%xn,17)] Start playing a game.%r[ljust(%xh+mm/leave%xn,17)] Leave the current game %xrWARNING:%xn Counts as a loss.%r[ljust(%xh+mm/guess <color>%xn,17)] Place <color> peg in the next open slot.%r[space(18)]Can be abbreavated as %xhb <color>%xn%r[ljust(%xh+mm/clear%xn,17)] Clears your current guess.%r[ljust(%xh+mm/check%xn,17)] Submit your current guess to the codemaster.%r[ljust(%xh+mm/say <message>%xn,17)] Allows you to speak with the other players.%r[space(17)] Can also be abbrevated as %xhm<message>%xn.%r[ljust(%xh+mm/stat%xn,17)] See player statitstics.%r[ljust(%xh+mm/help%xn,17)] These messages.%r%r[space(5)]For the rules of the game type %xh+mm/help%xn.%r[space(5)]For an explanation of the board display type %xh+mm/help2%xn%r[space(5)]For help on a specific command type %xh+mm/help <command>%xn%r[ansi(%=<BORDER_COLOR_ANSI>,[repeat(_,78)])]
&CMD_HELP_CMD MasterMind=$+mm/help *:@pemit %#=[center(%xiMasterMind Help%xn,78)]%r[center([%xiEasy to learn, Easy to play, Impossible to Master%xn],78)]%r[ansi(%=<BORDER_COLOR_ANSI>,[repeat(_,78)])]%r%r[border([switch([remove(%0,+mm,/)],[play],[Command: +mm/play \\\[<difficulty>\\\]%r%rJoin the game and have the codemaster create a code for you to crack. Supplying a <difficulty> will start a game at that level. The different levels give you more or fewer guesses. The levels are; newbie \\\(50\\\), apprentance \\\(25\\\), journeyman \\\(10\\\), master \\\(5\\\) and guru \\\(2\\\). The Journeyman level is the default for most games. Not all games allow you to specify a difficulty.],[leave],[Command: +mm/leave%r%rLeave the game in progress. This will count as a loss for you.],[guess],[Command: +mm/guess <color>%rShortcut: g <color>%r%rPlaces <color> in the next open slot. <color> must be one of: red%xR%b%xn, green%xG%b%xn, blue%xB%b%xn, yellow%xY%b%xn, magenta%xM%b%xn, cyan%xC%b%xn, white%xW%b%xn or black%xX%b%xn.],[clear],[Command: +mm/clear%r%rClears your current guesses from the board and allows you to start over. Does not effect the previos guesses.],[check],[Command: +mm/check%r%rSubmits your current guess to the codemaster. You will be told how many colors are correct and how many are in the correct locations, but not which color is which.],[say],[Command: +mm/say <message>%rShortcut: m<message>%r%rEmits <message> to all players currently playing MasterMind. Please use this rather than normal say and pose to help reduce screen spam for players not involved in the game. The shortcut must not have a space after the inital 'm' putting a space there will result in a 'You can't go that way.' error. I have no idea why.],[stat*],[Command: +mm/stat\\\[/all\\\] \\\[<string>\\\]%r%rWhen used without switches or arguments, this command will list the top 10 MasterMind players. When supplied with the /all switch this command will list the records for every player who has played on this MasterMind. The argument <string> can be used to only display players whos name starts with <string>.],[help*],[Command: +mm/help\\\[2|3\\\] \\\[<command>\\\]%r%rProvides help and information about MasterMind and this object.],[Command '%0' not found.])],78)]%r[ansi(%=<BORDER_COLOR_ANSI>,[repeat(_,78)])]