Ejemplo n.º 1
0
def play_game():
    mat_init = add_two(
        add_two([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]))
    # mat_init = [[8, 4, 2, 8], [2, 16, 8, 4], [256, 32, 4, 2], [4, 2, 4, 2]]    # Death matrix for debugging
    # mat_init = [[2,4,32,2],[4,16,512,4],[4,8,128,16],[4,16,8,2]]               # Death matrix for debugging

    state = GameState(mat_init)  # Initialize 2048 grid
    loop_count = 0
    game_over = False  # True if add_two can't add anywhere after moving

    while state.game_state(
    ) == 'not over' and game_over == False:  # Run if the game is not over
        loop_count += 1

        print("Move count: " + str(loop_count))
        print("Points    : " + str(state.point_count))
        print(str(state))

        move = UCT(root_state=state,
                   n_search_path=50,
                   n_search_depth=5,
                   exploration_const=100,
                   verbose=True)

        print("Best Move: " + str(move) + "\n")
        game_over = state.do_move(move)

    print("======================= Game Over =======================")
    print(str(state))
    print(state.game_state())
Ejemplo n.º 2
0
def main():
    running = True
    win = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
    clock = pygame.time.Clock()
    game_state = GameState()
    blocks = Blocks()
    current_block = blocks.get_random_block()
    next_block = blocks.get_random_block()
    score = 0

    while running:
        clock.tick(150)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            # Movement
            keys_pressed = pygame.key.get_pressed()
            if getPressed(keys_pressed, pygame.K_LEFT,
                          50) and not current_block.dropped:
                current_block.move_left(game_state)
            if getPressed(keys_pressed, pygame.K_RIGHT,
                          50) and not current_block.dropped:
                current_block.move_right(game_state)
            if getPressed(keys_pressed, pygame.K_DOWN,
                          50) and not current_block.dropped:
                current_block.dropped = True
            if getPressed(keys_pressed, pygame.K_UP,
                          50) and not current_block.dropped:
                current_block.rotate_block(game_state)

        # Next Block
        if not next_block:
            next_block = blocks.get_random_block()

        game_state.draw_window(win, current_block, score, next_block)

        # Move Block
        if not current_block.move_down(game_state):
            game_state.game_state = game_state.set_current_block_to_gamestate(
                current_block)
            current_block = next_block
            next_block = None
            score += 1

        score = game_state.check_tetris(score)

        if game_state.check_fail():
            pygame.quit()
            quit()