-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpytetxcz.py
237 lines (210 loc) · 7.66 KB
/
pytetxcz.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
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
226
227
228
229
230
231
232
233
234
235
236
237
class Matrix:
count = 0
def get_count(self):
return Matrix.count
def get_dy(self):
return self._dy
def get_dx(self):
return self._dx
def get_array(self):
return self._array
def __alloc(self, cy, cx):
if cy < 0 or cx < 0:
raise MatrixError("wrong matrix size")
self._dy = cy
self._dx = cx
self._array = [[0]*self._dx for i in range(self._dy)]
def __init__(self, arg):
Matrix.count += 1
if isinstance(arg, list):
array = arg
cy = len(array)
cx = len(array[0])
self.__alloc(cy, cx)
for y in range(cy):
for x in range(cx):
self._array[y][x] = array[y][x]
return
elif isinstance(arg, Matrix):
other = arg
cy = other._dy
cx = other._dx
self.__alloc(cy, cx)
for y in range(cy):
for x in range(cx):
self._array[y][x] = other._array[y][x]
return
else:
self.__alloc(0, 0)
return
def __str__(self):
return 'Matrix(%d, %d)' % (self._dy, self._dx)
def print(self):
print('[', end=' ')
for y in range(self._dy-1):
print('[', end=' ')
for x in range(self._dx-1):
print(self._array[y][x], end=', ')
print(self._array[y][self._dx-1], end=' ')
print('],') # , end=' ')
print('[', end=' ')
for x in range(self._dx-1):
print(self._array[self._dy-1][x], end=', ')
print(self._array[self._dy-1][self._dx-1], end=' ')
print(']') # , end=' ')
print(']')
def clip(self, top, left, bottom, right):
cy = bottom - top
cx = right - left
temp = [[0]*cx for i in range(cy)]
for y in range(cy):
for x in range(cx):
if (top+y >= 0) and (left+x >= 0) \
and (top+y < self._dy) and (left+x < self._dx):
temp[y][x] = self._array[top+y][left+x]
else:
raise MatrixError("invalid matrix range")
return Matrix(temp)
def paste(self, other, top, left):
for y in range(other._dy):
for x in range(other._dx):
if (top+y >= 0) and (left+x >= 0) \
and (top+y < self._dy) and (left+x < self._dx):
self._array[top+y][left+x] = other._array[y][x]
else:
raise MatrixError("invalid matrix range")
def __add__(self, other):
if (self._dx != other._dx) or (self._dy != other._dy):
raise MatrixError("matrix sizes mismatch")
temp = [[0]*self._dx for i in range(self._dy)]
for y in range(self._dy):
for x in range(self._dx):
temp[y][x] = self._array[y][x] + other._array[y][x]
return Matrix(temp)
def sum(self):
total = 0
for y in range(self._dy):
for x in range(self._dx):
total += self._array[y][x]
return total
def mulc(self, coef):
for y in range(self._dy):
for x in range(self._dx):
self._array[y][x] *= coef
def anyGreaterThan(self, val):
for y in range(self._dy):
temp = [v for v in self._array[y] if v > val]
if len(temp) > 0:
return True
return False
def draw_matrix(m):
array = m.get_array()
for y in range(m.get_dy()):
for x in range(m.get_dx()):
if array[y][x] == 0:
print("□", end='')
elif array[y][x] == 1:
print("■", end='')
else:
print("XX", end='')
print()
###
### initialize variables
###
arrayBlk = [[ 0, 1, 1, 0 ], [ 0, 1, 1, 0 ] ]
### integer variables: must always be integer!
iScreenDy = 15
iScreenDx = 10
iScreenDw = 4
top = 17
left = iScreenDw + iScreenDx//2 - 2
newBlockNeeded = False
arrayScreen = [
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0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, 0, 0, 0, 0, 0, 0 ] ]
##0
### prepare the initial screen output
###
iScreen = Matrix(arrayScreen)
oScreen = Matrix(iScreen)
currBlk = Matrix(arrayBlk)
tempBlk = iScreen.clip(top, left, top+currBlk.get_dy(), left+currBlk.get_dx())
tempBlk = tempBlk + currBlk
oScreen.paste(tempBlk, top, left)
draw_matrix(oScreen); print()
###
### execute the loop
###
while True:
key = input('Enter a key from [ q (quit), a (left), d (right), s (down), w (rotate), \' \' (drop) ] : ')
if key == 'q':
print('Game terminated...')
break
elif key == 'a': # move left
left -= 1
elif key == 'd': # move right
left += 1
elif key == 's': # move down
top += 1
elif key == 'w': # rotate the block clockwise
print('Not implemented')
continue
elif key == ' ': # drop the block
print('Not implemented')
continue
else:
print('Wrong key!!!')
continue
tempBlk = iScreen.clip(top, left, top+currBlk.get_dy(), left+currBlk.get_dx())
tempBlk = tempBlk + currBlk
if tempBlk.anyGreaterThan(1):
if key == 'a': # undo: move right
left += 1
elif key == 'd': # undo: move left
left -= 1
elif key == 's': # undo: move up
top -= 1
newBlockNeeded = True
elif key == 'w': # undo: rotate the block counter-clockwise
print('Not implemented')
elif key == ' ': # undo: move up
print('Not implemented')
tempBlk = iScreen.clip(top, left, top+currBlk.get_dy(), left+currBlk.get_dx())
tempBlk = tempBlk + currBlk
oScreen = Matrix(iScreen)
oScreen.paste(tempBlk, top, left)
draw_matrix(oScreen); print()
if newBlockNeeded:
iScreen = Matrix(oScreen)
top = 0
left = iScreenDw + iScreenDx//2 - 2
newBlockNeeded = False
currBlk = Matrix(arrayBlk)
tempBlk = iScreen.clip(top, left, top+currBlk.get_dy(), left+currBlk.get_dx())
tempBlk = tempBlk + currBlk
if tempBlk.anyGreaterThan(1):
print('Game Over!!!')
break
oScreen = Matrix(iScreen)
oScreen.paste(tempBlk, top, left)
draw_matrix(oScreen); print()
###
### end of the loop
###