1+ # test_slider_puzzle.py
2+ import slider_puzzle as sp
3+
4+
5+ def test_is_solved_true_for_solved_board ():
6+ board = list (range (1 , 16 )) + [0 ]
7+ assert sp .is_solved (board ) is True
8+
9+
10+ def test_is_solved_false_for_unsolved_board ():
11+ board = [1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12 ,13 ,15 ,14 ,0 ]
12+ assert sp .is_solved (board ) is False
13+
14+
15+ def test_inversion_count_solved_board_zero ():
16+ board = list (range (1 , 16 )) + [0 ]
17+ assert sp .inversion_count (board ) == 0
18+
19+
20+ def test_inversion_count_simple_inversion ():
21+ board = [1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12 ,13 ,15 ,14 ,0 ]
22+ assert sp .inversion_count (board ) == 1
23+
24+
25+ def test_is_solvable_for_solved_board ():
26+ board = list (range (1 , 16 )) + [0 ]
27+ assert sp .is_solvable (board ) is True
28+
29+
30+ def test_is_solvable_false_for_known_unsolvable ():
31+ board = [1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12 ,13 ,15 ,14 ,0 ]
32+ assert sp .is_solvable (board ) is False
33+
34+
35+ def test_generate_shuffled_board_properties ():
36+ board = sp .generate_shuffled_board ()
37+ assert len (board ) == 16
38+ assert sorted (board ) == list (range (16 )) # 0..15
39+ assert sp .is_solvable (board ) is True
40+ assert sp .is_solved (board ) is False
41+
42+
43+ def test_can_move_neighbors_true ():
44+ board = [1 ,2 ,3 ,4 ,
45+ 5 ,6 ,7 ,8 ,
46+ 9 ,10 ,0 ,11 ,
47+ 13 ,14 ,15 ,12 ]
48+ # leeres Feld Index 10
49+ assert sp .can_move (board , 9 , 10 ) is True
50+ assert sp .can_move (board , 11 , 10 ) is True
51+ assert sp .can_move (board , 6 , 10 ) is True
52+ assert sp .can_move (board , 14 , 10 ) is True
53+
54+
55+ def test_can_move_diagonal_false ():
56+ board = [1 ,2 ,3 ,4 ,
57+ 5 ,6 ,7 ,8 ,
58+ 9 ,10 ,0 ,11 ,
59+ 13 ,14 ,15 ,12 ]
60+ assert sp .can_move (board , 5 , 10 ) is False # diagonal
61+
62+
63+ def test_can_move_wrap_around_false ():
64+ board = [1 ,2 ,3 ,4 ,
65+ 5 ,6 ,7 ,8 ,
66+ 9 ,10 ,0 ,11 ,
67+ 13 ,14 ,15 ,12 ]
68+ assert sp .can_move (board , 7 , 8 ) is False
69+
70+
71+ def test_move_tile_valid_and_board_changes ():
72+ board = [1 ,2 ,3 ,4 ,
73+ 5 ,6 ,7 ,8 ,
74+ 9 ,10 ,0 ,11 ,
75+ 13 ,14 ,15 ,12 ]
76+ zero_before = board .index (0 )
77+ moved = sp .move_tile (board , "a" ) # versucht, 0 nach rechts zu bewegen
78+ assert moved is True
79+ assert board .index (0 ) != zero_before
80+
81+
82+ def test_move_tile_invalid_direction ():
83+ board = list (range (1 , 16 )) + [0 ]
84+ copy = board .copy ()
85+ moved = sp .move_tile (board , "x" )
86+ assert moved is False
87+ assert board == copy
88+
89+ def test_move_tile_blocked_at_edges ():
90+ # Leerfeld oben links: 'w' und 'a' dürfen nichts tun
91+ board_top_left = [
92+ 0 , 2 , 3 , 4 ,
93+ 5 , 6 , 7 , 8 ,
94+ 9 , 10 , 11 , 12 ,
95+ 13 ,14 , 15 , 1
96+ ]
97+ copy_top_left = board_top_left .copy ()
98+ assert sp .move_tile (board_top_left , "w" ) is False
99+ assert board_top_left == copy_top_left
100+ assert sp .move_tile (board_top_left , "a" ) is False
101+ assert board_top_left == copy_top_left
102+
103+ # Leerfeld oben rechts: 'w' und 'd' dürfen nichts tun
104+ board_top_right = [
105+ 1 , 2 , 3 , 0 ,
106+ 5 , 6 , 7 , 8 ,
107+ 9 , 10 , 11 , 12 ,
108+ 13 ,14 , 15 , 4
109+ ]
110+ copy_top_right = board_top_right .copy ()
111+ assert sp .move_tile (board_top_right , "w" ) is False
112+ assert board_top_right == copy_top_right
113+ assert sp .move_tile (board_top_right , "d" ) is False
114+ assert board_top_right == copy_top_right
115+
116+ # Leerfeld unten links: 's' und 'a' dürfen nichts tun
117+ board_bottom_left = [
118+ 1 , 2 , 3 , 4 ,
119+ 5 , 6 , 7 , 8 ,
120+ 9 , 10 , 11 , 12 ,
121+ 0 , 14 , 15 , 13
122+ ]
123+ copy_bottom_left = board_bottom_left .copy ()
124+ assert sp .move_tile (board_bottom_left , "s" ) is False
125+ assert board_bottom_left == copy_bottom_left
126+ assert sp .move_tile (board_bottom_left , "a" ) is False
127+ assert board_bottom_left == copy_bottom_left
128+
129+ # Leerfeld unten rechts: 's' und 'd' dürfen nichts tun
130+ board_bottom_right = [
131+ 1 , 2 , 3 , 4 ,
132+ 5 , 6 , 7 , 8 ,
133+ 9 , 10 , 11 , 12 ,
134+ 13 ,14 , 15 , 0
135+ ]
136+ copy_bottom_right = board_bottom_right .copy ()
137+ assert sp .move_tile (board_bottom_right , "s" ) is False
138+ assert board_bottom_right == copy_bottom_right
139+ assert sp .move_tile (board_bottom_right , "d" ) is False
140+ assert board_bottom_right == copy_bottom_right
0 commit comments