Tiny Mastermind - Printable Version +- QB64 Phoenix Edition (https://staging.qb64phoenix.com) +-- Forum: QB64 Rising (https://staging.qb64phoenix.com/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://staging.qb64phoenix.com/forumdisplay.php?fid=3) +---- Forum: Programs (https://staging.qb64phoenix.com/forumdisplay.php?fid=7) +---- Thread: Tiny Mastermind (/showthread.php?tid=1580) |
Tiny Mastermind - johannhowitzer - 03-25-2023 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! Code: (Select All) do: input "How many possible letters (2-26)? ", code_colors%: loop while code_colors% < 2 or code_colors% > 26 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. RE: Tiny Mastermind - bplus - 03-26-2023 I like the tiny game! Works fine But about that line count Search is not helping find Pete's code so here it is instead of link: Code: (Select All) Print "Line count analysis...": Print ' 1 RE: Tiny Mastermind - johannhowitzer - 03-26-2023 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. Code: (Select All) input "How many possible letters, how many letters long? ", code_colors%, code_length% RE: Tiny Mastermind - bplus - 03-26-2023 Nice, I would probably set the number of letters and the length of code string and save more lines even idiot proofing the length of guess string, just like you get in the board game. But I guess that goes against the spirit of programming games with all kinds of options. RE: Tiny Mastermind - mnrvovrfc - 03-26-2023 I never understood the need to have as few lines of code as possible for any task. Colons were considered part of the problem of coding with interpreted BASIC. Leave them to Python I say. LOL that's why once in Purebasic forum there was a contest for the most impressive program with the least lines of code, precisely because that's how the demo version of the compiler was set up. They even wrote a program that checked another source code to make sure it qualified for the contest. It was still fairly easy to trick, since they were so full of macros. A bunch of raytracing examples and weak text-mode adventures, such as "Stupid-noid" but I've written that before and many more Pong-like things in SCREEN 0, which was a bit harder for the ones using that compiler. RE: Tiny Mastermind - johannhowitzer - 03-27-2023 "Need?" No, this was just a fun challenge for myself. I have a version of Mastermind that's longer and more fun to play, but I was bored at work with too much downtime as usual, and wondered how short I could make it while still keeping the gameplay intact. bplus, I suppose you could save one more line of code by removing the first INPUT statement, and replacing all instances of those two variables with hard numbers. I just found it very boring to play 6, 4 traditional games very quickly, so ended up playing with more letters and longer codes, and got better at the game overall. Even solved a 40-long code using every possible letter of the alphabet. |