Jan 30th, 2024
Graduating as a mechanical engineer got me almost zero base knowledge needed from programming (except math and a bit of Matlab). During my years of professional coding, I picked up enormous amounts of knowledge related to computer science, but I occasionally still feel there is a lot of room for improvement.
That's why I decided that I will undergo Harvard's CS50 course. This is the first Harvard CS50 course I set myself to complete.
Each course has a final project, which my implementation of it I will present in these CS50-related blog posts.
The game follows standard hangman game rules:
The game has two modes:
Per standard hangman game rules, a part of the hangman is drawn for each unsuccessful try. If you reach ten failed attempts, a complete picture will be drawn.
Hangman - Start screen
After completing the game (winning or losing), you can start another game or quit the program.
Hangman - Lost game
Hangman - Mid game
Hangman - Won game
To play the game, clone the project, install Python, move into the repository's root folder, install dependencies with
pip install -r requirements.txt
and run
python project.py
inside the terminal.
project.py
- as per guidelines consists of a main function and three functions on the same level, needed for testing. However, for state-keeping reasons, I have initialized and kept all of my game inside a Game class, which is in the same file. I wanted to avoid using global variables.Como se indica en las pautas, el código consta de una función principal y tres funciones en el mismo nivel, necesarias para las pruebas. Sin embargo, por razones de mantenimiento de estado, he inicializado y mantenido todo mi juego dentro de una clase Game, que se encuentra en el mismo archivo. Quería evitar el uso de variables globales.
The three required functions on the same indentation level as the main function implement calls to Game class methods so that final project requirements are fulfilled. But I must point out that also all Class methods were thoroughly tested.
test_project.py
- contains all the tests of the project
requirements.txt
- contains all package dependencies and their versions used in the project
data/words.py
- contains two lists of five-letter and six-letter English words used when picking game modes
components/separators.py
- contains functions used to draw hangman game inside the CLI
components/stages.py
- contains stages of each of the wrong guess drawings for the hangman game. Stages are on purpose not following the DRY principle, so they are easier to maintain and review. If this was a bigger app, where performance would be critical, these stages + start screen drawings could be optimised.
As already mentioned beforehand, class
Game
encapsulates the whole game logic.
Here is the breakdown of key methods and their properties:
def __init__(self):
self.separator_length = 40
self.game_screen = 0
self.game_mode = "0"
self.guessed_letters = []
self.used_letters = []
self.word = ""
self.failures = 0
self.already_used_letter = ""
project.py - Game class
@property
def game_screen(self):
return self._game_screen
@game_screen.setter
def game_screen(self, n):
self._game_screen = n
@property
def game_mode(self):
return self._game_mode
@game_mode.setter
def game_mode(self, n):
self._game_mode = n
@property
def guessed_letters(self):
return self._guessed_letters
@guessed_letters.setter
def guessed_letters(self, new_values):
if len(new_values) == 2 and 0 <= new_values[1] < len(self.guessed_letters):
self._guessed_letters[new_values[1]] = new_values[0]
elif len(new_values) == 1:
self._guessed_letters = new_values[0]
else:
self._guessed_letters = []
@property
def used_letters(self):
return self._used_letters
@used_letters.setter
def used_letters(self, new_values):
if len(new_values) == 2 and new_values[1]:
self._used_letters.append(new_values[0])
elif len(new_values) == 1:
self._used_letters = new_values[0]
else:
self._used_letters = []
@property
def word(self):
return self._word
@word.setter
def word(self, n):
if n == "1":
self._word = random.choice(words_five_letters).upper()
elif n == "2":
self._word = random.choice(words_six_letters).upper()
else:
self._word = n # for testing purposes
@property
def failures(self):
return self._failures
@failures.setter
def failures(self, n):
self._failures = n
@property
def already_used_letter(self):
return self._already_used_letter
@already_used_letter.setter
def already_used_letter(self, n):
self._already_used_letter = n
project.py - Game class
def run_game(self):
self.clear_terminal()
while self.game_screen <= 1:
if self.game_screen == 0:
self.start_game()
while self.game_mode != "1" and self.game_mode != "2":
user_input = input("Mode: ")
self.game_mode = user_input
self.word = user_input
self.guessed_letters = [[" " for _ in range(len(self.word))]]
self.game_screen = 1
elif self.game_screen == 1:
self.clear_terminal()
self.main_game()
if self.is_game_finished():
user_input = input("Decision: ").upper()
else:
user_input = input("Guess a letter: ").upper()
self.already_used_letter = ""
if len(user_input) == 1 and user_input.isalpha():
###
## check if hit, miss, or repeat guess
# hit
if user_input in self.word and user_input not in self.used_letters:
indexes_of_hit = [index for index, char in enumerate(self.word) if char == user_input]
for index in indexes_of_hit:
self.guessed_letters = [user_input, index]
# repeat
elif user_input in self.used_letters:
self.already_used_letter = user_input
# miss
else:
self.failures = self.failures + 1
###
# add to used letters list
if user_input not in self.used_letters:
self.used_letters = [user_input, True]
elif user_input == "YES":
self.restart()
elif user_input == "NO":
self.quit()
break
project.py - Game class
def start_game(self):
draw_separator(self.separator_length)
draw_separator(self.separator_length)
self.empty_space()
self.empty_space()
self.empty_space()
center_text(text="HANGMAN", length=self.separator_length, draw_border=True, double_border=True)
self.empty_space()
self.empty_space()
self.empty_space()
draw_separator(self.separator_length)
draw_separator(self.separator_length)
self.empty_space()
center_text(text="Enter mode to start the game", length=self.separator_length, draw_border=True)
center_text(text="[1] - Normal mode", length=self.separator_length, draw_border=True)
center_text(text="[2] - Hard mode", length=self.separator_length, draw_border=True)
self.empty_space()
draw_separator(self.separator_length)
print("2023 - Alan Jereb".rjust(self.separator_length))
center_text(text="", length=self.separator_length)
project.py - Game class
def main_game(self):
draw_separator(self.separator_length)
self.empty_space()
stage_functions = [stage.zero, stage.one, stage.two, stage.three, stage.four, stage.five, stage.six,
stage.seven, stage.eight, stage.nine, stage.ten]
if 0 <= self.failures <= 10:
stage_functions[self.failures](self.separator_length)
self.empty_space()
self.empty_space()
if self.user_has_won():
center_text(text="You win!", length=self.separator_length, draw_border=True, double_border=True)
elif self.user_has_failed():
center_text(text="You lose!", length=self.separator_length, draw_border=True, double_border=True)
self.empty_space()
if not self.user_has_failed():
center_text(text=(" ".join(self.guessed_letters)), length=self.separator_length, draw_border=True, double_border=True)
center_text(text=("__ " * len(self.word)), length=self.separator_length, draw_border=True, double_border=True)
else:
center_text(text=self.word, length=self.separator_length, draw_border=True, double_border=True)
self.empty_space()
draw_separator(self.separator_length)
if self.is_game_finished():
center_text(text="To play another game type: yes", length=self.separator_length, draw_border=True)
center_text(text="To quit the game type: no", length=self.separator_length, draw_border=True)
else:
center_text(text="Used letters:", length=self.separator_length, draw_border=True)
center_text(text=", ".join(self.used_letters[0:9]), length=self.separator_length, draw_border=True)
center_text(text=", ".join(self.used_letters[9:]), length=self.separator_length, draw_border=True)
draw_separator(self.separator_length)
if len(self.already_used_letter):
print("You have already used letter", self.already_used_letter ,"!")
else:
center_text(text="", length=self.separator_length)
center_text(text="", length=self.separator_length)
project.py - Game class
def clear_terminal(self):
if platform == "Windows":
os.system("cls")
else:
os.system("clear")
project.py - Game class
def restart(self):
self.clear_terminal()
self.game_screen = 0
self.used_letters = []
self.failures = 0
self.game_mode = "0"
project.py - Game class
Completing the course was a fun little project. I got to know Python basics enough to build on top of it. I can see why Python is so popular nowadays, but at the same time, I have some reservations towards it. I find its syntax a bit hard to read (significantly longer functions) and would want to see Python using more curly braces and less indentation. Python lacking type-safety is also something I cannot get used to.