This is a very simple anagram game that plays only with names of colors.
I tried to focus on code design. I also wanted it to support further updates. But, my init
seems smelly and you will notice it, especially in the main loop.
I was wondering about my naming and commenting/docstrings. I need strong criticism here, without any mercy.
This is the module I've created with the only class:
class AnagramGame:
def __init__(self, words=None, rword=None, inp=None,
anagram=None, is_corr_word=False):
self._words = None
self._rword = rword # Stands for random word
self._anagram = anagram
self._inp = inp # User input
self._is_corr_word = is_corr_word # Stands for correct word bool
def get_words(self):
return self._words
def set_words(self, filename):
in_file = open(filename, 'r')
self._words = [in_line.rstrip('\n')
for in_line in in_file.readlines()]
in_file.close()
def del_words(self):
del self._words
words = property(get_words, set_words, del_words)
def get_rword(self):
return self._rword
def set_rword(self, sequence):
import random
self._rword = random.choice(sequence)
def del_rword(self):
del self._rword
rword = property(get_rword, set_rword, del_rword)
def get_anagram(self):
return self._anagram
def set_anagram(self, sequence):
import random
self._anagram = ''.join(random.sample(sequence, len(sequence)))
def del_anagram(self):
del self._anagram
anagram = property(get_anagram, set_anagram, del_anagram)
def get_is_corr_word(self):
return self._is_corr_word
def set_is_corr_word(self, boolean):
self._is_corr_word = boolean
def del_is_corr_word(self):
del self._is_corr_word
is_corr_word = property(get_is_corr_word, set_is_corr_word,
del_is_corr_word)
def ask_input(self, prompt=''):
self._inp = input(prompt)
def check_input(self, prompt_corr, prompt_incorr):
if self._inp == self.rword:
print(prompt_corr, end='')
self._is_corr_word = True
return self._is_corr_word
else:
print(prompt_incorr, end='')
self._is_corr_word = False
return self._is_corr_word
And this is the main program:
def main():
import games
print("Anagram Mini Game", "\n")
while True:
Angrm = games.AnagramGame() # Reset all attributes
Angrm.words = 'colors.txt' # Load words
Angrm.rword = Angrm.words # Extract a word
Angrm.anagram = Angrm.rword # Create anagram of the word
print("Try build an anagram of this shuffled color name: ", Angrm.anagram)
Angrm.ask_input("Give it a try - ")
Angrm.check_input("GG beast", "Wrong - ") # Not indent after impact prompts
while not Angrm.is_corr_word:
Angrm.ask_input()
Angrm.check_input("GG beast", "Wrong - ")
print("\n\n") # Indent for a new game
if __name__ == '__main__':
main()