From 6cfd08095e2266cd4d0c65300b0a0668a003626d Mon Sep 17 00:00:00 2001 From: Ting-Jun Wang Date: Thu, 30 Jun 2022 23:18:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20RL=20Enviroment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- enviroment.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ game.py | 10 +++++++-- 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 enviroment.py diff --git a/enviroment.py b/enviroment.py new file mode 100644 index 0000000..4960b93 --- /dev/null +++ b/enviroment.py @@ -0,0 +1,61 @@ +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 \ No newline at end of file diff --git a/game.py b/game.py index b692dbc..d784ba4 100644 --- a/game.py +++ b/game.py @@ -100,6 +100,8 @@ class Block(): self.y -= 1 self.board.place(self) self.reset() + if(isCollision(self, self.board)): + raise Exception("GAME OVER") return False else: return True @@ -127,10 +129,15 @@ class TetrisGame(): self.block = Block(block_id, self.board) self.score = 0 + self.done = False def action(self, mode): if mode == 'd': - self.block.fall() + try: + self.block.fall() + except: + print("GAME OVER") + self.done = True elif mode == 'l': self.block.shiftLeft() elif mode == 'r': @@ -139,7 +146,6 @@ class TetrisGame(): self.block.rotate() self.score += self.board.checkScore() - print("SCORE:", self.score) def view(self): return view(self.board, self.block)