03-25-2023, 11:45 PM
(This post was last modified: 03-26-2023, 02:51 PM by johannhowitzer.)
NOTE: Later down there's an updated version that is more optimized and does away with colons.
I present to you the game Mastermind, in 25 lines of code. Technically a little more because of
colons, but all the colons here are fine I think. For tiny little one-statement DO and FOR loops,
I find it's more readable like this; likewise for concatenating a handful of simple results of an
IF statement, to avoid a block IF...END IF. For much larger programs, it makes understanding
program flow much less convoluted, since my eyes have less nesting hierarchy to navigate.
Also, this code works in QBasic!
This operates exactly like Mastermind as you know it, except with lowercase letters instead of
colored pegs. You specify how many letters are possible, and how long the solution is. So, if
you say 12 possible letters and 4 letters long, the game will use the letters a through l, and
produce solutions such as "glhf" and "dljk." If you say 2 possible letters and 15 letters long,
it will produce things like "baabbbabaababba."
Guesses must be of the specified length, and must be in all lowercase. The game will signal
a correct letter in correct position with a "black peg," just like a typical Mastermind set,
and will signal a correct letter that is in the wrong position with a "white peg." For example,
if the solution is "hall," and you guess "hola," you will get two black pegs for the first h and
the third l, and one white peg for the a, which is not in the second position. If the solution
is "other," and you guess "trout," you will get three white pegs, not four, since the solution
only has one t.
I present to you the game Mastermind, in 25 lines of code. Technically a little more because of
colons, but all the colons here are fine I think. For tiny little one-statement DO and FOR loops,
I find it's more readable like this; likewise for concatenating a handful of simple results of an
IF statement, to avoid a block IF...END IF. For much larger programs, it makes understanding
program flow much less convoluted, since my eyes have less nesting hierarchy to navigate.
Also, this code works in QBasic!
Code: (Select All)
do: input "How many possible letters (2-26)? ", code_colors%: loop while code_colors% < 2 or code_colors% > 26
do: input "How many letters long? ", code_length%: loop while code_length% < 1
dim match%(code_length%)
randomize timer
for n = 1 to code_length%: solution$ = solution$ + chr$(int(rnd * code_colors%) + asc("a")): next n
do
do: input "> ", attempt$: loop until len(attempt$) = code_length%
black_pegs% = 0: white_pegs% = 0
for n = 1 to code_length%
if mid$(attempt$, n, 1) <> mid$(solution$, n, 1) then match%(n) = 0 else match%(n) = n: black_pegs% = black_pegs% + 1
next n
for n = 1 to code_length%
if match%(n) = 0 then
for n1 = 1 to code_length%
if n1 <> n and mid$(attempt$, n, 1) = mid$(solution$, n1, 1) then
for n2 = 1 to code_length%
if match%(n2) <> n1 then m = 0 else m = -1: exit for ' Check for pre-existing white peg
next n2
if m = 0 then match%(n) = n1: white_pegs% = white_pegs% + 1: exit for
end if
next n1
end if
next n
print black_pegs%; "black,"; white_pegs%; "white"
loop while black_pegs% < code_length%
This operates exactly like Mastermind as you know it, except with lowercase letters instead of
colored pegs. You specify how many letters are possible, and how long the solution is. So, if
you say 12 possible letters and 4 letters long, the game will use the letters a through l, and
produce solutions such as "glhf" and "dljk." If you say 2 possible letters and 15 letters long,
it will produce things like "baabbbabaababba."
Guesses must be of the specified length, and must be in all lowercase. The game will signal
a correct letter in correct position with a "black peg," just like a typical Mastermind set,
and will signal a correct letter that is in the wrong position with a "white peg." For example,
if the solution is "hall," and you guess "hola," you will get two black pegs for the first h and
the third l, and one white peg for the a, which is not in the second position. If the solution
is "other," and you guess "trout," you will get three white pegs, not four, since the solution
only has one t.