Bletchley Completed
But It Doesn’t End Well!
It’s been a very long week or so for my Mastermind type game, and my brain.
I made such a mess of the code, I got lost in a tangle of global statements (over 60, no joking, see later in this post about how I solved that) and spaghetti code of the likes never seen before, by even the lowest of BASIC noob programmers. I am thoroughly ashamed of the code, BUT, it bloody works!
Question: If the compiled program runs and plays smoothly, and works, does it matter how it was created?
Well, it’s something to think about. I don’t suppose a non-programmer end user would give two flying figurines about how badly it’s coded, as long as it worked.
Coder, there’s a bug in my spaghetti!
But it still riles me thinking about the days and nights wasted on trying to hunt down simple bugs in the spaghetti and trying to follow the control flow of the program. It’s so bad that to add even a simple feature or change could (and I’ve tried) make the whole kit and caboodle come crashing down.
For example. I spent the best part of a week on one routine, the game logic where it reports to the user if a peg is in the correct position and colour, correct colour and wrong position or neither, should be simple, it wasn’t, not for me anyway.
I wrote a routine to do those three tasks, but when there were duplicate colours either in the users selection or the secret code, or both, the game always reported these outcomes incorrectly. The game would be useless in this state.
I tried so hard for five days, but I could not get my head around how to solve the duplicate problem. In the end, I asked for help on Reddit learn python, and two very nice guys, props to both “MikeTheWatchGuy” and “efmccurdy” who tried to help me. Without people like these it would be even harder to learn Python, I’m grateful to everyone that even tries to help me. I do try to give back by helping noobs that are not even at my level yet, but they are few and far between, ha!
Even with help and advice I couldn’t work out what they were telling me, or how to integrate their code into the alphabetti-spaghetti monster that I had created.
Inciidently, Mikeas quite rightly appalled at the length of my code, at one stage it was nearly 3000 line long, LOL, to be fair a large chunk of that was Tkinter GUI stuff and a lot of comments, I have now trimmed it to 2,500, still far too long I know, especially as a non-gui, no frills Mastermind game can be written in 50 to 100 lines of code!
Finally, in desperation, I found the source code to someone else’s (non-GUI) Mastermind game to see how they did it. Thank you Mr F. Grant, his Mastermind code here, I didn’t really understand how it worked exactly, but I understood what inputs were needed (the players entered pegs and the secret code) and how the output of the function had to be used. I realised it was either make his bit of code work in Bletchley, or the game would have to be shelved.
Note: It looks like Mr F updated and improved the code since I used it. Also while I was searching for the the original source of his code I came across a couple of coders with the same problem as this on Reddit (I wish I had just searched “mastermind” on Reddit when I first had this problem), Still, it heartens me a little that I am not the only dumb ass around here LOL.
I had written my code to use integer for the colours, like 1=red, 2=blue etc, and then at the required time I would convert the integers to a string like “red” depending on its related integer. I thought this would be the smart way to do it, maybe it is, but just my luck, the code I was “borrowing” worked on strings only, goddammit ha-ha, it just made things that litter bit more confusing and frustrating.
I lack many qualities as a human, but I do have pretty good determination most of the time. I might say ‘I give up‘, a lot, but I still come back after a rest, (well usually).
It was complicated, but I managed it in about four hours, linking up\changing variables, moving\deleting\adding chunks of code, adding more globals to get at his variables, you get the picture, this did not feel like my happiest day of coding though I tell thee.
I set out to make a Mastermind clone with a GUI, and in the end I succeeded, well almost.
I know you are dying to hear what doesn’t work
I’m sorry to report dear reader, that because of my disgraceful planning and design (I warned myself about this a few posts ago), I am unable, at this stage, to get a proper game over\start new game routine to work.
The game is still totally playable, but after each go you have to reload the program, yep that sucks ass, I hate the idea as much as anyone. The program does not even exit gracefully, or at all, unless you use the “x” option in the window frame, or the quit option in the drop-down menu. Daft thing is they use the same code as the game over routine.
In the end I needed to stop work on this game today, and start writing this post because to be honest I couldn’t face another day of staring at my code, getting a migraine and then getting nowhere.
I tried to get the start new game routine to work, but it was like a leaking boat, as soon as I plugged one leak, two more leaks would appear, ad infinitum, most disheartening.
Not a happy ending, said the masseuse to the old bloke
What to do? I had a working game with a bad ending? The game is actually quite good, and a bit of a challenge to get it in six tries.
Look at the first screenshot on this page for a clue as to how I can crack the code about 7 out of 10 tries. I’ve played so many games on it I think I’ve sussed it out now.
There is also no scoring system, but I don’t think that’s too important, the original game did not have a scoring system anyway.
You can get the 10mb .exe of Bletchley from my Dropbox “Bletchley” folder. If you want the optional music, then get the “music” folder as well, and place that in the same directory as the .exe file. Because I am not posting the source code (probably never will) this will only be for Windows I’m afraid.
My forte, at least for the foreseeable future, appears to be making small apps where I can just about get away with bad structure and design. Because the code is short it doesn’t notice so much or really matter. That’s no solution of course, but I will get there sooner or later, as I learn more.
Talking of my other small projects. I tried updating TIM to use the Pygame internal mp3 player, rather than an external system player but it keep coming up with “file in use” crashes, so I abandoned that idea, like I said, not a great week or two in my little Python world.
Global to classes, it’s not that hard actually
At the beginning of this stupidly long and rambling post, I mentioned about the 60+ global statements I felt I had to use in Bletchley. I had mentioned in another post that I had to have a look at classes to help with the global problem, so I did.
I had previously tried to understand classes when I first started out learning Python but I couldn’t make head nor tail of them, so I avoided them completely. I took another look at classes for the sake of Bletchley.
I went through all the YouTube videos and articles I could find nothing much sunk in, until I found the perfect little article on classes that all beginners that are scared of classes should start with.
That article is concise, plainly laid out, and keeps it simple, which is just how I need it. Of course I know there must be a lot more to classes than this, and most likely, I am using them incorrectly, but they served their purpose.
I took all my global statements and put them into simple classes, the variables were now global without using the global statement, awesome.
Why aren’t beginners shown this I wonder? Probably because it’s not ‘Pythonic‘ is my guess. As this is so easy to do, I suspect the Python experts won’t like it. Just like global, it’s not complex and confusing enough.
Here is a simple class I used for a set of five globals:
class outcome:
”’build a string of the hint pegs”’
def __init__(self, out_come, out_come1, out_come2, out_come3, out_come4):
self.out_come = out_come
self.out_come1 = out_come1
self.out_come2 = out_come2
self.out_come3 = out_come3
self.out_come4 = out_come4
#initialise
r1 = outcome (0,0,0,0,0)
Now if I need to access any of those variables anywhere in my code, including inside functions, I can refer to them as “r1.out_come1“, “r1.out_come2” etc. Note that classes use a two space indent, unlike a function’s four space indent.
So if you are a beginner and don’t understand classes or are scared of their complexity, like I was, then start with the article I linked above.
My excuses for being a bad programmer
The following is a lot of self indulgent crap that you will probably want to skip, or get the violins out. Up to you.
Making Bletchley and HI-Lo has made me realise where I am at in Python programming, not nearly as good as I thought I was, and that’s saying something, because I am my own harshest critic.
However, I do have some caveats that I have to allow myself some give on.
I have been ADHD (called “Hyperkinetic Disorder” by my doctor for some reason), since I was a nipper. I was diagnosed in my 40s. It’s not the wibbly wobbly, running up walls, physical version [now], but an internal thing, affecting my mind in many ways that I won’t bore you with, apart from to tell you that my mind appears to work quite differently to most other people. Sometimes this is helpful, with creativity for example, but most of the time it is a pain in the smegging arse to be honest.
I’ve managed to overcome, dampen, or put up with, most of the effects of ADD (as they call the adult version now) without medication. In the UK it is very difficult to get the treatment, especially since I was on Dexamphetamine and then I stopped it after a year because of the side-effects. It can take years to get back on it again, or even a different drug, because it is a class A drug.
Because of my ADHD behaviour as a child (I had the wibbly wobbly running up walls thing from about 5 years old to roughly 15) and the condition not being understood in the UK in the 1960s\70s, and therefore no treatment available, I missed out on a proper education. Which meant mister thicko here didn’t get to Uni, or even college. Most of what I know is self-taught.
Add to that, I am nearly 60 years old (I can hear you gasping at the back there, (I’m really only 58 and a quarter, in fact), and maybe my mind isn’t as sharp as it once was.
Also, I may not have many years left. I had a heart attack and surgery a few years back. I have a dodgy ticker that stops at random in my sleep and I probably won’t make 65-70, so you can see why time is so important to me. I’m in a rush.
Sorry for this self-indulgence, but sometimes I feel like I am up against spectacularly bad odds of achieving anything worthwhile on planet Earth before I go. Boo hoo, blub.
Using Python V3.6.5(32 bit) on Windows 7, (64 bit).
Previous post: Python GUI Mastermind Game
from Python Coder
via read more
No comments:
Post a Comment