-
Notifications
You must be signed in to change notification settings - Fork 277
/
tetris_model.py
197 lines (163 loc) · 6.16 KB
/
tetris_model.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import random
class Shape(object):
shapeNone = 0
shapeI = 1
shapeL = 2
shapeJ = 3
shapeT = 4
shapeO = 5
shapeS = 6
shapeZ = 7
shapeCoord = (
((0, 0), (0, 0), (0, 0), (0, 0)),
((0, -1), (0, 0), (0, 1), (0, 2)),
((0, -1), (0, 0), (0, 1), (1, 1)),
((0, -1), (0, 0), (0, 1), (-1, 1)),
((0, -1), (0, 0), (0, 1), (1, 0)),
((0, 0), (0, -1), (1, 0), (1, -1)),
((0, 0), (0, -1), (-1, 0), (1, -1)),
((0, 0), (0, -1), (1, 0), (-1, -1))
)
def __init__(self, shape=0):
self.shape = shape
def getRotatedOffsets(self, direction):
tmpCoords = Shape.shapeCoord[self.shape]
if direction == 0 or self.shape == Shape.shapeO:
return ((x, y) for x, y in tmpCoords)
if direction == 1:
return ((-y, x) for x, y in tmpCoords)
if direction == 2:
if self.shape in (Shape.shapeI, Shape.shapeZ, Shape.shapeS):
return ((x, y) for x, y in tmpCoords)
else:
return ((-x, -y) for x, y in tmpCoords)
if direction == 3:
if self.shape in (Shape.shapeI, Shape.shapeZ, Shape.shapeS):
return ((-y, x) for x, y in tmpCoords)
else:
return ((y, -x) for x, y in tmpCoords)
def getCoords(self, direction, x, y):
return ((x + xx, y + yy) for xx, yy in self.getRotatedOffsets(direction))
def getBoundingOffsets(self, direction):
tmpCoords = self.getRotatedOffsets(direction)
minX, maxX, minY, maxY = 0, 0, 0, 0
for x, y in tmpCoords:
if minX > x:
minX = x
if maxX < x:
maxX = x
if minY > y:
minY = y
if maxY < y:
maxY = y
return (minX, maxX, minY, maxY)
class BoardData(object):
width = 10
height = 22
def __init__(self):
self.backBoard = [0] * BoardData.width * BoardData.height
self.currentX = -1
self.currentY = -1
self.currentDirection = 0
self.currentShape = Shape()
self.nextShape = Shape(random.randint(1, 7))
self.shapeStat = [0] * 8
def getData(self):
return self.backBoard[:]
def getValue(self, x, y):
return self.backBoard[x + y * BoardData.width]
def getCurrentShapeCoord(self):
return self.currentShape.getCoords(self.currentDirection, self.currentX, self.currentY)
def createNewPiece(self):
minX, maxX, minY, maxY = self.nextShape.getBoundingOffsets(0)
result = False
if self.tryMoveCurrent(0, 5, -minY):
self.currentX = 5
self.currentY = -minY
self.currentDirection = 0
self.currentShape = self.nextShape
self.nextShape = Shape(random.randint(1, 7))
result = True
else:
self.currentShape = Shape()
self.currentX = -1
self.currentY = -1
self.currentDirection = 0
result = False
self.shapeStat[self.currentShape.shape] += 1
return result
def tryMoveCurrent(self, direction, x, y):
return self.tryMove(self.currentShape, direction, x, y)
def tryMove(self, shape, direction, x, y):
for x, y in shape.getCoords(direction, x, y):
if x >= BoardData.width or x < 0 or y >= BoardData.height or y < 0:
return False
if self.backBoard[x + y * BoardData.width] > 0:
return False
return True
def moveDown(self):
lines = 0
if self.tryMoveCurrent(self.currentDirection, self.currentX, self.currentY + 1):
self.currentY += 1
else:
self.mergePiece()
lines = self.removeFullLines()
self.createNewPiece()
return lines
def dropDown(self):
while self.tryMoveCurrent(self.currentDirection, self.currentX, self.currentY + 1):
self.currentY += 1
self.mergePiece()
lines = self.removeFullLines()
self.createNewPiece()
return lines
def moveLeft(self):
if self.tryMoveCurrent(self.currentDirection, self.currentX - 1, self.currentY):
self.currentX -= 1
def moveRight(self):
if self.tryMoveCurrent(self.currentDirection, self.currentX + 1, self.currentY):
self.currentX += 1
def rotateRight(self):
if self.tryMoveCurrent((self.currentDirection + 1) % 4, self.currentX, self.currentY):
self.currentDirection += 1
self.currentDirection %= 4
def rotateLeft(self):
if self.tryMoveCurrent((self.currentDirection - 1) % 4, self.currentX, self.currentY):
self.currentDirection -= 1
self.currentDirection %= 4
def removeFullLines(self):
newBackBoard = [0] * BoardData.width * BoardData.height
newY = BoardData.height - 1
lines = 0
for y in range(BoardData.height - 1, -1, -1):
blockCount = sum([1 if self.backBoard[x + y * BoardData.width] > 0 else 0 for x in range(BoardData.width)])
if blockCount < BoardData.width:
for x in range(BoardData.width):
newBackBoard[x + newY * BoardData.width] = self.backBoard[x + y * BoardData.width]
newY -= 1
else:
lines += 1
if lines > 0:
self.backBoard = newBackBoard
return lines
def mergePiece(self):
for x, y in self.currentShape.getCoords(self.currentDirection, self.currentX, self.currentY):
self.backBoard[x + y * BoardData.width] = self.currentShape.shape
self.currentX = -1
self.currentY = -1
self.currentDirection = 0
self.currentShape = Shape()
def clear(self):
self.currentX = -1
self.currentY = -1
self.currentDirection = 0
self.currentShape = Shape()
self.backBoard = [0] * BoardData.width * BoardData.height
def gameOver(self):
for x in range(BoardData.width):
if self.backBoard[x] > 0:
return True
return False
BOARD_DATA = BoardData()