This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gameStateManager.py
311 lines (252 loc) · 10.3 KB
/
gameStateManager.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# -------------
#
# Init Settings
#
# -------------
# windowをアクティブにする
try:
import win32gui
window = win32gui.FindWindow(None, "PuyoPuyoTetris")
win32gui.SetForegroundWindow(window)
print("Window activated.", flush=True)
except:
print("win32gui not installed.")
from lib import *
from params.eval import *
# GetOccupiedPositionsの前計算
InitGetOccupiedPositions()
import boardWatcher
import decisionMaker
import minoMover
import simulator
import evaluator
import openTemplateMaker
# 探索の深さの設定
INIT_SEARCH_LIMIT = 4
INIT_BEAM_WIDTH = [3,3,3]
# 実行時引数の設定
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("mode", help="Select mode (app/sim)")
parser.add_argument("-q", "--quickSearch", help="Reduce the number of search nodes, and speed up calculation.", action="store_true")
parser.add_argument("-m", "--multiPlay", help="Play with AI in multiplayer-mode.", action="store_true")
args = parser.parse_args()
if args.quickSearch:
decisionMaker.quickSearch = True
# -------------
#
# Init Settings End
#
# -------------
# simulator上で思考を再現する(無限ループ)
def PytrisSimulator ():
# 初期化
decisionMaker.SEARCH_LIMIT = INIT_SEARCH_LIMIT
decisionMaker.BEAM_WIDTH = INIT_BEAM_WIDTH
print("\n\nPy-tris Simulator\n\n")
# 適当に盤面を生成
board = Board()
board.followingMinos = [simulator.GenerateMino() for _ in range(FOLLOWING_MINOS_COUNT)]
print("\n\n\n")
PrintBoard(board)
"""# Decide
while True:
assert type(board.score) == int
assert len(board.followingMinos) == FOLLOWING_MINOS_COUNT
board = simulator.AddFollowingMino(board)
# 思考ルーチン
value, mino, path = decisionMaker.Decide(board)
board, isTspin, isTspinmini = simulator.PutMino(path, board)
newMainBoard, newTopRowIdx, clearedRowCount = simulator.ClearLinesOfBoard(board)
scoreAdd, backToBack, ren = evaluator.Score(isTspin, isTspinmini, clearedRowCount, board.backToBack, board.ren)
board = Board(
newMainBoard,
None,
board.followingMinos,
board.holdMino,
True,
newTopRowIdx,
board.score + scoreAdd,
backToBack,
ren,
board.minoBagContents
)
"""
# Multi-Decide
# テンプレを狙える時は狙う
board = simulator.AddFollowingMino(board)
while True:
assert type(board.score) == int
assert len(board.followingMinos) == FOLLOWING_MINOS_COUNT
# 思考ルーチン
multipath = openTemplateMaker.GetCustomTemplateMove(board)
if not multipath:
multipath = decisionMaker.MultiDecide(board)
for path in multipath:
board, isTspin, isTspinmini = simulator.PutMino(path, board)
newMainBoard, newTopRowIdx, clearedRowCount = simulator.ClearLinesOfBoard(board)
scoreAdd, backToBack, ren = evaluator.Score(isTspin, isTspinmini, clearedRowCount, board.backToBack, board.ren)
board = Board(
newMainBoard,
None,
board.followingMinos,
board.holdMino,
True,
newTopRowIdx,
board.score + scoreAdd,
backToBack,
ren,
board.minoBagContents
)
board = simulator.AddFollowingMino(board)
# 実機上で思考を再現する(無限ループ、シングルスレッド)
# menu画面にいて、Startを押せる状態からはじめないとバグる
def PytrisMover ():
if args.multiPlay:
# マルチプレイヤーモード
boardWatcher.is1P = False
# キャラクターセレクト画面になるまで待機
while not boardWatcher.IsCharacterSelect():
time.sleep(0.1)
# キャラクターセレクトを実行
time.sleep(0.5)
print("'Chalacter Select' Recognized.")
time.sleep(0.5)
PressEnter()
time.sleep(0.5)
if boardWatcher.is1P: # 1Pだったらアルルなのでテトリスにずらす
Move(MOVE.RIGHT)
time.sleep(0.5)
PressEnter()
time.sleep(0.5)
PressEnter() # キャラ設定完了
time.sleep(0.5)
print("Character selected.")
else:
# シングルプレイヤーモード
pass
while True:
# 初期化
decisionMaker.SEARCH_LIMIT = INIT_SEARCH_LIMIT
decisionMaker.BEAM_WIDTH = INIT_BEAM_WIDTH
paths = []
# ゲーム開始待機状態になるまで待機
while not boardWatcher.IsGameReady():
time.sleep(0.1)
print("Ready Recognized. Wait for starting a game...")
previousFollowingMinos = [boardWatcher.GetFollowingMinos()[0]]
# ゲーム開始するまで待機
while not boardWatcher.HasGameStarted():
continue
print("Start!")
# ゲーム開始
while True:
# 消去中のラインが消えるまで待つ
while boardWatcher.IsClearingLines():
pass
# followingMinosが変化するまで待つ
while True:
if previousFollowingMinos != boardWatcher.GetFollowingMinos():
previousFollowingMinos = boardWatcher.GetFollowingMinos()
break
# 現在のミノが出てくるまで待つ
while True:
if boardWatcher.GetCurrentMino() is not None:
break
# 各列において,上から順に見ていって,一番最初にブロックがある部分のrowIdxを格納する
mainBoard = boardWatcher.GetMainBoard()
topRowIdx = [BOARD_HEIGHT for _ in range(BOARD_WIDTH)]
for rowIdx in range(BOARD_HEIGHT-1, -1, -1):
for colIdx in range(BOARD_WIDTH):
if mainBoard[rowIdx] & (0b1000000000 >> colIdx) > 0:
topRowIdx[colIdx] = rowIdx
# 盤面の状況を読み取る
board = Board(
mainBoard,
DirectedMino(
boardWatcher.GetMinoTypeOfCurrentMino(),
FIRST_MINO_DIRECTION,
FIRST_MINO_POS
),
boardWatcher.GetFollowingMinos(),
boardWatcher.GetHoldMino(),
True,
topRowIdx,
0,
False,
0
)
# 思考ルーチン
decideTimer = Timer()
if not paths:
multiPath = openTemplateMaker.GetCustomTemplateMove(board)
if not multiPath:
multiPath = decisionMaker.MultiDecide(board)
paths += multiPath
print("Making Decition in {}s".format(decideTimer.Stop()), flush=True)
# 移動
# todo: 連続でおく途中でおきミスしたときや、盤面の状況が変化したときの対処(porinky0424がやります)
mainBoard = board.mainBoard
while paths:
path = paths.pop(0)
currentMino = boardWatcher.GetMinoTypeOfCurrentMino()
# 最初に実行するのがHOLDの時は別に実行する
if path[0] is MOVE.HOLD:
time.sleep(0.1) # 安定のためにHOLDの前後にsleepを入れる
Move(MOVE.HOLD)
time.sleep(0.1)
# pathからHOLDを取り除く
path = path[1:]
# 次のミノが出てくるまで待機
while True:
if boardWatcher.GetCurrentMino() is not None:
break
currentMino = boardWatcher.GetMinoTypeOfCurrentMino()
# 移動
directedMino = minoMover.InputMove(
path,
DirectedMino(
currentMino,
FIRST_MINO_DIRECTION,
FIRST_MINO_POS
),
mainBoard
)
# おいた後の盤面を生成
joinedMainBoard = JoinDirectedMinoToBoardWithoutTopRowIdx(directedMino, mainBoard)
mainBoard, clearedRowCount = ClearLinesWithoutTopRowIdx(joinedMainBoard)
# 試合が終了して、次のゲームが始まっていないか気にしながら次の操作ができるような状態になるまで待機
isGameReady = False
if paths:
# followingMinosが変化するまで待つ
while True:
if previousFollowingMinos != boardWatcher.GetFollowingMinos():
previousFollowingMinos = boardWatcher.GetFollowingMinos()
break
# ゲーム開始待機状態に戻ったら、次のゲームに移行する
if boardWatcher.IsGameReady():
isGameReady = True
break
# 次のミノが出てくるまで待機
while True:
if boardWatcher.GetCurrentMino() is not None:
break
# ゲーム開始待機状態に戻ったら、次のゲームに移行する
if boardWatcher.IsGameReady():
isGameReady = True
break
if isGameReady:
break
# ゲーム開始待機状態に戻ったら、次のゲームに移行する
if isGameReady or boardWatcher.IsGameReady():
print("Next game started. Reloading...", flush=True)
break
def main():
if args.mode == "sim":
# simulatorモード
PytrisSimulator()
elif args.mode == "app":
# 実機確認モード
PytrisMover()
else:
Error("Invalid mode inputted.")