def take(self, candidate): # First check if has 3 letters if len(candidate) < 3: self.previous_guess = self.guess self.status = "Word is too short! " + f"({self.previous_guess})" self.guess = '' return None # Then check if a word if len(candidate) < 10: is_word = twl.check(candidate.lower()) else: is_word = root.merriam_word_check(candidate) if not is_word: # self.__display_text("Not a word!", 200, 400) self.previous_guess = self.guess self.status = "Not a word! " + f"({self.previous_guess})" self.guess = '' return None error_trivial_extension = False error_tiles = False # Get the merriam stripped version of the guess (e.g. TRAINED --> TRAIN) merriam_candidate = root.merriam_strip(candidate) # Check if can take the other player's words (not checking middle steal) is_taken, event_type, taken_word, taken_i = self.__check_steal( candidate, merriam_candidate, self.player2words, True) if is_taken: # Get time of this steal self.last_update = datetime.datetime.now() if event_type == 'steal': # in theory, this if statement is unnecessary since there are no middle steals # Make the candidate word into a list to remove individual letters. candidate_list = list(candidate) # Delete the taken word from opponent's list, add it to your own dictionary and corresponding list (list for ordering purposes) del self.player2words[taken_word] self.player2words_list.remove(taken_word) self.playerwords.update({candidate: merriam_candidate}) self.playerwords_list.append(candidate) # Figure out what tiles are used from the middle and remove those from self.current for letter in taken_word: candidate_list.remove(letter) for letter in candidate_list: self.current.remove(letter) self.previous_guess = self.guess self.status = "Success! " + f"({self.previous_guess})" self.fresh_take = True self.pre_take_word = taken_word self.middle_used = candidate_list else: # If no steal was triggered above, then couldn't steal opponent's words for one of the reasons below # (wait until you check whether you can take one of your own words to see if it was a failure overall) if event_type == 'trivial': error_trivial_extension = True elif event_type == 'tiles': error_tiles = True # if could not take the other player's words, check if can take one's own if not is_taken: self_is_taken, event_type, taken_word, taken_i = self.__check_steal( candidate, merriam_candidate, self.playerwords, False) if self_is_taken: self.last_update = datetime.datetime.now() self.updated = True if event_type == 'steal': candidate_list = list(candidate) del self.playerwords[taken_word] self.playerwords.update({candidate: merriam_candidate}) self.playerwords_list[taken_i] = candidate for letter in taken_word: candidate_list.remove(letter) for letter in candidate_list: self.current.remove(letter) self.previous_guess = self.guess self.status = "Success! " + f"({self.previous_guess})" self.fresh_take = True self.pre_take_word = taken_word self.middle_used = candidate_list elif event_type == 'middle': candidate_list = list(candidate) for letter in candidate_list: self.current.remove(letter) self.playerwords.update({candidate: merriam_candidate}) self.playerwords_list.append(candidate) self.previous_guess = self.guess self.status = "Success! " + f"({self.previous_guess})" self.fresh_take = True self.pre_take_word = taken_word self.middle_used = candidate_list elif error_trivial_extension: self.previous_guess = self.guess self.status = "Same root! " + f"({self.previous_guess})" elif error_tiles: self.previous_guess = self.guess self.status = "Tiles aren't there! " + f"({self.previous_guess})" else: self.previous_guess = self.guess self.status = "Tiles aren't there! " + f"({self.previous_guess})" self.guess = ''
def take(self, candidate): # First check if has 3 letters if len(candidate) < 3: self.previous_guess = self.guess self.status = "Word is too short! " + f"({self.previous_guess})" self.guess = '' return None # Then check if a word if len(candidate) < 10: is_word = twl.check(candidate.lower()) else: is_word = root.merriam_word_check(candidate) if not is_word: # self.__display_text("Not a word!", 200, 400) self.previous_guess = self.guess self.status = "Not a word! " + f"({self.previous_guess})" self.guess = '' return None error_trivial_extension = False error_tiles = False # Get the merriam stripped version of the guess (e.g. TRAINED --> TRAIN) self.merriam_candidate = root.merriam_strip(candidate) # Check if can take the other player's words (not checking middle steal) is_taken, event_type, taken_word, taken_i = self.__check_steal( candidate, self.merriam_candidate, self.player2words, True) self.event_type = event_type if is_taken: # Get time of this steal self.last_update = datetime.datetime.now() if self.event_type == 'opponent steal': # in theory, this if statement is unnecessary since there are no middle steals # Get what letters were used in the middle candidate_list = list(candidate) for letter in taken_word: candidate_list.remove(letter) self.middle_used = candidate_list self.fresh_take = True self.pre_take_word = taken_word self.taken_i = taken_i else: # If no steal was triggered above, then couldn't steal opponent's words for one of the reasons below # (wait until you check whether you can take one of your own words to see if it was a failure overall) if self.event_type == 'trivial': error_trivial_extension = True elif self.event_type == 'tiles': error_tiles = True # if could not take the other player's words, check if can take one's own if not is_taken: self_is_taken, event_type, taken_word, taken_i = self.__check_steal( candidate, self.merriam_candidate, self.playerwords, False) self.event_type = event_type if self_is_taken: self.last_update = datetime.datetime.now() self.updated = True if self.event_type == 'self steal': candidate_list = list(candidate) for letter in taken_word: candidate_list.remove(letter) self.middle_used = candidate_list self.fresh_take = True self.pre_take_word = taken_word self.taken_i = taken_i elif self.event_type == 'middle': candidate_list = list(candidate) self.middle_used = candidate_list self.fresh_take = True self.pre_take_word = taken_word self.taken_i = taken_i elif error_trivial_extension: self.previous_guess = self.guess self.status = "Same root! " + f"({self.previous_guess})" elif error_tiles: self.previous_guess = self.guess self.status = "Tiles aren't there! " + f"({self.previous_guess})" else: self.previous_guess = self.guess self.status = "Tiles aren't there! " + f"({self.previous_guess})" self.previous_guess = self.guess self.guess = ''
def take(self, candidate): # First check if has 3 letters if len(candidate) < 3: print("Word is too short!") return None # First check if a word if len(candidate) < 10: is_word = twl.check(candidate.lower()) else: is_word = merriam_word_check(candidate) if not is_word: print("Not a word!") return None error_trivial_extension = False error_tiles = False steal_type = None merriam_candidate = root.merriam_strip(candidate) for i, word in enumerate(self.playerwords): # First, check if candidate is a superset of the current word if self.__superset(candidate, word, strict=True): # root_candidate = self.__root(candidate) # root_word = self.__root(word) # Then, check if the tiles needed to make candidate are in the middle if not self.__superset(self.current, self.__subtract(candidate, word)): error_tiles = True else: merriam_word = self.playerwords[word] root_candidate = root.lookup(merriam_candidate) root_word = root.lookup(merriam_word) # merriam_root_check = root.merriam_root_check(candidate, word) try: root_overlap = any(x in root_candidate for x in root_word) except TypeError: root_overlap = False if (root_candidate is None or root_word is None) and (merriam_candidate == merriam_word): error_trivial_extension = True elif root_overlap or (merriam_candidate == merriam_word): error_trivial_extension = True else: steal_type = 'steal' taken_word = word # elif word in candidate and any(x in root.lookup(candidate) for x in root.lookup(word)): # error_trivial_extension = True # If all of those check out, then it's a steal and record which word is being stolen if steal_type is None: if self.__superset(self.current, candidate, strict=False): steal_type = 'middle' if steal_type == 'steal': candidate_list = list(candidate) del self.playerwords[taken_word] self.playerwords.update({candidate: merriam_candidate}) for letter in taken_word: candidate_list.remove(letter) for letter in candidate_list: self.current.remove(letter) self.printstatus() elif steal_type == 'middle': candidate_list = list(candidate) for letter in candidate_list: self.current.remove(letter) self.playerwords.update({candidate: merriam_candidate}) self.printstatus() elif error_trivial_extension: print("Same root!") elif error_tiles: print("Tiles aren't there!") else: print("Tiles aren't there!")