""" Interacting with the Wordle web service on jeff.cis.cabrillo.edu. """ import requests from colorama import Fore, Back, Style URL = 'https://jeff.cis.cabrillo.edu/util/wordle' # Might as well keep this around USER_ID = '0123456789abcdef0123456789abcdef01234567' # Change this def _load_words(): """Returns the Wordle dictionary file as a set of strings.""" with open('/srv/datasets/wordle_words.txt') as word_file: all_words = set(map(str.rstrip, word_file)) return all_words DICTIONARY = _load_words() class WordlePlayer: """I try to win Wordle games!""" def __init__(self): """Logs into the Wordle service on jeff.cis.cabrillo.edu.""" self._session = requests.Session() self._session.post(URL, {'user_id': USER_ID}) # TODO (as desired) def play(self): """Plays a game from start to finish, attempting to guess the word.""" # TODO (for now plays interactive with user input; change to have it play automatically!) guess = input() response = self._session.post(URL, {'guess': guess}).json() print(''.join(Fore.WHITE + (Back.BLACK if r is None else Back.GREEN if r else Back.YELLOW) + c for (c, r) in zip(guess, response['progress'])) + Style.RESET_ALL) while 'word' not in response: guess = input() response = self._session.post(URL, {'guess': guess}).json() print(''.join(Fore.WHITE + (Back.BLACK if r is None else Back.GREEN if r else Back.YELLOW) + c for (c, r) in zip(guess, response['progress'])) + Style.RESET_ALL) if all(response['progress']): print('✅') else: print(f'🔴 ({response["word"]})') if __name__ == '__main__': player = WordlePlayer() player.play()