コード例 #1
0
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.")
コード例 #2
0
def basics():
    # checks.py
    assert C.is_position_valid("A1") is True
    assert C.is_position_valid("O15") is True
    assert C.is_position_valid("B52") is False
    assert C.is_position_valid("Z6") is False

    assert C.is_coordinate_valid(0, 0) is True
    assert C.is_coordinate_valid(99, 99) is False
    assert C.is_coordinate_valid(75, 0) is False
    assert C.is_coordinate_valid(-1, -1) is False

    assert C.is_position_empty("A1") is True
    assert C.is_position_empty("A2") is True
    assert C.is_position_empty("A3") is True
    assert C.is_position_empty("A4") is True
    assert C.is_position_empty("O15") is True

    whole_range = L.convert_positions_to_list("A1", "O1")
    broken_range = ["A1", "A3"]
    assert C.is_position_list_continuous(whole_range) is True
    assert C.is_position_list_continuous(broken_range) is False

    # logic_old.py
    assert list(L.find_substring("A", "ABRACADABRA")) == [0, 3, 5, 7, 10]
    assert list(L.find_substring("Z", "BANANA")) == []

    assert L.convert_coordinate_to_position(0, 0) == "A1"
    assert L.convert_coordinate_to_position(14, 14) == "O15"
    assert L.convert_coordinate_to_position(62, 52) is None
    assert L.convert_coordinate_to_position(2, 52) is None
    assert L.convert_coordinate_to_position(52, 2) is None

    assert L.convert_position_to_coordinate("A1") == (0, 0)
    assert L.convert_position_to_coordinate("O15") == (14, 14)
    assert L.convert_position_to_coordinate("Ö92") == (None, None)
    assert L.convert_position_to_coordinate("A92") == (None, None)
    assert L.convert_position_to_coordinate("Z6") == (None, None)

    assert L.modify_position_numeric("H8", -7, -7) == "A1"
    assert L.modify_position_numeric("A1", +7, +7) == "H8"
    assert L.modify_position_numeric("O15", -14, -14) == "A1"
    assert L.modify_position_numeric("H8", -10, -10) is None
    assert L.modify_position_numeric("B1", -4, -4) is None

    assert L.convert_positions_to_list("A1",
                                       "A5") == ["A1", "A2", "A3", "A4", "A5"]
    assert L.convert_positions_to_list(
        "A1", "F1") == ["A1", "B1", "C1", "D1", "E1", "F1"]
    assert L.convert_positions_to_list("A1", "A1") == ["A1"]
    assert L.convert_positions_to_list("A1") == ["A1"]
    assert L.convert_positions_to_list("A1", "B2") == []

    # Place "ERNST" on actual board
    L.set_letter_to_position("E", "A1")
    L.set_letter_to_position("R", "B1")
    L.set_letter_to_position("N", "C1")
    L.set_letter_to_position("S", "D1")
    L.set_letter_to_position("T", "E1")

    w = ""
    for currentPosition in L.convert_positions_to_list("A1", "E1"):
        w += "".join(
            L.get_letter_from_position(currentPosition, is_temporary=False))
    # print(w)
    assert w == "ERNST"

    # Place "TEMPORÄR" on temporary board
    # global recent temp. Positions are added automatically.
    L.set_letter_to_position("T", "A1", is_temporary=True)
    L.set_letter_to_position("E", "B1", is_temporary=True)
    L.set_letter_to_position("M", "C1", is_temporary=True)
    L.set_letter_to_position("P", "D1", is_temporary=True)
    L.set_letter_to_position("O", "E1", is_temporary=True)
    L.set_letter_to_position("R", "F1", is_temporary=True)
    L.set_letter_to_position("Ä", "G1", is_temporary=True)
    L.set_letter_to_position("R", "H1", is_temporary=True)

    w = ""
    for currentPosition in L.convert_positions_to_list("A1", "H1"):
        w += "".join(
            L.get_letter_from_position(currentPosition, is_temporary=True))
    # print(w)
    assert w == "TEMPORÄR"

    # get filled positions from both boards in various ways.
    # assert L.get_non_empty_positions("A1") == ["A1"]
    # assert L.get_non_empty_positions("A1", "A0") == []
    # assert L.get_non_empty_positions("A1", axis="X") is not None
    # assert L.get_non_empty_positions("A1", axis="Y") is not None
    # assert L.get_non_empty_positions("A1",
    #                                  axis="X",
    #                                  isTemporary=True) is not None
    # assert L.get_non_empty_positions("A1",
    #                                  axis="Y",
    #                                  isTemporary=True) is not None
    # assert L.get_non_empty_positions("A1",
    #                                  axis="X",
    #                                  isTemporary=True,
    #                                  returnLetters=True) is not None
    # assert L.get_non_empty_positions("A1",
    #                                  axis="Y",
    #                                  isTemporary=True,
    #                                  returnLetters=True) is not None
    # assert L.get_non_empty_positions(startPosition="A1",
    #                                  isTemporary=True,
    #                                  returnLetters=True,
    #                                  axis="X") == list("TEMPORÄR")
    # assert L.get_non_empty_positions("A1", "E1",
    #                                  returnLetters=True) == list("ERNST")
    # assert L.get_non_empty_positions("A1", "A10", returnLetters=True) == ["E"]
    # assert L.get_non_empty_positions("A1", "A10", returnLetters=True) == ["E"]
    # assert L.get_non_empty_positions("A1", "A10",
    #                                  returnLetters=True, axis="X") == list("ERNST")

    # see if "TEMPORÄR" is still in the global variable.
    w = ""
    for tempPosition in S.RECENT_TEMPORARY_POSITIONS:
        w += ''.join(
            L.get_letter_from_position(tempPosition, is_temporary=True))
    # print(w)

    # clear the recent temporary positions.
    assert len(S.RECENT_TEMPORARY_POSITIONS) > 0
    L.remove_temporary_positions()
    assert len(S.RECENT_TEMPORARY_POSITIONS) == 0

    # check if everything on the temporary Board is empty.
    # assert L.get_non_empty_positions("A1", isTemporary=True, axis="X") == []
    # assert L.get_non_empty_positions("A1", isTemporary=True, axis="Y") == []

    # get_word_from_position
    assert L.get_word_from_position("A1", "E1") == "ERNST"
    assert L.get_word_from_position("A0", "B12") == ""
    assert L.get_word_from_position("A1", "A15") == "E"

    # modifier-fields
    assert L.get_word_multiplier("A1", "A15") == 27
    assert L.get_word_multiplier("A1", "O1") == 27
    assert L.get_word_multiplier("A1", "A8") == 9
    assert L.get_word_multiplier("A1") == 3
    assert L.get_word_multiplier("A2") == 1
    assert L.get_word_multiplier("B2", "N2") == 4
    assert L.get_word_multiplier("B6") == 1

    assert L.get_letter_multiplier("B2") == 1
    assert L.get_letter_multiplier("F2") == 3
    assert L.get_letter_multiplier("B6") == 3
    assert L.get_letter_multiplier("B1") == 1

    # set entire words:
    L.set_word_to_position("ERNST", "A2", "E2")
    L.set_word_to_position("ER?ST", "A3", "E3", joker_letters="N")
    L.set_word_to_position("ER??T", "A4", "E4", joker_letters="NS")
    L.set_word_to_position("?????", "A5", "E5", joker_letters="ERNST")
    # set word with wrong endPosition: should still only place from A8 to D8
    L.set_word_to_position("EINS", "A8", "O8", axis="X")

    # read entire words
    assert L.get_word_from_position("A2", "E2") == "ERNST"
    assert L.get_word_from_position("A8", "O8") == "EINS"

    # read jokers
    assert L.get_letter_from_position("A5", show_joker=False) == "E"
    assert L.get_letter_from_position("A5", show_joker=True) == "?"

    assert L.get_word_from_position("A3", "E3", show_joker=True) == "ER?ST"
    assert L.get_word_from_position("A4", "E4", show_joker=True) == "ER??T"
    assert L.get_word_from_position("A5", "E5", show_joker=True) == "?????"
    assert L.get_word_from_position("A5", "E5", show_joker=False) == "ERNST"

    # score letters
    assert L.score_letter("E", "O1") == 1
    assert L.score_letter("Q", "O1") == 10
    assert L.score_letter("Q", "M7") == 20
    assert L.score_letter("K", "N6") == 12

    # score Words
    # assert L.scoreWord("ERNST", "A1", "E1") == 15
    # assert L.scoreWord("ERNST", "A1", axis="X") == 15
    # assert L.scoreWord("WIEDERKEHRENDER", "O1", axis="Y") == 621

    # reset everything and make sure the reset worked.
    S.reset()
    assert L.get_word_from_position("A1", "E1") == ""

    # word searches
    # remove letters from the rack as they are placed

    S.set_rack("BEIN")

    ernst = D.Play("ERNST", "F8", "x")
    erde = D.Play("ERDE", "G7", "y")
    erbse = D.Play("ERBSE", "I5", "y")

    L.execute_play(ernst)
    S.increase_turn()
    L.execute_play(erde)
    S.increase_turn()
    L.execute_play(erbse)
    S.increase_turn()

    # allPlays = WS.find_all_plays()

    # usable_a = L.convert_positions_to_list("B8", "N8")
    # possibleWords_a = WS.createWords(usable_a, "x")
    # print(possibleWords_a)
    # startingPosition = WS.findStartingPosition("BERNSTEIN",
    #                                            usable_a,
    #                                            "x",
    #                                            ernst.used_positions)
    # print("starting position for BERNSTEIN:", startingPosition)
    # print("\n")
    #
    # usable_b = L.convert_positions_to_list("E5", "L5")
    # possibleWords_b = WS.createWords(usable_b, "x")
    # print(possibleWords_b)
    # startingPosition = WS.findStartingPosition("EBEN",
    #                                            usable_b,
    #                                            "x",
    #                                            erbse.used_positions)
    #  S.set_rack("HMLSA")
    #  L.set_letter_to_position("K5", "E")
    # # L.set_letter_to_position("M5", "A")
    #  usable_b = L.convert_positions_to_list("I5", "O5")
    #  possibleWords_b = WS.createWords(usable_b, "x")
    #  # TODO: add the tests for everything in WordSearch
    #  print(possibleWords_b)

    # print(allPlays)

    S.reset()
    print("RESET".center(20, "*"))
    return