aboutsummaryrefslogtreecommitdiffstats
path: root/tests_solver/test_solver.py
blob: 808804ee16164261f9b615950c50714d7e5e5abe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python3

import os
import pprint
import sys

sys.path.append(os.getcwd())

from solver import *

def test_is_swappable():

    swappable_grids = [
            [Kana("norm", "su"), Kana("norm", "su")],
            [Kana("froz", "su"), Kana("norm", "su")],
            [Kana("norm", "su"), Kana("norm"      )],
            [Kana("norm"      ), Kana("froz", "su")],
            [Kana("norm"      ), Kana("norm"      )],
    ]

    for swappable_grid in swappable_grids:
        kanagrid = KanaGrid((2, 1), swappable_grid)
        print(kanagrid.grid)
        assert kanagrid.is_swappable((0, 0), (1, 0))

    not_swappable_grids = [
            [Kana("norm", "su"), Kana("rock", "su")],
            [Kana("froz", "su"), Kana("froz", "su")],
    ]

    for not_swappable_grid in not_swappable_grids:
        kanagrid = KanaGrid((2, 1), not_swappable_grid)
        print(kanagrid.grid)
        assert not kanagrid.is_swappable((0, 0), (1, 0))


def test_action_reveal():

    kanagrid_orig = KanaGrid((1, 1), [Kana("myst", "ko")])
    kanagrid_new = kanagrid_orig.action(pos=(0, 0), action_type="reveal")
    expected_grid = KanaGrid((1, 1), [Kana("norm", "ko")], action_count=1)

    assert kanagrid_new == expected_grid


def test_action_slime_infest():

    kanagrid_orig = KanaGrid((2, 1), [Kana("norm", "ko"), Kana("slim", "e")])
    kanagrid_new_1 = kanagrid_orig.action(pos=(1, 0), action_type="left")
    kanagrid_new_2 = kanagrid_orig.action(pos=(0, 0), action_type="right")
    expected_grid = KanaGrid((2, 1), [Kana("norm", "ke"), Kana("void")], action_count=1)

    assert kanagrid_new_1 == expected_grid
    # Move of slime only is implemented, not other to slime
    assert kanagrid_new_2 == None


def test_kana_grid():

    initial_grid = [
        kana_void         , Kana("myst", "su"), kana_void         , Kana("myst", "ko"), kana_void         ,
        Kana("froz", "se"), Kana("norm"      ), Kana("norm"      ), Kana("norm"      ), Kana("froz", "so"),
        Kana("froz", "ku"), Kana("norm"      ), Kana("norm"      ), Kana("norm"      ), Kana("froz", "no"),
        kana_void         , kana_void         , Kana("rock", "ka"), kana_void         , kana_void         ,
    ]
    initial_grid_size = 5, 4
    chain_target = 7

    expected_grid = [
        kana_void         , Kana("myst", "su"), kana_void         , Kana("myst", "ko"), kana_void         ,
        Kana("norm"      ), Kana("norm"      ), Kana("norm"      ), Kana("froz", "se"), Kana("froz", "so"),
        Kana("froz", "ku"), Kana("norm"      ), Kana("norm"      ), Kana("norm"      ), Kana("froz", "no"),
        kana_void         , kana_void         , Kana("rock", "ka"), kana_void         , kana_void         ,
    ]

    kanagrid_orig = KanaGrid(initial_grid_size, initial_grid)
    kanagrid_new  = kanagrid_orig.action(pos=(0, 1), action_type="right")

    print("kanagrid_orig")
    print(kanagrid_orig)
    print("kanagrid_new")
    print(kanagrid_new)
    print("expected_grid")
    display_grid(expected_grid, initial_grid_size)

    assert kanagrid_new.grid == expected_grid


def test_kana_arrow_swap():
    kanagrid_orig = KanaGrid((2, 1), [Kana("ar_r", "su"), Kana("norm",)])
    kanagrid_new = kanagrid_orig.action(pos=(0, 0), action_type="right")
    expected_grid = KanaGrid((2, 1), [Kana("norm",), Kana("ar_r", "su")])

    print("kanagrid_orig")
    print(kanagrid_orig)
    print("kanagrid_new")
    print(kanagrid_new)
    print("expected_grid")
    print(expected_grid)

    assert kanagrid_new.grid == expected_grid.grid

def test_kana_arrow_swap():
    moves = [
        {
            'move_ok': 'up',
            'other_move_ok': 'down',
            'dest_pos': (1, 0),
            'orig': KanaGrid((3, 3), [
                Kana("void"      ), Kana("norm"      ), Kana("void"      ),
                Kana("norm"      ), Kana("ar_u", "ko"), Kana("norm"      ),
                Kana("void"      ), Kana("norm"      ), Kana("void"      ),
            ]),
            'moved': KanaGrid((3, 3), [
                Kana("void"      ), Kana("ar_u", "ko"), Kana("void"      ),
                Kana("norm"      ), Kana("norm"      ), Kana("norm"      ),
                Kana("void"      ), Kana("norm"      ), Kana("void"      ),
            ], action_count=1),
        },
        {
            'move_ok': 'right',
            'other_move_ok': 'left',
            'dest_pos': (2, 1),
            'orig': KanaGrid((3, 3), [
                Kana("void"      ), Kana("norm"      ), Kana("void"      ),
                Kana("norm"      ), Kana("ar_r", "ko"), Kana("norm"      ),
                Kana("void"      ), Kana("norm"      ), Kana("void"      ),
            ]),
            'moved': KanaGrid((3, 3), [
                Kana("void"      ), Kana("norm"      ), Kana("void"      ),
                Kana("norm"      ), Kana("norm"      ), Kana("ar_r", "ko"),
                Kana("void"      ), Kana("norm"      ), Kana("void"      ),
            ], action_count=1),
        },
        {
            'move_ok': 'down',
            'other_move_ok': 'up',
            'dest_pos': (1, 2),
            'orig': KanaGrid((3, 3), [
                Kana("void"      ), Kana("norm"      ), Kana("void"      ),
                Kana("norm"      ), Kana("ar_d", "ko"), Kana("norm"      ),
                Kana("void"      ), Kana("norm"      ), Kana("void"      ),
            ]),
            'moved': KanaGrid((3, 3), [
                Kana("void"      ), Kana("norm"      ), Kana("void"      ),
                Kana("norm"      ), Kana("norm"      ), Kana("norm"      ),
                Kana("void"      ), Kana("ar_d", "ko"), Kana("void"      ),
            ], action_count=1),
        },
        {
            'move_ok': 'left',
            'other_move_ok': 'right',
            'dest_pos': (0, 1),
            'orig': KanaGrid((3, 3), [
                Kana("void"      ), Kana("norm"      ), Kana("void"      ),
                Kana("norm"      ), Kana("ar_l", "ko"), Kana("norm"      ),
                Kana("void"      ), Kana("norm"      ), Kana("void"      ),
            ]),
            'moved': KanaGrid((3, 3), [
                Kana("void"      ), Kana("norm"      ), Kana("void"      ),
                Kana("ar_l", "ko"), Kana("norm"      ), Kana("norm"      ),
                Kana("void"      ), Kana("norm"      ), Kana("void"      ),
            ], action_count=1),
        },
    ]

    for move in moves:
        print("pprint.pprint(move):")
        pprint.pprint(move)
        for action in ("up", "right", "down", "left"):
            print("\n"*3)
            print("action:", action)

            # try to move arrowed kana
            kanagrid_test = move['orig'].copy()
            kanagrid_test = kanagrid_test.action((1, 1), action)
            print("=== move arrowed kana ===")
            print(kanagrid_test)
            if action == move['move_ok']:
                assert move['orig'].is_swappable((1, 1), move['dest_pos'])
                assert kanagrid_test == move['moved']
            else:
                assert kanagrid_test is None

            for other_pos in ((1, 0), (2, 1), (1, 2), (0, 1)):
                # try to move other kana with the arrowed one
                kanagrid_test = move['orig'].copy()
                kanagrid_test = kanagrid_test.action(other_pos, action)
                print("=== move other kana ===")
                print(kanagrid_test)
                if action == move['other_move_ok'] \
                        and other_pos == move['dest_pos']:
                    assert move['orig'].is_swappable(other_pos, (1, 1))
                    assert kanagrid_test == move['moved']
                else:
                    assert kanagrid_test is None


def test_solver_load_and_type_1():
    # ./solver.py tests_solver/level_test_type.yaml

    kanagrid_orig = KanaGrid((6, 2), [
                Kana("ar_r", "sa"), Kana("norm"      ), Kana("norm", "so"), Kana("rock", "se"), Kana("myst", "ka"), Kana("ar_d", "na"),
                Kana("ar_u", "su"), Kana("ar_l", "nu"), Kana("void"      ), Kana("void"      ), Kana("froz"      ), Kana("froz", "ke"),
            ])

    expected_grid = KanaGrid((6, 2), [
                Kana("ar_u", "su"), Kana("ar_r", "sa"), Kana("norm", "so"), Kana("rock", "se"), Kana("froz", "ke"), Kana("norm", "ka"),
                Kana("ar_l", "nu"), Kana("norm"      ), Kana("void"      ), Kana("void"      ), Kana("froz"      ), Kana("ar_d", "na"),
            ], action_count=6, score=8)

    type_yaml_level_file = 'tests_solver/level_test_type.yaml'
    with open(type_yaml_level_file, encoding='utf8') as stream:
        input_dict = yaml.safe_load(stream)
    loaded_grid = KanaGrid.load(input_dict)
    target_score = input_dict['target_score']
    max_actions = input_dict['max_actions']

    assert loaded_grid == kanagrid_orig

    grids = list(search_all_solution(loaded_grid, target_score, max_actions))

    assert len(grids) == 1
    assert grids[0] == expected_grid