I'm looking to improve my code writing in general by peer review. I've followed the following exercise: https://edabit.com/challenge/dLnZLi8FjaK6qKcvv.
The exercise is to create an English version Shiritori Game. You can find an explanation by following the link but I'm going to explain below anyway.
The game is a word matching games where the last letter of the last word said must match the first letter of the next word. A word cannot be used again once it has been played in that game. If a word doesn't match these rules then it is game over and the game is restarted.
Please find the code below:
#Exercise source = https://edabit.com/challenge/dLnZLi8FjaK6qKcvv ''' Class Shiritori Controls the action and state of the game ''' class Shiritori: ''' Set up an empty array to store the last words used. Stores the currently used word for judging Initiates the main game loop main() ''' def __init__(self): self.playedWords =  self.currentWord = None self.main() ''' Determines if the current word's first letter matches the last word's letter ''' def ruleOne(self): lastWord = self.playedWords[-1] if lastWord[-1] == self.currentWord: return True return False ''' Determines if the current word has already been played ''' def ruleTwo(self): if self.currentWord in self.playedWords: return False return True ''' Adds the current word to the list of played words ''' def addWord(self): self.playedWords.append(self.currentWord) ''' Play a turn 1. enters a word which is turned into lowercase and removes any additional spaces 2. checks if it is the first word to be played, add the word if no words have been played 3. checks the word against rule 1 and rule 2, adds the word if both rules are true 4. if 2 or 3 have not been done then activate the game over message and restart the game ''' def play(self, word): word = word.lower() word = word.strip() self.currentWord = word if not self.playedWords: self.addWord() elif(self.ruleOne() and self.ruleTwo()): self.addWord() else: self.gameOver() ''' prints the game over message and restarts the game ''' def gameOver(self): print("You have lost. Game is being restarted.") self.playedWords =  self.currentWord = None ''' The main loop of the game. Keeps accespting words from users and playing them in the games. Exit the loop by entering in an empty string or just pressing enter. ''' def main(self): print("Enter a word or enter '' to exit the game") while True: word = input("Please enter your word>") self.play(word) if word == '': break print("Thank you for playing.") game = Shiritori()
Once run the code should run the main function of the class which will loop over until the user decided to quit by pressing enter (entering an empty string). Otherwise the program should accept a string of characters as words. Please note there is no parsing of the data except turning the word into lowercase and removing any additional spaces. The entered word will be checked against an array to see if the two rules match up, if the rules don't match then the game goes into the 'game over' function which resets the game automatically.
The game runs within the console.
string[-1]last letter and last string retrieval
I originally used
len(string/array)-1to retrieve the last letter/string. However this seems more visually appealing but it is questionable whether it is readable
Main inside or outside of class
I chose inside however maybe it should be outside of the class as it makes sense to have the main loop there and keep it separate from the object however it is the main controls so I am unsure.
elifstatement do the same thing, there should be a more efficient way of doing it without separating the action into the function and having both statements
if word == ''
Not sure if unorthodox but there should be a better comparison
self.currentWord = word
There might be a better way of combining this?