-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtetrominoes.py
75 lines (62 loc) · 2.67 KB
/
tetrominoes.py
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
from __future__ import print_function
from collections import namedtuple
X, O = 'X', None
Tetromino = namedtuple("Tetrimino", "color shape")
tetrominoes = {
"long": Tetromino(color="blue",
shape=((O,O,O,O),
(X,X,X,X),
(O,O,O,O),
(O,O,O,O))),
"square": Tetromino(color="yellow",
shape=((X,X),
(X,X))),
"hat": Tetromino(color="pink",
shape=((O,X,O),
(X,X,X),
(O,O,O))),
"right_snake": Tetromino(color="green",
shape=((O,X,X),
(X,X,O),
(O,O,O))),
"left_snake": Tetromino(color="red",
shape=((X,X,O),
(O,X,X),
(O,O,O))),
"left_gun": Tetromino(color="cyan",
shape=((X,O,O),
(X,X,X),
(O,O,O))),
"right_gun": Tetromino(color="orange",
shape=((O,O,X),
(X,X,X),
(O,O,O)))
}
list_of_tetrominoes = list(tetrominoes.values())
def rotate(shape, times=1):
""" Rotate a shape to the right """
return shape if times == 0 else rotate(tuple(zip(*shape[::-1])), times-1)
def shape_str(shape):
""" Return a string of a shape in human readable form """
return '\n'.join(''.join(map({'X': 'X', None: 'O'}.get, line))
for line in shape)
def shape(shape):
""" Print a shape in human readable form """
print(shape_str(shape))
def test():
tetromino_shapes = [t.shape for t in list_of_tetrominoes]
map(rotate, tetromino_shapes)
map(shape, tetromino_shapes)
map(shape_str, tetromino_shapes)
assert shape_str(tetrominoes["left_snake"].shape) == "XXO\nOXX\nOOO"
assert rotate(tetrominoes["square"].shape) == tetrominoes["square"].shape
assert rotate(tetrominoes["right_snake"].shape, 4) == tetrominoes["right_snake"].shape
assert rotate(tetrominoes["hat"].shape) == ((O,X,O),
(O,X,X),
(O,X,O))
assert rotate(tetrominoes["hat"].shape, 2) == ((O,O,O),
(X,X,X),
(O,X,O))
print("All tests passed in {}, things seems to be working alright".format(__file__))
if __name__ == '__main__':
test()