-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtetrosolver.py
91 lines (69 loc) · 2.9 KB
/
tetrosolver.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
import random
import piece
from field import Field, FieldConfig
class TetroConfig():
def __init__(self):
self.log = False
self.fieldConfig = FieldConfig()
random.seed(None)
class TetroSolver():
def __init__(self, game, solverConfig):
self.__field = Field(solverConfig.fieldConfig)
self.__game = game
self.__config = solverConfig
def getGameScore(self):
if self.__game.scoringCalculator is not None:
return self.__game.scoringCalculator.scoring
else:
return self.__field.rowsCleared
def check(self):
res = True
currentPiece = self.__game.piecePicker.getNewPiece()
if currentPiece is not None:
res = self.__addToBestPosition(currentPiece)
return res
def getField(self):
return self.__field
def __addToBestPosition(self, thePiece):
bestField = None
bestScore = 999999999
bestRotation = 0
bestPosition = 0
bestColumnHeights = []
for rotation in range(4):
if rotation > 0 and thePiece.pieceType == piece.PieceType.O_SHAPE:
# No need to check rotated O
break
if rotation > 1 and \
(thePiece.pieceType == piece.PieceType.I_SHAPE or
thePiece.pieceType == piece.PieceType.S_SHAPE or
thePiece.pieceType == piece.PieceType.Z_SHAPE):
# No need to check more than 2 rotations for I, S, Z
break
thePiece.setRotation(rotation)
for position in range(self.__field.WIDTH - thePiece.getWidth() + 1):
score, field, height, clearedLines, columnHeights = self.__field.tryAddPiece(thePiece, position)
if score < bestScore:
bestScore = score
bestRotation = rotation
bestPosition = position
bestField = field
bestHeight = height
bestClearedLines = clearedLines
bestColumnHeights = columnHeights
if bestField == None:
if self.__config.log:
print("Found no place to add piece")
return False
if self.__config.log:
print(f"Move piece to position {bestPosition} with rotation {bestRotation}")
self.__field.validatePieceAdd(bestField, thePiece, bestRotation, bestHeight, bestClearedLines, bestColumnHeights)
if bestHeight > Field.LOSE_HEIGHT:
if self.__config.log:
print(f"Height was too high({bestHeight}), game lost")
return False
if self.__game.outputManager is not None:
self.__game.outputManager.output(bestRotation, bestPosition)
if self.__game.scoringCalculator is not None:
self.__game.scoringCalculator.updateSore(bestClearedLines)
return True