def _is_valid_word(self, word): """ Uses twl to determine if word is a valid word. """ ret = twl.check(word) if self.debug: print(f"Word '{word}' is valid? {ret}") return twl.check(word)
def score_word(self, user_word): if len(user_word) == 0: return 0 if len(user_word) > 2: if user_word in self.used_words: self.feedback = "Word already used." return 0 if self.game.in_board(user_word): if twl.check(user_word.lower()): if len(user_word) < 5: word_score = 1 elif len(user_word) == 5: word_score = 2 elif len(user_word) == 6: word_score = 3 elif len(user_word) == 7: word_score = 4 else: word_score = 11 self.feedback = ("Your word " + '"' + user_word + '"' + " scored " + str(word_score) + " points!") return word_score else: self.feedback = 'That is not a valid word.' return -1 else: self.feedback = 'That word is not in the game board.' return -1 else: self.feedback = 'Word must be at least 3 letters long.' return -1
def valid(comb): print("All Valid Combinations are:") word_dict = {} for i in comb: if twl.check(i): word_dict[i] = 0 return word_dict
def makeWordsFromScrabbleDict(numElems): "numElems is how many elements to use at a time" elemList = tryPeriodic.getSymbolList() result = set() # empty set candidates = makeCandidateWords(elemList, numElems) for w in candidates: if twl.check(w.lower()): result.add(w) return result
def play_game_singleplayer(): print() print("WELCOME TO BOGGLE SOLITARE!") print() print("The rules are simple:") print("You have limited to type as many words as you can find.") print("you can go in any direction, but you can't use the same letter twice.") print("3 letter words and shorter are worth 1 point, ", "while longer words are worth 1 extra point per letter over 3") print("'Qu' can count as Q or QU in words.") print() length = input("How many seconds do you want for guessing? > " ) try: length = int(length) except ValueError: print("You failed to enter a valid number, default is 60 seconds") length = 60 else: if length < 0: print("You failed to enter a valid number, default is 60 seconds") length = 60 user_word_set = set() now = time.time() end = now + length score = 0 print(f"You have {length} seconds, starting...") time.sleep(2) print("NOW!") board = make_board(5) while time.time() < end: user_word_set.add(input("> ").upper()) print("STOP") time.sleep(1) for word in list(user_word_set): if twl.check(word.lower()) is False: print(f"{word} not in dictionary") user_word_set.remove(word) for word in user_word_set: if find(board, word): if len(word) <= 3: score += 1 else: score += len(word)-3 else: print(f"{word} is not on the board") print(f"your score is:{score}")
def privmsg(self, user, channel, message): if (channel == self.chatroom): if (message.startswith(self.nickname)): temp = message.split() if (len(temp) > 1): if (len(temp[1]) > 0): if (temp[1].lower() == 'help'): self.msg( self.chatroom, "Give me a word and I will return that word's Scrabble score. Example: \"" + self.nickname + ", [word]\" I also point out exceptional words!" ) elif (temp[1].lower() == 'points'): valstring = str(values) self.msg(self.chatroom, "Point values: \n" + self.printValues()) else: val = self.computeScore(temp[1]) if (not twl.check(temp[1])): self.msg( self.chatroom, temp[1] + " is not a valid word. Reason: was not found in dictionary." ) elif (len(temp[1]) > 15): self.msg( self.chatroom, temp[1] + " is not a valid word. Reason: too many letters (max 15)." ) else: self.msg(self.chatroom, temp[1] + ": " + str(val) + " points") else: temp = message.split() for word in temp: if (twl.check(word)): if (len(word) > 5 and len(word) < 16): val = self.computeScore(word) if (float(val) / float(len(word)) > 2.5): self.msg(self.chatroom, word + ": " + str(val) + " points")
def offlineSearch(word, dictionary): if dictionary == 0: dictionary = sowpodsDictionaryList.sowpodsDictionary if word.upper() in dictionary: return '1' else: return '0' elif dictionary == 1: if twl.check(word.lower()) == True: return '1' else: return '0'
def post(self, val): params = dict(cgi.parse_qsl(self.request.body)) addr = self.request.remote_ip global handle_count global global_scoreboard, global_board #TODO: Fix self.set_header("Content-Type", "text/html") if params["m"] == "check": self.write(str(handle_count)) elif params["m"] == "scores": #Generate scoreboard dynamically scores = global_scoreboard.get_scores() sb = "<table>" for (x, y) in scores: sb += "<tr>" sb += "<td align=right><b>%s:</b></td><td>%s</td>" % (str(x), str(y)) sb += "</tr>" sb += "</table>" self.write(sb) elif params["m"] == "board": sg = "<table width=\"418px\" height=\"451px\" style=\"background-image: url('scrabble.jpg')\" cellpadding=0 cellspacing=0>" for j in range(0, 15): sg += "<tr height=\"28\" valign=\"middle\">" for i in range(0, 15): p = global_board.get(i, j) at = " bgcolor=\"#FFCC66\"" if p is not None else "" ltr_txt = ( "<b><font color=%s>%s</font></b>" % ("black" if not isinstance(p, Blank) else "blue", str(p).upper()) if p is not None else "") pts = str(get_letter_points(p)) if p is not None else "" sg += "<td width=\"29\" valign=\"middle\" align=\"center\"><table cellpadding=0 cellspacing=0 height=23 width=23%s><tr valign=middle><td align=center valign=middle>%s</td><td valign=bottom><font size=1>%s</font></td></tr></tr></table></td>" % ( at, ltr_txt, pts) sg += "</tr>" sg += "</table>" self.write(sg) elif params["m"] == "dict": word = params["d"] if twl.check(word.strip().lower()): os.system( "beep -f 150 -l 10 &>/dev/null &") #dictionary lookup beep self.write("The word %s is in the dictionary." % word) else: os.system( "beep -f 100 -l 10 &>/dev/null &") #dictionary lookup beep self.write("The word %s IS NOT the dictionary." % word)
def check_and_score_row(self, grid, r, c, multiplier, xtra_score, valid, is_touching, mutex): # checking row so col is changing word = [grid[r][c]] temp_score = grid[r][c].score front_c = c - 1 # finds begining of word while self.inbounds(front_c): if grid[r][front_c].is_blank(): break else: word.append(grid[r][front_c]) temp_score += grid[r][front_c].score front_c -= 1 word.reverse() back_c = c + 1 # finds end of word while self.inbounds(back_c): if grid[r][back_c].is_blank(): break else: word.append(grid[r][back_c]) temp_score += grid[r][back_c].score back_c += 1 if len(word) == 1: # is valid but not a new word return if multiplier[1] == 'l': temp_score += grid[r][c] * (multiplier[0] - 1) else: temp_score *= multiplier[0] str_word = tiles_to_string(word) word_valid = twl.check(str_word) with mutex: valid[0] = valid[0] and word_valid xtra_score[0] += temp_score is_touching = True return
def post(self, val): params = dict(cgi.parse_qsl(self.request.body)) addr = self.request.remote_ip global handle_count global global_scoreboard, global_board #TODO: Fix self.set_header("Content-Type", "text/html") if params["m"] == "check": self.write(str(handle_count)) elif params["m"] == "scores": #Generate scoreboard dynamically scores = global_scoreboard.get_scores() sb = "<table>" for (x,y) in scores: sb += "<tr>" sb += "<td align=right><b>%s:</b></td><td>%s</td>" % (str(x), str(y)) sb += "</tr>" sb += "</table>" self.write(sb) elif params["m"] == "board": sg = "<table width=\"418px\" height=\"451px\" style=\"background-image: url('scrabble.jpg')\" cellpadding=0 cellspacing=0>" for j in range(0,15): sg += "<tr height=\"28\" valign=\"middle\">" for i in range(0,15): p = global_board.get(i,j) at = " bgcolor=\"#FFCC66\"" if p is not None else "" ltr_txt = ("<b><font color=%s>%s</font></b>" % ("black" if not isinstance(p, Blank) else "blue", str(p).upper()) if p is not None else "") pts = str(get_letter_points(p)) if p is not None else "" sg += "<td width=\"29\" valign=\"middle\" align=\"center\"><table cellpadding=0 cellspacing=0 height=23 width=23%s><tr valign=middle><td align=center valign=middle>%s</td><td valign=bottom><font size=1>%s</font></td></tr></tr></table></td>" % (at, ltr_txt, pts) sg += "</tr>" sg += "</table>" self.write(sg) elif params["m"] == "dict": word = params["d"] if twl.check(word.strip().lower()): os.system("beep -f 150 -l 10 &>/dev/null &") #dictionary lookup beep self.write("The word %s is in the dictionary." % word) else: os.system("beep -f 100 -l 10 &>/dev/null &") #dictionary lookup beep self.write("The word %s IS NOT the dictionary." % word)
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): 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
print "-- Begin %s's turn --" % cur_player voice.say("%s's turn!" % cur_player) sbox.highlight(cur_player) serve.refresh() clock.clock_start() while True: rsp = ask("Push enter to register move").lower().strip() splitted = map(lambda x: x.lower().strip(), rsp.split(' ')) if splitted[0] == "check" or splitted[0] == "lookup": wrd = splitted[1] in_dict = twl.check(wrd) print "The word %s %s in the dictionary." % (wrd, "is" if in_dict else "IS NOT") elif splitted[0] == "define": DictLookup(splitted[1].strip().lower()) elif splitted[0] == "pause": print "Turn clock paused" clock.clock_stop() elif splitted[0] == "resume": print "Resuming turn clock" clock.clock_start() elif splitted[0] == "accept": #Hack to accept a current game board state #used at least once to use this program for verification #purposes in an already-started scrabble game new_board = sv.get_current_board()
def update(self, starting_positon, word, direction, is_first): score = 0 word_multipler = 1 is_touching = False tile = Tile() # basic Tile used for comparison # used to see if user placed 7 tiles so 50 point bonus can be added new_tile_count = 0 with self.lock: # checks to see if its a valid english word if (not twl.check(tiles_to_string(word))): return (False, self.grid, 0) # check if starting position is valid row = starting_positon[0] col = starting_positon[1] if not self.inbounds(row, col): return (False, self.grid, 0) # create copy of grid so if the word turns out to not work we # the old list grid = [row_make_temp[:] for row_make_temp in self.grid] # used to lock cross_score and is_valid mutex = threading.Lock() threads = [] cross_score = [0] is_valid = [True] for letter in word: # check to see if a tile is already there if grid[row][col].is_blank(): # can be inserted new_tile_count += 1 # calculating multipler for word temp_multi = grid[row][col].multiplier if temp_multi[1] == 'l': score += letter.score * temp_multi[0] else: score += letter.score word_multipler *= temp_multi[0] grid[row][col] = letter # check to see if a tile is touching thats not in the # direction, and if there is make sure thats a word if direction == 'd': # to check row cur_thread = threading.Thread( target=self.check_and_score_row, args=(grid, row, col, temp_multi, cross_score, is_valid, is_touching, mutex)) else: # to check col cur_thread = threading.Thread( target=self.check_and_score_col, args=(grid, row, col, temp_multi, cross_score, is_valid, is_touching, mutex)) cur_thread.daemon = True cur_thread.start() threads.append(cur_thread) # check to see if tile trying to insert is already there elif grid[row][col] != letter: return (False, self.grid, 0) # this means we are inserting the same tile already there else: is_touching = True score += grid[row][col].score # update row and col if direction == 'd': row += 1 else: col += 1 # joining threads that might be checking cross_words for thread in threads: thread.join() # checking to see if it was a valid move if (not is_valid[0]) or ((not is_touching) and (not is_first)): return (False, self.grid, 0) self.grid = grid score = score * word_multipler + (cross_score[0]) return (True, self.grid, (score, score + 50)[new_tile_count == 7])
def checkVaild(word): x = scrabble.check(word) return x
print "-- Begin %s's turn --" % cur_player voice.say("%s's turn!" % cur_player) sbox.highlight(cur_player) serve.refresh() clock.clock_start() while True: rsp = ask("Push enter to register move").lower().strip() splitted = map(lambda x: x.lower().strip(), rsp.split(" ")) if splitted[0] == "check" or splitted[0] == "lookup": wrd = splitted[1] in_dict = twl.check(wrd) print "The word %s %s in the dictionary." % (wrd, "is" if in_dict else "IS NOT") elif splitted[0] == "define": DictLookup(splitted[1].strip().lower()) elif splitted[0] == "pause": print "Turn clock paused" clock.clock_stop() elif splitted[0] == "resume": print "Resuming turn clock" clock.clock_start() elif splitted[0] == "accept": # Hack to accept a current game board state # used at least once to use this program for verification # purposes in an already-started scrabble game new_board = sv.get_current_board() game_board = new_board
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})"
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: 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!")
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 finder(limit, row, col, oldString='', oldPos=''): global times_run global beginningLetters #quit if out of bounds if (row > 3 or row < 0): return if (col > 3 or col < 0): return #quit if length of string > set number if (len(oldString) >= limit): return #quit if you have already clicked on that tile if (gridPos[row][col] in oldPos): return #quit if u cant lead to a word if (len(oldString) < 4): if (oldString not in beginningLetters): return #make new string to check newString = oldString + grid[row][col] #add to oldPos newPos = oldPos + gridPos[row][col] #append word to list if it works if (twl.check(newString)): wordDict[newString] = newPos #check #print(newString) times_run = times_run + 1 #call 8 functions, start with right finder(limit, row, col + 1, newString, newPos) #reset oldString oldString = '' oldPos = '' #down right finder(limit, row + 1, col + 1, newString, newPos) oldString = '' oldPos = '' #down finder(limit, row + 1, col, newString, newPos) oldString = '' oldPos = '' #down left finder(limit, row + 1, col - 1, newString, newPos) oldString = '' oldPos = '' #left finder(limit, row, col - 1, newString, newPos) oldString = '' oldPos = '' #left up finder(limit, row - 1, col - 1, newString, newPos) oldString = '' oldPos = '' #up finder(limit, row - 1, col, newString, newPos) oldString = '' oldPos = '' #up right finder(limit, row - 1, col + 1, newString, newPos)
if len(word) > 1: return word else: del word[:] if len(word) > 1: return word else: print("Fant ingen nye ord! Ordet må bestå av flere enn 1 bokstav.") exit() def traverse(): pass length_of_word, start_x, start_y = findStart() word_excl = possCheck(length_of_word, start_x, start_y) print(word_excl) word = ''.join(word_excl) print(word) print(twl.check(word)) #print(length_of_word, start_x, start_y)