From b1172e5850151c1a1d95002c152bdf17f4b43996 Mon Sep 17 00:00:00 2001 From: Conor Walker <2089327w@student.gla.ac.uk> Date: Tue, 1 Feb 2022 22:37:41 +0000 Subject: [PATCH] Chooses word based on seed for consistency in a day --- src/pydle.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/pydle.py b/src/pydle.py index ba8eb26..3f62320 100644 --- a/src/pydle.py +++ b/src/pydle.py @@ -1,6 +1,7 @@ import os import random import sys +from datetime import date # Gets the input from a user - number param is used to display the guess number the user is on @@ -22,10 +23,19 @@ def resource_path(relative_path): return os.path.join(base_path, relative_path) -# Gets a random word from a words.txt file present in the same directory as this script +# Gets a random word from a words.txt file present in the same directory as this script. +# If file is not present, uses a small fallback list of words. +# Shuffles based on a seed and selects a consistent member of the list to allow consistency between runs on same day. def get_random_word(): - lines = open(resource_path('words.txt')).read().splitlines() - return random.choice(lines) + lines = [] + try: + lines = open(resource_path('words.txt')).read().splitlines() + except IOError: + lines = ["Arrow", "Pride", "Whole", "React", "Chose", "Quote", "Adieu", "Story", "Whale", "Musty", "Korea", + "Snide", "Zebra", "Shite", "Plans", "Bride", "Blare", "Child", "Towns", "Treat", "Lusty", "Ocean"] + random.seed(int(date.today().day)) + random.shuffle(lines) + return lines[1] # Compares the users input with the word to be guessed. Returns true if the guess is correct, false otherwise