Beispiel #1
0
def make_env():
  #env = gym.envs.make(FLAGS.env)
  # remove the timelimitwrapper
  #env = env.env
  #if wrap:
   # env = atari_helpers.AtariEnvWrapper(env)
  return Tetris(action_type = FLAGS.action_type,use_fitness=True)
    def __init__(self):
        self.t = Tetris()

        state_num = 2**(
            4 * 8
        ) * 4 * 4 * 9 * 5  # 4x8 board [filled or not], 4*9 active-shape locations, 4 rotation positions, 5 shape types
        action_num = 4  # rotate, left, right, step
        #P = {s : {a : [] for a in range(action_num)} for s in range(state_num)}

        init_state_dist = []
        for x in range(4):
            for rot in range(4):
                for shape_type in range(5):
                    init_state_dist.append(encode([0, 0, x, rot, shape_type]))

        init_state_dist = np.array(init_state_dist)
        #init_state_dist /= init_state_dist.sum()

        #super(TetrisEnv, self).__init__(state_num, action_num, P, init_state_dist)

        self.action_space = spaces.Discrete(5)

        self.observation_space = spaces.Tuple(
            (spaces.Discrete(2**(4 * 8)), spaces.Discrete(4 * 9),
             spaces.Discrete(4), spaces.Discrete(5)))
        size = 2**(4 * 8) * 4 * 4 * 9 * 5
        #-int(-np.log(2**(4*8)*4*4*9*5)/np.log(2))
        self.observation_space.shape = np.zeros(size, dtype=int)
Beispiel #3
0
def hand_crafted():
    env = Tetris()
    current_state = env.reset()
    done = False
    steps = 0

    agent = Handcrafted_agent(env.get_state_size())

    # Game
    while not done:
        next_states = env.get_next_states_handcrafted()
        best_state = agent.best_state(next_states.values())
        
        best_action = None
        for action, state in next_states.items():
            if state == best_state:
                best_action = action
                break

        reward, done = env.play(best_action[0], best_action[1], render=True,
                                render_delay=None)
        
        current_state = next_states[best_action]
        steps += 1
    
    return env.get_game_score()
    def consumer(self, msg):
        if self.game is None:
            if 'snake' in msg:
                self.game = Snake(20, 25)
                self.run_thread()
            if 'tetris' in msg:
                self.game = Tetris(20, 25)
                self.run_thread()
        else:
            # fire all of the events
            if len(msg) > 0:
                #with self.lock:

                if 'left' in msg:
                    self.eventwatcher += self.game.on_left
                elif 'right' in msg:
                    self.eventwatcher += self.game.on_right
                elif 'up' in msg:
                    self.eventwatcher += self.game.on_up
                elif 'down' in msg:
                    self.eventwatcher += self.game.on_down
                elif 'abutton' in msg:
                    self.eventwatcher += self.game.on_a
                elif 'bbutton' in msg:
                    self.eventwatcher += self.game.on_b
Beispiel #5
0
    def __init__(self, cfg):
        self.num_states = cfg.MODEL.SIZE_STATE
        self.num_actions = cfg.MODEL.SIZE_ACTION
        self.num_episodes = cfg.SOLVER.NUM_EPISODES

        self.tetris = Tetris(cfg)
        self.agent = Agent(cfg,self.tetris)
Beispiel #6
0
def run():
    game = Tetris()
    episodes = 2000
    discount=0.9 
    epsilon=.99
    nuerons=[32, 32, 32]
    activation_functions=['relu', 'relu', 'relu', 'linear']
    batchSize = 32
    epoch = 5
    trainIteration = 1

    dqn = Agent(4, discount, epsilon, nuerons, activation_functions)
    episodes = 2000
    scores = []
    for episode in range(episodes):
        current = game.newGame()
        gameOver, step = False, 0
        renderEpisode = episode % 50 == 0

        while not gameOver:
            actions = game.getLegalActions()
            bestAction, bestState = dqn.best_state(list(actions.keys()), list(actions.values()))

            reward, gameOver = game.play(xLoc=bestAction[0], degrees=bestAction[1], render=renderEpisode)
            dqn.add_sample(current, bestState, reward, gameOver)
            current = bestState
            scores.append(reward)
        
        if episode % trainIteration == 0:
            dqn.train(batch_size=batchSize, epoch=epoch)
Beispiel #7
0
def new_client(client, server):
	global tetris, command, first_run
	id = client['id']
	print("New client(%d) connected" % id)
	tetris.insert(id, Tetris())
	command.insert(id, "")
	print("Game created")
Beispiel #8
0
 def getSample(self):
     '''gets a sample trajectory from
        the specified domain
     '''
     d = Chain()
     if self.domain == "blackjack":
         d = Game()
     elif self.domain == "wumpus":
         d = Grid(4)
     elif self.domain == "blocksworld":
         d = BW(4)
     elif self.domain == "traffic":
         d = TrafficSignal()
     elif self.domain == "pong":
         d = Pong()
     elif self.domain == "tetris":
         d = Tetris()
     #elif wumpus
     sample = []
     state = d
     actions = d.actions
     while True:
         if state == "winner" or state == "loser":
             sample += [deepcopy(state)]
             break
         sample += [deepcopy(str(state))]
         action = actions[randint(0,len(actions)-1)]
         state = d.takeAction(state,action)
     return sample
def test(model, model_path):
    if torch.cuda.is_available():
        torch.cuda.manual_seed(123)
    else:
        torch.manual_seed(123)

    model.load_model(model_path)

    env = Tetris(width=TETRIS_WIDTH,
                 height=TETRIS_HEIGHT,
                 block_size=TETRIS_BLOCK_SIZE)
    env.reset()

    if torch.cuda.is_available():
        model.cuda()

    while True:
        next_steps = env.get_next_states()
        next_actions, next_states = zip(*next_steps.items())
        action, _ = model.choose_action(next_actions,
                                        next_states,
                                        is_random=False)
        _, done = env.step(action, render=True)

        if done:
            print("Cleared: {}".format(env.cleared_lines))
            break
Beispiel #10
0
def fitness(network):
    env = Tetris()
    done = False
    while not done:
        next_states = env.get_next_states()
        flattenedBoard = np.array(env._get_complete_board()).flatten()
        outputs = network.computeOutput(flattenedBoard)
        outputs = [t for t in enumerate(outputs)]
        outputs.sort(key=lambda input: input[1])

        i = 0
        best_state = None
        while best_state is None:
            best_index = outputs[i][0]
            i -= -1
            best_action = (int(best_index / 4), int(best_index % 4))
            for action, state in next_states.items():
                if action == best_action:
                    best_state = state
                    break

            if best_action is None:
                print(best_state, "is not a valid action")

        reward, done = env.play(best_action[0],
                                best_action[1],
                                render=True,
                                render_delay=None)

    return env.get_game_score()
Beispiel #11
0
 def __init__(self,FA="LSReg",domain="50chain",N=100,loss="ls",trees=500,type="max",depth=2):
     '''class constructor'''
     self.domain = domain
     if domain == "50chain":
         self.domObj = Chain()
     elif domain == "blackjack":
         self.domObj = Game()
     elif domain == "wumpus":
         self.domObj = Grid(4)
     elif domain == "blocksworld":
         self.domObj = BW(4)
     elif domain == "traffic":
         self.domObj = TrafficSignal()
     elif domain == "pong":
         self.domObj = Pong()
     elif domain == "tetris":
         self.domObj = Tetris()
     if FA == "LSReg":
         self.FA = LSReg()
     elif FA == "NN":
         self.FA = NeuralNetwork()
     elif FA == "GB":
         self.FA = GradientBooster(loss=loss,trees=trees,depth=depth)
     self.value,self.count = {},{}
     self.values = [{} for i in range(N)]
     self.approxValues = [{} for i in range(N)]
     self.BE = []
     self.type = type
     self.TD(self.FA,N)
    def getSimulator(self, simulator):
        if simulator == "logistics":
            return Logistics(start=True)

        elif simulator == "pong":
            return Pong(start=True)

        elif simulator == "tetris":
            return Tetris(start=True)

        elif simulator == "wumpus":
            return Wumpus(start=True)

        elif simulator == "blocks":
            return Blocks_world(start=True)

        elif simulator == "blackjack":
            return Game(start=True)

        elif simulator == "50chain":
            return Chain(start=True)

        elif simulator == "net_admin":
            return Admin(start=True)
        else:
            print('{} is not a supported simulation'.format(simulator))
            raise NameError()
Beispiel #13
0
def main():
    game = Tetris()
    try:
        game.start_game()
    except Exception:
        pass
    curses.endwin()
Beispiel #14
0
    def start_tetris(self):
        self.tetris = Tetris(self.app.ipcon)

        if self.tetris.okay:
            self.tetris.run_game_loop()

        self.tetris = None
    def __init__(self):
        self.t = Tetris()
        self.action_space = spaces.Discrete(5)

        self.observation_space = spaces.Tuple(
            (spaces.Discrete(32), spaces.Discrete(4)))
        self.seed()
Beispiel #16
0
 def run(self):
     """Run this game."""
     clock = pygame.time.Clock()
     count = 0
     while True:
         for event in pygame.event.get():
             if event.type == pygame.QUIT:
                 pygame.quit()
                 if self.mode == 1:
                     with open('data.json', 'w') as f:
                         json.dump({'record': self.record}, f)
                 exit()
             elif event.type == pygame.KEYDOWN:
                 if event.key == pygame.K_RETURN:
                     if self.game.state == 0:
                         if self.game.score.points > self.record and self.mode == 1:
                             self.record = self.game.score.points
                         self.game = Tetris(WIDTH, HEIGHT, self.level,
                                            self.mode == 1, None)
                     else:
                         self.paused = not self.paused
         pressed = pygame.key.get_pressed()
         if not self.paused:
             if self.game.state == 1:
                 if self.mode == 0:
                     if self.timer.tick() == 0:
                         self.agent.play(self.game.current_tetromino,
                                         self.game.grid)(self.game)
                 elif self.mode == 1:
                     self.interact(pressed)
             self.game.update()
         self.pressed = pressed
         self.render()
         clock.tick(60)
Beispiel #17
0
def test(args, episodes=0):
    if torch.cuda.is_available():
        torch.cuda.manual_seed(0)
    else:
        torch.manual_seed(0)
    device = torch.device(
        'cuda:{}'.format(args.gpu) if torch.cuda.is_available() else 'cpu')
    model = torch.load("{}/{}".format(args.saved_path, args.ckpt_name),
                       map_location=device)
    model.eval()

    random.seed(episodes)
    rom_env = SymbolTetrisSimple(gym.make('Tetris-v0'),
                                 max_episode_length=-1,
                                 align=False)
    sim_env = Tetris(width=args.width,
                     height=args.height,
                     block_size=args.block_size,
                     shuffle=False)
    out = None
    if args.gui_render:
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter(
            "{}/{}".format(args.saved_path, args.out_video), fourcc, args.fps,
            (args.width * args.block_size, args.height * args.block_size))
    _, _, rom_done, _, _ = rom_env.reset(out)
    sim_env.reset()
    sim_env.update_board(rom_env._get_board_ram().astype(int))
    sim_env.set_new_piece(rom2sim_id(rom_env.game_state.fallingPiece))
    step_counter = 0
    while True:
        if step_counter % 100 == 0:
            print("PyGame Counter: %d Lines: %d" %
                  (step_counter, sim_env.cleared_lines))
        next_steps = sim_env.get_next_states()
        next_actions, next_states = zip(*next_steps.items())
        next_states = torch.stack(next_states).to(device)
        predictions = model(next_states)[:, 0]
        index = torch.argmax(predictions).item()
        action = next_actions[index]

        _, done = sim_env.step(action, render=False)
        x, num_rotations = action
        # Note: current translation isn't optimal
        _, _, rom_done, _, _ = rom_env.step(num_rotations * 10 + x, video=out)
        done = rom_done or done
        if rom_env.game_state.fallingPiece is not None and not done:
            sim_env.update_board(rom_env._get_board_ram().astype(int))
            sim_env.set_new_piece(rom2sim_id(rom_env.game_state.fallingPiece))

        step_counter += 1
        if done:
            if args.gui_render:
                out.release()
            break

    print("EpisodeID: %d\tSteps: %d\tLines: %d" %
          (episodes, step_counter, sim_env.cleared_lines))
    return sim_env.cleared_lines
Beispiel #18
0
def materializeNewTetrominoAfterPlace():
    tetris = Tetris(10, 10)
    tetris.state = Tetris.State.PLACE
    tetris.tetro = Tetromino(0, 5, Tetris.TetrominoKind.O)
    tetris.place()
    assert (tetris.state == Tetris.State.MATERIALIZE)
    tetris.materialize()
    assert (tetris.tetro.row == 0)
Beispiel #19
0
def main():
    game = Tetris(width=10, height=12)

    while not game.isFinished:
        # One game update
        game.update()
        print(game)
        time.sleep(0.2)
Beispiel #20
0
def placeAfterFallingEnough():
    tetris = Tetris()
    tetris.state = Tetris.State.FALLING
    tetris.tetro = Tetromino(0, 0, Tetris.TetrominoKind.Z)
    tetris.moveDown(tetris.height)
    assert (tetris.state == Tetris.State.FALLING)
    tetris.moveDown(1)
    assert (tetris.state == Tetris.State.PLACE)
Beispiel #21
0
    def __init__(self, d):
        display.App.__init__(self, d)

        self.menu_items = [('Clock', Clock(d)), ('Tetris', Tetris(d)),
                           ('Snake', Snake(d)), ('Off', Blank(d))]

        self.selected = 0

        self.font = pygame.font.Font("fonts/5x7.pcf", 7)
Beispiel #22
0
 def get_fitness(self, chromosome, seed):
     """Return the fitness of a given chromosome."""
     agent = Agent(chromosome.chromosome)
     game = Tetris(WIDTH, HEIGHT, 0, 0, seed)  # simulate a game of Tetris
     while game.state != 0:
         agent.play(game.current_tetromino, game.grid)(game)
         game.update()
     chromosome.fitness = game.score.score
     print(f'Fitness: {chromosome.fitness}')
     return chromosome
Beispiel #23
0
def start_game():
    global game

    game = Tetris()
    game.start()

    global action_report
    action_report = game.move_piece('up')

    return json.dumps(action_report.state)
Beispiel #24
0
    def generate_random_games(self, num=1):
        """Generates a completely new set of Tetris instanes and AIs with randomized weights."""

        self.tetris_instances.clear()
        self.tetris_ais.clear()
        for i in range(num):
            self.tetris_instances.append(
                Tetris(self.grid_width, self.grid_height, self.cell_width))
            self.tetris_ais.append(
                TetrisAI(self.grid_width, self.grid_height, [], [], []))
Beispiel #25
0
def playTetris(screen):
    graphics = TetrisGraphicsCurses(screen)
    controls = TetrisControlsCurses(screen)
    tetris = Tetris()
    while (tetris.state != Tetris.State.END):
        action = controls.readTetrisAction(tetris.state)
        actionOutput = tetris.action(action)
        tetrisOutput = tetris.update()
        for out in actionOutput, tetrisOutput:
            graphics.update(out)
Beispiel #26
0
	def __init__(self):
		self.game = Tetris()
		self.led_display = LED_Module()
		self.led = MAX7219()
		self.to_show_led = ""
		self.showed_led = ""
		self.to_show_matrix = []
		self.showed_matrix = []
		self.state = ""
		self.change_state("PLAY")
		self.num = 0
Beispiel #27
0
def lineClearIntegrationTest():
    height = 5
    width = 4
    tetris = Tetris(height, width)
    tetris.tetro = Tetromino(height - 1, 0, Tetris.TetrominoKind.I)
    tetris.state = Tetris.State.PLACE

    out = tetris.update()

    assert (out != None and Tetris.Output.LINE_CLEAR in out)
    _, clearedLines = out
    assert (clearedLines == [4])
Beispiel #28
0
def lineClearingLogicIsCorrect():
    tetris = Tetris(5, 4)
    i = Tetris.TetrominoKind.I.value
    initialGrid = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0],
                   [i, i, i, i]]
    expectedGrid = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0],
                    [0, 0, 0, 0]]

    tetris.grid = initialGrid
    assert (tetris.rowIsFull(4))
    tetris.clearLine(4)
    assert (tetris.grid == expectedGrid)
Beispiel #29
0
def fallActuallyFalls():
    tetris = Tetris(20, 10)
    tetris.state == Tetris.State.FALLING
    tetro0 = Tetromino(0, 0, Tetris.TetrominoKind.O)
    tetris.tetro = copy.copy(tetro0)
    tetro1 = tetris.tetro

    tetris.moveDown(1)

    assert (tetro0.tetroKind == tetro1.tetroKind)
    assert (tetro0.col == tetro1.col)
    assert (tetro0.row + 1 == tetro1.row)
Beispiel #30
0
def main():
    running = True
    objFalling = False
    currTime = pygame.time.get_ticks()
    tetris = Tetris()
    right = False
    left = False
    prevMoveTime = 0
    while running:
        screen.fill(WHITE)
        tBoard = tetris.board
        drawBlocks(tBoard)

        if not objFalling:
            tetris.newObject()
            objFalling = True

        if pygame.time.get_ticks() - currTime >= 500:
            currTime = pygame.time.get_ticks()
            if not tetris.hitBottom():
                tetris.moveObject()
            else:
                tetris.newObject()

        if right and pygame.time.get_ticks() - prevMoveTime >= 75:
            tetris.moveRight()
            prevMoveTime = pygame.time.get_ticks()
        elif left and pygame.time.get_ticks() - prevMoveTime >= 75:
            tetris.moveLeft()
            prevMoveTime = pygame.time.get_ticks()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    right = True
                    prevMoveTime = pygame.time.get_ticks()
                if event.key == pygame.K_LEFT:
                    left = True
                    prevMoveTime = pygame.time.get_ticks()
                if event.key == pygame.K_UP:
                    tetris.rotate()
                if event.key == pygame.K_DOWN:
                    tetris.slamDown()
            if event.type == pygame.KEYUP:
                right = False
                left = False

        tetris.checkRowDone()
        clock.tick(60)
        pygame.display.update()