How To Create a Code Cracker Game in Python
Code Cracker Tutorial
This tutorial is meant for beginners to intermediate. This tutorial is fairly simple, with some complex concepts to it. In all, this tutorial will get you on your way to learning Python. Without further ado, let's get on with it!
Go here for the output:
If you prefer a video version of this tutorial here it is 😀! Please like and subscribe!
The code on GitHub:
The code on Repl.it:
Objective: Generate a random password which is 3 to 5 integers long. Based on the length of the password, we generate the number of lives considering the fact that 'n' integer long password can have n! possible ways of arranging those 'n' numbers. The user has to guess the password based on the computer generated hints provided.
First, click on the plus sign in the bottom-right corner of your screen.
This file cannot be displayed: https://storage.googleapis.com/replit/images/1539540877304_6dcae5439ca3431190c660e07a31fc6b.pn
Then, click "Python3" in "Popular", or you can search.
Now let's get coding!
First, we need to import the modules needed for our code. So type in:
Now we can use the files that the creators made in our code! So we do not have to reinvent the wheel.
Let's make our functions to make our program more reusable and easy to understand. We will call these functions later when displaying our hints.
Now, we need to let the player have a certain number of tries before the game ends. This is why we need the factorial function. To make it, type in this code:
def factorial(n): if n == 0: return True else: return n * factorial(n-1)
This code will basically puts this equation into code:
or in visuals:
This file cannot be displayed: https://storage.googleapis.com/replit/images/1539573913398_36a8825d37c77aff0926f4b44d078332.pn
Thanks to Wikipedia for the image.
What this code does is that when n, the parameter that the player will input, is 0, the program will stop. If that fails to be true, it subtracts itself until it becomes 0. This is what's called a recursive function (a function which either calls itself or is in a potential cycle of function calls).
Now we have to check if the number is even, so we create a function called even. To do so, we type in the following code:
def even(num): if num % 2 == 0: return True else: return False
Quite straightforward, when the value inputted divided by two leaves a remainder of 0, it returns True, telling the program that the number inputted is even.
Now, we need to make a function that counts the number of even numbers. To do so, we type in the following code:
def countEven(password): isEven = 0 for x in password: if even(int(x)): isEven += 1 return isEven
What this code does is that we use the previous function for them to see if it is even, and it moves the variable isEven up by one. Finally it returns isEven.
We also need to make the function that finds the sum. To do so, we type the following code:
def Sum(password): _sum_ = 0 for x in password: _sum_ += int(x) return _sum_
In this code, we make the value of the sum in a variable called sum because "sum" is a function in python. And we can't assign a variable the name of a function. This code returns the sum.
The final function we are going to make is the product of all the numbers. This works like Sum(), except with multiplication. This is the code:
def product(password): producty = 1 for x in password: producty *= int(x) return producty
Yay!!! 😀 You made all the functions! Now that we've made them, it's time to call them and display them on the screen. We will use concatenation to join the words together, or in other words, concatenate.
On this line, we choose the length of the code.
integer = random.randint(3, 5)
This creates a list of numbers to choose from.
This line creates the password.
password = random.sample(chars, integer)
This determines the lives the player will have.
lives = factorial(integer)
This defines the sum.
_sum_ = Sum(password)
Taking out all the weird characters (Delete the next line and put print(password) instead and you'll see what I mean.)
password = ''.join(password)
Notifying player the code is generated.
Print the hints.
print('it is', integer ,'numbers long') print('the sum is', _sum_) producty = product(password) print('the product is', producty) isEven = countEven(password) print('The number of even numbers is', isEven) print('The first number is', int(password)) print('The last number is', int(password[-1])) isOdd = integer - isEven print('The number of odd numbers is', isOdd) print('The highest number is', str(max(password))) print('The lowest number is', str(min(password))) print('you have', lives, 'lives')
Checking if the player has any lives left. If not, it breaks the program, stopping the game.
while lives > 0: answer = input('what is your password') if answer == password: print('OMG, you got it ') print('The password was', password) break if answer != password: lives -= 1 print('you have', lives, 'lives') print('BOO') if lives <= 1: print ('GAME OVER!!!!!') break
AND THAT'S IT! You made it! 👍
Also, I put a video tutorial on youtube about this, on a channel called funcoding14. The link to it is above. However, I am moving it to a channel called Tech Tips, in where I will also make 3d modeling tutorials. I hope you enjoyed this tutorial. If you viewed this tutorial, please upvote it. It means a lot.
This is nice.
Also, you can format code inside your text so the identation doesn't get ruined. Here's an example: https://repl.it/talk/challenge/How-to-draw-Pixel-Art-on-Python-with-Turtle/7556.