61 lines
1.8 KiB
Python
61 lines
1.8 KiB
Python
from game import TetrisGame
|
|
|
|
class TetrisEnviroment():
|
|
def __init__(self) -> None:
|
|
self.game = TetrisGame()
|
|
self.score = 0
|
|
|
|
def step(self, mode):
|
|
if mode == 0: # 不動
|
|
None
|
|
elif mode == 1: # left 1
|
|
self.game.action('l')
|
|
elif mode == 2: # left 2
|
|
for i in range(2):
|
|
self.game.action('l')
|
|
elif mode == 3: # left 3
|
|
for i in range(3):
|
|
self.game.action('l')
|
|
elif mode == 4: # right 1
|
|
self.game.action('r')
|
|
elif mode == 5: # right 2
|
|
for i in range(2):
|
|
self.game.action('r')
|
|
elif mode == 6: # right 3
|
|
for i in range(3):
|
|
self.game.action('r')
|
|
elif mode == 7: # rotate 1
|
|
self.game.action('f')
|
|
elif mode == 8: # rotate 2
|
|
for i in range(2):
|
|
self.game.action('f')
|
|
elif mode == 9: # rotate 3
|
|
for i in range(3):
|
|
self.game.action('f')
|
|
self.game.action('d')
|
|
|
|
deltaScore = self.game.score - self.score
|
|
self.score = self.game.score
|
|
|
|
return self.game.view(), deltaScore, self.game.done, (self.game.block.x, self.game.block.block.y)
|
|
# observation, reward, done, info(block location)
|
|
|
|
def observation(self):
|
|
return self.game.view()
|
|
|
|
env = TetrisEnviroment()
|
|
while 1:
|
|
views = env.observation()
|
|
for i in range(20):
|
|
print(str(i).rjust(2), end=' ')
|
|
for j in range(10):
|
|
if views[i][j]:
|
|
print('■', end='')
|
|
else:
|
|
print('□', end='')
|
|
print()
|
|
action = int(input("Action: "))
|
|
observation, reward, done = env.step(action)
|
|
print(observation, reward, done)
|
|
if done:
|
|
break |