def rack_complete(): S.reset() print("Drawing all letters from the bag...") letters_drawn = [] bag = S.GAME_SETTINGS['bag'] while len(bag) > 0: print("number of letters still in the bag:", len(bag)) S.fill_rack() letters_drawn.extend(S.get_rack()) print("letters drawn:", S.get_rack()) S.set_rack("") print("total letters drawn:", len(letters_drawn)) print("in order:", letters_drawn) print("PASSED.")
def custom_turn(): # Goal: all plays in the Area of L1 to L15 need bonus plays to be valid. # BUG: doesn't consider single letters along the way - investigate! # see also: Line 3378 in logic_new # S.reset() S.set_rack("ERNSTLUA") word_1 = D.Play("NEUTRAL", "F8", "x") L.execute_play(word_1) S.increase_turn() S.set_rack("ERNSTLUA") word_2 = D.Play("ANLAUTES", "K5", "y") L.execute_play(word_2) S.increase_turn() # S.set_rack("ERNSTLUA") S.set_rack("FASS") # Fass should be valid on L2 searching_area = D.Area("L1", "L15") possible_plays = Scratch.find_plays_for_area(searching_area) # pprint.pprint(possible_plays) max_length = len(searching_area.non_empty_letters) + len(S.get_rack()) + 1 for current_length in range(max_length, 2, -1): highest_length = [ item for item in possible_plays if len(item) == current_length ] print("Plays with a length of", current_length) pprint.pprint(highest_length) Display.print_board()
def rack_simple(): S.reset() S.set_rack("ERNST") rack = S.get_rack() print("rack after reset:", rack) S.fill_rack() print("rack after fill_rack, without re-assignment:", rack) print("PASSED.") return
def entire_turn(number_of_turns: int = 5): # TODO: DEBUG A WHOLE LOT # BUG: # considers FARBEN a proper play on M2, y # only FARBE gets planted. # if there's no letter on the rack, it's still trying to find a valid play. # TODO: Add checks to ensure at least one Rack-letter must be used. # BUG: "FABEL" on L4-L8 is considered the best play on Turn 3 in this setup. # The available Area is F8 to F14... # Also doesn't check for bonus-plays # which would need to be: FA, AR, BB, and EE # tested in debugger: # expected # [STRUDELN F8-M8:x score:20(20+0) , # FARBEN M3-M8:y score:24(24+0) , # BEDARF K7-K12:y score:22(22+0) , # RAFFE H12-L12:x score:24(24+0) , # BEDARF I5-N5:x score:22(22+0) ] S.reset() # S.fill_rack() turn_number = S.GAME_SETTINGS['turn'] all_turns = [] while turn_number < number_of_turns: # TODO: finally make Game_settings a proper Object turn_number = S.GAME_SETTINGS['turn'] if C.is_first_turn() is True: S.set_rack("ERNSTLU?") else: S.set_rack("BEDARF") print(f" Turn No.: {turn_number} ".center(80, "-")) Display.print_board() turn = Game.Turn(None, S.get_rack()) all_turns.append(turn) # input("Press Enter to execute the highest scoring play from this turn...") L.execute_play(turn.highest_scoring_play) print(" End of turn. ".center(80, "-")) # S.fill_rack() S.increase_turn() Display.print_board() pprint.pprint(WL.get_active_plays()) print("PASSED.")
def find_position_ranges(): S.reset() S.set_rack("TEST") # length of 4 print("Rack:", S.get_rack()) # starting at the top-barrier of the board H1 # initial field is empty # expected: H4 print("H1, y") test = WS.find_position_range_for_position("H1", "y") print(test) assert (test == L.convert_positions_to_list("H1", "H4")) # starting at the bottom-barrier of the board H15, # initial field is empty print("H15, y") test = WS.find_position_range_for_position("H15", "y") print(test) assert (test == L.convert_positions_to_list("H12", "H15")) # expected: H12 # starting at the center of the board H8 # initial field is empty # expected: list from H5 to H11 (length of 7, center with 3 to either side) print("H8, y") test = WS.find_position_range_for_position("H8", "y") print(test) assert (test == L.convert_positions_to_list("H5", "H11")) # H8 stays free, H7 and H9 have a letter on them. # expected: list from H4 to H12 (+1 to either direction from before) print("letters on H7 and H9, starting on H8, y") L.set_letter_to_position("X", "H7") L.set_letter_to_position("X", "H9") test = WS.find_position_range_for_position("H8", "y") print(test) assert (test == L.convert_positions_to_list("H4", "H12")) # H8 stays free, 4 positions are empty: H1, H2, H8, H15 # expected: all positions on H, so H1 to H15 L.set_word_to_position("XXXX", "H3", axis="y") L.set_word_to_position("XXXXX", "H10", axis="y") test = WS.find_position_range_for_position("H8", "y") print(test) assert (test == L.convert_positions_to_list("H1", "H15"))
def area_finding(): S.set_rack("ERNSTL?") test_play_open = D.Play("LÜSTERN", "G8", "X") L.execute_play(test_play_open) S.increase_turn() Display.print_board() # Goal: make this consider FLÜSTERN by Building BEDARF on F3, along Y. S.set_rack("BEDARF") rack = S.get_rack() area_list = Scratch.find_all_areas_per_play(test_play_open, "y", rack) possible_plays = [] for current_area in area_list: neighbors = current_area.get_area_neighbors() sub_turn = Game.SubTurn(current_area.position_list) possible_plays.append(sub_turn.highest_scoring_play) print(possible_plays) print("PASSED.")
def entire_game(is_automatic: bool = False, always_ERNSTLUA: bool = False): # emulate the turns, from start to empty bag. # ask before executing a play whether it's correct, # write "incorrect" plays to a list for debugging. S.reset() remaining_letters_initial = len(S.INITIAL_SETTINGS['bag']) # remaining_letters_initial = len("AAAA") remaining_letters = deepcopy(remaining_letters_initial) turn_number = S.GAME_SETTINGS['turn'] all_turns = [] incorrect_plays = [] game_score = 0 previous_best_play = None current_rack = [] running = True print("Number of Letters:", remaining_letters) while running: if always_ERNSTLUA is True: # 4 letters still in the bag: only "ERNS" should be on the rack. num_letters_replaced = len("ERNSTLUA") - len(S.get_rack()) print("number of letters replaced:", num_letters_replaced) if remaining_letters == 0: pass elif remaining_letters < num_letters_replaced: offset = len("ERNSTLUA") - remaining_letters ernstlua_letters = "ERNSTLUA"[0:-offset] S.set_rack(ernstlua_letters) else: S.set_rack("ERNSTLUA") current_rack = S.get_rack() remaining_letters -= num_letters_replaced else: S.fill_rack() current_rack = S.get_rack() remaining_letters = len(S.GAME_SETTINGS['bag']) # highest_scoring_play = None print(f" Turn No.: {turn_number} ".center(80, "-")) Display.print_board() # BUG: sometimes yields the same turn? # see scatch.md turn = Game.Turn(None, current_rack) # highest_scoring_play = turn.highest_scoring_play # print("Total possible Plays for this turn:") # pprint.pprint(turn.possible_plays) Display.print_board() print("The Highest scoring play is:".center(80)) pprint.pprint(turn.highest_scoring_play) # TODO: this never fires. # if previous_best_play is not None: # if highest_scoring_play == previous_best_play: # raise NotImplementedError("Previous Best Play is identical to the current best Play.") if is_automatic is True: answer = "y" else: answer = "" while answer.casefold() not in ["y", "n"]: if answer == "": answer = input( "Check against the board - is this play correct? [y/n]: >") if len(answer) == 0: continue elif answer.casefold() == "n": incorrect_plays.append(turn.highest_scoring_play) break elif answer.casefold() == "y": break print(" End of turn. ".center(80, "-")) print("Remaining letters:", remaining_letters) L.execute_play(turn.highest_scoring_play) # previous_best_play = highest_scoring_play game_score += turn.highest_scoring_play.score_total if remaining_letters < 0 and len(S.get_rack()) == 0: running = False break S.increase_turn() print("Game has ended.") print("Total score:", game_score) print("Incorrect plays:") pprint.pprint(incorrect_plays) print("Play Log:") pprint.pprint(WL.get_active_plays())
def play_finding_by_position(): # This passes when the play BEDARF, F3, Y # gets points for the 2 plays it extends. # (DERBE, E5, X) and (FL?STERN, E8, X) # Start by setting LÜSTERN S.set_rack("ERNSTL?") test_play_open = D.Play("LÜSTERN", "G8", "X") L.execute_play(test_play_open) S.increase_turn() Display.print_board() # Set BORSTE S.set_rack("BORTE") test_play_borste = D.Play("BORSTE", "I5", "Y") L.execute_play(test_play_borste) S.increase_turn() Display.print_board() # Set ERBE S.set_rack("ERE") test_play_erbe = D.Play("ERBE", "G5", "X") L.execute_play(test_play_erbe) S.increase_turn() Display.print_board() # found_play = WL.find_active_play_by_position("G8") # print(found_play) # Find BEDARF, # Extends ERBE to DERBE # Extends LÜSTERN to FLÜSTERN # mark both extended-plays as "active" in the WordLog. # mark ERBE and LÜSTERN S.set_rack("BEDARF") test_area_bedarf = D.Area("F3", "F8") # empty_area_with_no_neighbors = D.Area("D3", "D8") # Works. bedarf_turn = Game.SubTurn(test_area_bedarf.position_list) L.execute_play(bedarf_turn.highest_scoring_play) S.increase_turn() S.set_rack("VERNDE") Display.print_board() # Works. area_verderbende = D.Area("A5", "O5") verderbende_turn = Game.SubTurn(area_verderbende.position_list) L.execute_play(verderbende_turn.highest_scoring_play) S.increase_turn() Display.print_board() S.set_rack("ZIERENDE") area_extends_right = D.Area("N1", "N15") extends_right_turn = Game.SubTurn(area_extends_right.position_list) print("highest scoring play:") pprint.pprint(extends_right_turn.highest_scoring_play) L.execute_play(extends_right_turn.highest_scoring_play) S.increase_turn() Display.print_board() # active_plays = WL.get_active_plays() # pprint.pprint(active_plays) # print("Length of active plays:", len(active_plays)) # # print("All plays of extends_right_turn") # pprint.pprint(extends_right_turn.possible_plays) # for position in test_area_bedarf.neighbors: # L.set_letter_to_position(".", position) # Display.print_board() # print("-"*30) # print("LÜSTERN can be expanded at:", test_play_open.extendable_at) # test_area_flsternd = D.Area("F8", "N8") # print("contested at:", test_area_flsternd.contested_at) # print("contested play(s):") # print(test_area_flsternd.contested_plays) # TODO: the exact same play can be contested twice. # -> identical play on 2 different positions S.set_rack("ERNSTZUNEHMEND") area_non_continuous = D.Area("L1", "L15") turn_non_continuous = Game.SubTurn(area_non_continuous.position_list) print("Current Rack:", S.get_rack()) print("Plays possible on L1 to L15:") pprint.pprint(turn_non_continuous.possible_plays) # TODO, testing: # select an area directly adjacent to an existing word, make sure all sub-plays are # counted as well # UR on E13-F13 should be possible # Bonus: DU, E12-E13 // ER, F12-F13 S.set_rack("ERDE") play_erde = D.Play("ERDE", "C12", "X") L.execute_play(play_erde) S.increase_turn() S.set_rack("URNE") Display.print_board() parallel_area = D.Area("C13", "H13") affected_parallel_plays = parallel_area.contested_plays print("affected_parallel plays:") pprint.pprint(affected_parallel_plays) parallel_subturn = Game.SubTurn(parallel_area.position_list) print("highest scoring play:") pprint.pprint(parallel_subturn.highest_scoring_play) print("Plays possible on C13 to F13:") pprint.pprint(parallel_subturn.possible_plays) # TODO, for Testing.: # create a situation on the board where the entire rack is played, # the word is vertical and extends all already existing words (7 extensions) # TODO: Idea - in an area with 2 or more possible extension_crossovers, # try to find the extensions first, then fill the area via regex-words. # needs: a function to reserve letters from the rack, # the word_search by regex, print("PASSED.")
def play_creating(): S.reset() S.set_rack("ERNSTL?") rack = S.get_rack() print("Rack:", rack) test_play_a = D.Play("LÜSTERN", "G8", "X") # print(WS.find_execution(test_play_a)) print(test_play_a.find_execution()) print(test_play_a) # passes with # [('L', 'G8'), # ('?', 'H8'), # ('S', 'I8'), # ('T', 'J8'), # ('E', 'K8'), # ('R', 'L8'), # ('N', 'M8')] L.execute_play(test_play_a) Display.print_board() S.increase_turn() S.set_rack("BEDARF") test_play_b = D.Play("BEDARF", "F3", "Y") print(test_play_b) # print(WS.find_execution(test_play_b)) print(test_play_b.find_execution()) L.execute_play(test_play_b) Display.print_board() print(L.get_word_from_position("F8", "N8", show_joker=True)) print(L.get_word_from_position("F8", "N8", show_joker=False)) # the complex play would be extending LÜSTERN to FLÜSTERN, # and create BEDARF in the process. # # Test-case with a letter already on the board # S.reset() # S.set_rack("T?") # rack = S.get_rack() # print("Rack:", rack) # L.set_letter_to_position("E", "H8") # test_play_a = D.Play("TEE", "G8", "x") # print(test_play_a) # # print(WS.find_execution(test_play_a)) # # test_play_b = D.Play("TEE", "F8", "x") # print(test_play_b) # placeable_suggestions = [] # possible_plays = [] # center = L.get_center_of_board() # # the board is symmetrical, might as well start on the x-axis # # find the usable area (x-axis along the center) # usable_positions = WS.find_usable_positions(center, "x") # usable_area = D.Area(position_list=usable_positions) # # convert to search parameters # search_parameters = WS.create_search_parameters(usable_area) # # find words according to those parameters # possible_words = WS.create_words(search_parameters) # # # TODO: extract function # for current_word in possible_words: # starting_positions = WS.find_starting_position(current_word, # search_parameters) # if starting_positions == []: # continue # else: # for s_pos in starting_positions: # suggestion = D.Suggestion(current_word, # s_pos, # search_parameters.axis) # if C.is_word_placeable(suggestion): # placeable_suggestions.append(suggestion) # # for current_suggestion in placeable_suggestions: # possible_plays.append(D.Play(d_word=current_suggestion)) # # sorted_plays = sorted(possible_plays, # key=operator.attrgetter('score'), # reverse=True) # highest_scoring_play = sorted_plays[0] # print("highest scoring play:") # print(highest_scoring_play) print("PASSED.")