def main(): game = Game(1, 1, debug=True) status = Game.GAME_CONTINUES while status == Game.GAME_CONTINUES: print game.board try: input = raw_input('cmd: ') args = input.split(' ') if args[0] == 'T': ticks = int(args[1]) for i in xrange(ticks): status = game.tick() if status != Game.GAME_CONTINUES: break elif args[0] == 'M': from_row, from_col, to_row, to_col = int(args[1]), int( args[2]), int(args[3]), int(args[4]) piece = game.board.get_piece_by_location(from_row, from_col) valid = game.move(piece.id, piece.player, to_row, to_col) if not valid: print 'Invalid move!' except KeyboardInterrupt: break except Exception, e: print 'Error: ' + str(e)
if profile: import yappi yappi.set_clock_type('wall') yappi.start() bot = get_bot('advanced') game = Game(Speed('standard'), {}) for i in xrange(10000): if game.finished: break move = bot.get_move(game, 1, random.randint(0, 9999)) if move: piece, row, col = move if not game.move(piece.id, piece.player, row, col): print piece, row, col move = bot.get_move(game, 2, random.randint(0, 9999)) if move: piece, row, col = move if not game.move(piece.id, piece.player, row, col): print piece, row, col game.tick() if i % 100 == 0: print i if profile: yappi.stop()
PATH_DQN = "Replay_dqn_state_dict.pt" DQN_model = DQN_replay(14, 6) DQN_model.model.load_state_dict(torch.load(PATH_DQN)) game = Game() done = False while not done: # Player 1 Move board1 = game.board() game._player_one = True p1_action = DQN_model._move(game) print(f"DQN Action: {p1_action + 1}") game.move(p1_action) # End game if move meets win condition if game.over(): break # Player 2 Move render(game.board()) game._player_one = False p2_action = input("Enter an Action between 1 and 6: ") p2_action = 13 - (int(p2_action)) print(p2_action) game.move(p2_action) render(game.board()) # End game if move meets win condition if game.over():
class Cmd(): def __init__(self): self.game = Game() self.keys = { "q": self.quit_game, "P": self.print_board, "c": self.clear_playfield, "g": self.take_stream, "?s": self.print_score, "?n": self.print_line_count, "s": self.step, "t": self.print_active_tetromino, ")": self.rotate_right, "(": self.rotate_left, ";": self.print_newline, "p": self.print_frozen_board, "<": self.move_left, ">": self.move_right, "v": self.move_down, "V": self.hard_drop, ">>>>": self.move_hard_right, "<<<<": self.move_hard_left, } def quit_game(self): sys.exit() def print_board(self): print(self.game.board) def print_frozen_board(self): print(self.game.frozen_board) def print_score(self): print(self.game.score) def print_line_count(self): print(self.game.line_count) def print_active_tetromino(self): print(self.game.shape) def clear_playfield(self): self.game.clear_board() def take_stream(self): self.game.board_from_stream(input) def step(self): self.game.step() def spawn(self, t): return lambda: self.game.spawn(t) def rotate_right(self): self.game.rotate(1) def rotate_left(self): self.game.rotate(-1) def print_newline(self): print() def move_left(self): self.game.move(-1, 0) def move_right(self): self.game.move(1, 0) def move_down(self): self.game.move(0, 1) def move_hard_right(self): self.game.move_hard(1, 0) def move_hard_left(self): self.game.move_hard(-1, 0) def hard_drop(self): self.game.hard_drop() def switch(self, key): return self.keys.get(key, self.spawn(key))() def run(self): while True: keys = input().split(" ") for k in keys: if k in {"?s", "?n", ">" * 4, "<" * 4}: self.switch(k) else: for a in k: self.switch(a)