03-26-2023, 01:36 PM
(This post was last modified: 03-26-2023, 01:47 PM by johannhowitzer.)
Okay, not only did I conform to your definition of a line, I did so while saving 11 lines. So this is now Mastermind in 27 lines, but now you also can't argue otherwise AND several things were optimized. More lines can be saved if I give up on QBasic compatibility and incorporate the _CONTINUE statement. Please note the following changes carefully, as user input and feedback has changed:
- No do loop to make sure player is not an idiot. If you say you want 30 different letters, get ready for punctuation. If you do something even crazier, you may crash the game. Ditto for the length check on each attempt; if it doesn't match the solution's length, something bad may happen. Maybe your milk in the fridge will spoil.
- Starting parameters set by the player are contained in the same INPUT, so you have to enter both at the same time, separated by a comma. (I should add that this is the ONLY time I have ever found a practical use for this quirk of INPUT, it's always just been a nuisance.)
- Conditionals that contained colons have been repackaged into two duplicate conditional lines, to avoid a block IF. In doing this, I noticed a couple ways the largest hierarchy could be pruned.
- The feedback is no longer peg-counting numerical variables, now it's a single string, and I avoid giving away extra information by appending "B" (for black peg) to the start, and "W" (white) to the end of the string. This doesn't save lines by my definition, but by using one variable instead of two, does save a colon. This did make it too awkward to keep the victory/end-program condition on the final LOOP, without adding more lines. So now you just win when you get enough black pegs, the program won't end.
- No do loop to make sure player is not an idiot. If you say you want 30 different letters, get ready for punctuation. If you do something even crazier, you may crash the game. Ditto for the length check on each attempt; if it doesn't match the solution's length, something bad may happen. Maybe your milk in the fridge will spoil.
- Starting parameters set by the player are contained in the same INPUT, so you have to enter both at the same time, separated by a comma. (I should add that this is the ONLY time I have ever found a practical use for this quirk of INPUT, it's always just been a nuisance.)
- Conditionals that contained colons have been repackaged into two duplicate conditional lines, to avoid a block IF. In doing this, I noticed a couple ways the largest hierarchy could be pruned.
- The feedback is no longer peg-counting numerical variables, now it's a single string, and I avoid giving away extra information by appending "B" (for black peg) to the start, and "W" (white) to the end of the string. This doesn't save lines by my definition, but by using one variable instead of two, does save a colon. This did make it too awkward to keep the victory/end-program condition on the final LOOP, without adding more lines. So now you just win when you get enough black pegs, the program won't end.
Code: (Select All)
input "How many possible letters, how many letters long? ", code_colors%, code_length%
dim match%(code_length%)
randomize timer
for n = 1 to code_length%
solution$ = solution$ + chr$(int(rnd * code_colors%) + asc("a"))
next n
do
input "> ", attempt$
pegs$ = ""
for n = 1 to code_length%
if mid$(attempt$, n, 1) = mid$(solution$, n, 1) then match%(n) = n else match%(n) = 0
if mid$(attempt$, n, 1) = mid$(solution$, n, 1) then pegs$ = "B" + pegs$
next n
for n = 1 to code_length%
for n1 = 1 to code_length%
if match%(n) = 0 and n1 <> n and mid$(attempt$, n, 1) = mid$(solution$, n1, 1) then
m = 0
for n2 = 1 to code_length%
if match%(n2) = n1 then m = -1
next n2
if m = 0 then match%(n) = n1
if m = 0 then pegs$ = pegs$ + "W"
end if
next n1
next n
print pegs$
loop