Before I continue on, here is my what part of the code looks like (source can be found in GitHub):The juicy, important part of the projectI wrote the whole program in Python, because during the time I was brainstorming, I was learning said language.
Another reason why I chose Python was so that I could get more familiar with a language I have been putting off to learn about.
And, luckily this project came to mind, which undoubtedly uses a lot of Python (or even basic programming) concepts.
Starting with line 1, an import code.
While I was still making games in JS, I needn’t import any random jumbos.
In fact, all I had to do was write Math.
random() (or Math.
random())) to make sure that it is a whole number).
In Python, not everything is available first hand.
I used to be lazy to import libraries, even the Math library because I was used to immediate coding in JS.
But, with this project I was able to learn the discipline needed to import a library first, before using their helpful functions.
The next segment is a Class code.
Honestly, I never understood OOP that well.
I first knew about OOP in a Java book.
I knew the concept, and how it might be better than procedural programming.
And, another thing to note was that I didn’t really understand why I was supposed to use a Class.
While I was brainstorming, I had this sudden thought that the variables are relatively the same.
So, instead of making many different variables such as user_currentThumbsUp or computer_guess, why not make a class that could define all those variables, and then make an object based on that class!Next in line is the long function.
However, what makes it special isn’t the fact that it might be inefficient due to its long and somewhat repetitive nature, but how I applied the concept of recursion.
I remember knowing recursion from a meme that defines what recursion means (in that meme, the picture basically repeats itself inside the picture, pretty hilarious if you ask me).
My second exposure to recursion was during the Olympiads.
While doing practice sets, I stumbled upon a question about recursions.
In the recursion, there are two important parts: a limit checker, and the function itself.
The limit checker is to check whether a certain variable has reached a certain value, so that the function could stop recusing.
In my code, the function will keep on running until a winner has been found.
This recursion most obviously replaces the while loop I originally thought of.
Why did I change to use recursion?.Because it’s fun.
It’s fun to try new things, and see the output.
If it didn’t work, then I’m learning something.
If it did, then I learned something too.
I see that as a win-win solution, and I think that is what truly makes learning a beautiful thing.
Anyways, I did want to use a while loop to start the game.
But, I somehow thought that while loops are inefficient, and it makes the code somewhat messy and hard to build.
There’s a certain lack of logic between using a while loop that just turns my head around.
Besides, if I was trying to return a certain value by looping through the function, why not use a recursion?If I was to use a while loop, then it would look something like this (actually based on my brain storm):while(user.
currentThumbs != 0 or computer.
currentThumbs != 0): if randomInt >= 5: #code user turn else: #code computer turnThe problem with said code is that it isn’t flexible enough.
After choosing a randomInt, how would the second turn be?.It should’t be decided by another randomInt, but instead it should be given to the opponent.
Now, I could try and write a new variable that would have a string or integer that can show who’s turn it is, and then once the turn is done I can change that integer to another value (it actually makes sense, what).
But, something in it feels complex and unnecessary.
I mean, isn’t another cool thing about programming is that there are certainly more efficient ways to do things?.Even in CodeWars, people were able to solve problems by using only 1–3 lines of code.
Another problem with that code is what if I wanted to add another player?.Should I make another elif statement and change the randomInt, while also changing the role of the variable which dictates who will get the next turn?I see that it is much more simple and zen to use a recursive function, and allowing it to run as it will, as many times as it likes, until it reaches a certain limit, only by changing the parameter/domain inside that function.
Beautiful things sure can be found in simple things.
I really like this code, because before it comes a thinking process.
I am not foreign to the idea that the fun part of programming isn’t the coding part.
Instead, is the thinking part.
To solve a problem.
To brainstorm yourself out of an obstacle.
That’s pretty much everything about the code.
Though, there are some certain aspects that could be improved (and will be), such as how the computer would sometimes guess 3 without putting any thumb up (which is basically impossible, because the maximum guesses someone can make without putting their thumb up is equal to how many thumbs the opponent have left (and I just thought of it right now.
Thinking is fun)).
I can also add fun and trivial stuffs, such as a difficulty system, multiplayer, or even add a GUI so that people would be interested instead of reading it through a terminal.
So, all in all, this is what I learned from this project:Ask questions, answer them as you go on.
Brainstorming, and giving time to think, is the best way to thinkRecursions can be helpful and funUnderstanding Class can help save timeThe “thinking” part of programming is what makes people locked inStart projects.
You can learn a lot from it.
Thank you, for reading this article!.I hope you will like it.
Please, email me if you have any questions.
I am delighted to learn from my mistakes.