def take(self, candidate): self.used_tiles = [] if time_check: start_time = time.time() self.take_start_time = datetime.datetime.now() # 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 = '' self.graphics_to_update = self.graphics_to_update + [ 'status', 'guess' ] return None # Then check if a word if len(candidate) < 10: is_word = twl.check(candidate.lower()) else: is_word = api.get_word_data(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 = '' self.graphics_to_update = self.graphics_to_update + [ 'status', 'guess' ] return None error_trivial_extension = False error_tiles = False etyms_candidate = api.get_etym(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, etyms_candidate, True) if is_taken: # Get time of this steal self.last_update = self.take_start_time 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) self.player2words_list.remove(taken_word) if not taken_word in self.player2words_list: del self.player2words[taken_word] self.playerwords.update({candidate: etyms_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.used_tiles.append(letter) self.current.remove(letter) self.previous_guess = self.guess self.status = "Success! " + f"({taken_word} -> {self.previous_guess})" self.fresh_take = True self.taken_word = taken_word self.middle_used = candidate_list self.who_took = 'self' self.taken_i = taken_i self.my_word_taken = False self.new_word_i = len(self.playerwords_list) - 1 self.graphics_to_update = self.graphics_to_update + [ 'tiles', 'playerwords', 'player2words', 'status', 'guess' ] self.take_end_time = datetime.datetime.now() 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, etyms_candidate, False) if self_is_taken: self.last_update = self.take_start_time self.updated = True if event_type == 'steal': candidate_list = list(candidate) self.playerwords.update({candidate: etyms_candidate}) self.playerwords_list[taken_i] = candidate if not taken_word in self.playerwords_list: del self.playerwords[taken_word] for letter in taken_word: candidate_list.remove(letter) for letter in candidate_list: self.used_tiles.append(letter) self.current.remove(letter) self.previous_guess = self.guess self.status = "Success! " + f"({taken_word} -> {self.previous_guess})" self.fresh_take = True self.taken_word = taken_word self.middle_used = candidate_list self.who_took = 'self' self.taken_i = taken_i self.my_word_taken = True self.new_word_i = taken_i self.graphics_to_update = self.graphics_to_update + [ 'tiles', 'playerwords', 'player2words', 'status', 'guess' ] self.take_end_time = datetime.datetime.now() elif event_type == 'middle': candidate_list = list(candidate) for letter in candidate_list: self.used_tiles.append(letter) self.current.remove(letter) self.playerwords.update({candidate: etyms_candidate}) self.playerwords_list.append(candidate) self.previous_guess = self.guess self.status = "Success! " + f"({self.previous_guess} from the middle)" self.fresh_take = True self.taken_word = '0' self.middle_used = candidate_list self.who_took = 'self' self.taken_i = len(self.playerwords_list) - 1 self.my_word_taken = False self.new_word_i = len(self.playerwords_list) - 1 self.graphics_to_update = self.graphics_to_update + [ 'tiles', 'playerwords', 'player2words', 'status', 'guess' ] self.take_end_time = datetime.datetime.now() elif error_trivial_extension or event_type == 'trivial': self.previous_guess = self.guess self.status = "Same root! " + f"({self.same_root_word} and {self.previous_guess} share root {self.root})" self.graphics_to_update = self.graphics_to_update + [ 'status', 'guess' ] elif error_tiles: self.previous_guess = self.guess self.status = "Tiles aren't there! " + f"({self.previous_guess})" self.graphics_to_update = self.graphics_to_update + [ 'status', 'guess' ] else: self.previous_guess = self.guess self.status = "Tiles aren't there! " + f"({self.previous_guess})" self.graphics_to_update = self.graphics_to_update + [ 'status', 'guess' ] self.guess = '' if time_check: end_time = time.time() self.time_dict['take'] = end_time - start_time
def take(self, candidate): self.take_dict['used_tiles'] = [] if time_check: start_time = time.time() self.take_dict['take_time'] = time.time() # First check if has 3 letters if len(candidate) < 3: self.status = "Word is too short! " + f"({candidate})" self.guess = '' self.graphics_to_update = self.graphics_to_update + ['status', 'guess'] return None # Then check if a word if len(candidate) < 10: candidate_lower = candidate.lower() is_word = twl.check(candidate_lower) or candidate_lower in word_add_twl else: is_word = api.get_word_data(candidate) if not is_word: # self.__display_text("Not a word!", 200, 400) self.status = "Not a word! " + f"({candidate})" self.guess = '' self.graphics_to_update = self.graphics_to_update + ['status', 'guess'] return None # If no prefixes and suffixes rule, check that if no_prefix_suffix: has_prefix_suffix, prefix, suffix = api.get_prefix_suffix(candidate) # print(f"Stuff: {has_prefix_suffix}, {prefix}, {suffix}") # print(f"{prefix in not_allowed_prefixes}, {suffix in not_allowed_suffixes}") if has_prefix_suffix and (prefix in not_allowed_prefixes or suffix in not_allowed_suffixes): self.status = "Prefix / suffix not allowed!" self.guess = '' self.graphics_to_update = self.graphics_to_update + ['status', 'guess'] return None etyms_candidate = api.get_etym(candidate) is_taken, used_tiles, self_taken_words, opp_taken_words, self_taken_is, opp_taken_is = self.__check_steal(candidate, etyms_candidate) if is_taken: self.take_dict['new_word'] = candidate self.take_dict['etyms_new_word'] = etyms_candidate self.take_dict['take_time'] = self.take_dict['take_time'] self.take_dict['used_tiles'] = used_tiles self.take_dict['self_taken_words'] = self_taken_words self.take_dict['opp_taken_words'] = opp_taken_words self.take_dict['self_taken_is'] = self_taken_is self.take_dict['opp_taken_is'] = opp_taken_is self.take_waiting = True self.take_waiting_time = time.time() # Make a copy of current game state in case we need to backtrack self.tiles_past = self.tiles.copy() self.current_past = self.current.copy() self.playerwords_past = self.playerwords.copy() self.playerwords_list_past = self.playerwords_list.copy() self.player2words_past = self.player2words.copy() self.player2words_list_past = self.player2words_list.copy() if self.mode == 'solo': self.update_take('self', self.take_dict) self.guess = '' if time_check: end_time = time.time() self.time_dict['take'] = end_time - start_time
def take(self, candidate, player, last_update): self.used_tiles = [] if time_check: start_time = time.time() self.take_start_time = time.time() # First check if has 3 letters if len(candidate) < 3: self.status = "Word is too short! " + f"({candidate})" return 'short', None # Then check if a word if len(candidate) < 10: candidate_lower = candidate.lower() is_word = twl.check( candidate_lower) or candidate_lower in word_add_twl else: is_word = api.get_word_data(candidate) if not is_word: # self.__display_text("Not a word!", 200, 400) self.status = "Not a word! " + f"({candidate})" return 'not_word', None # If no prefixes and suffixes, check that if no_prefix_suffix: has_prefix_suffix, prefix, suffix = api.get_prefix_suffix( candidate) # print(f"Stuff: {has_prefix_suffix}, {prefix}, {suffix}") # print(f"{prefix in not_allowed_prefixes}, {suffix in not_allowed_suffixes}") if has_prefix_suffix and (prefix in not_allowed_prefixes or suffix in not_allowed_suffixes): self.status = "Prefix / suffix not allowed!" return 'prefix_suffix', None error_trivial_extension = False error_tiles = False etyms_candidate = api.get_etym(candidate) # Check if can take the player 2's words (not checking middle steal) print(f"{candidate}, {etyms_candidate}, {True}") is_taken, event_type, taken_word, taken_i = self.__check_steal( candidate, etyms_candidate, True) if is_taken: # Get time of this steal self.last_update = self.take_start_time 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) # Figure out what tiles are used from the middle and remove those from self.current for letter in taken_word: candidate_list.remove(letter) return 'steal', Take(player, 1, candidate, etyms_candidate, taken_word, taken_i, candidate_list, self.take_start_time - last_update) self.take_end_time = datetime.datetime.now() 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, etyms_candidate, False) if self_is_taken: if event_type == 'steal': candidate_list = list(candidate) for letter in taken_word: candidate_list.remove(letter) return 'steal', Take(player, 0, candidate, etyms_candidate, taken_word, taken_i, candidate_list, self.take_start_time - last_update) elif event_type == 'middle': candidate_list = list(candidate) return 'steal', Take(player, -1, candidate, etyms_candidate, '', taken_i, candidate_list, self.take_start_time - last_update) elif error_trivial_extension or event_type == 'trivial': self.status = "Same root! " + f"({self.same_root_word} and {candidate} share root {self.root})" return 'trivial', None elif error_tiles: self.status = "Tiles aren't there! " + f"({candidate})" return 'tiles', None else: self.status = "Tiles aren't there! " + f"({candidate})"