Question

So I've written a tetris bot in python, and it uses the pygame events to respond to keyboard input. Now I'm trying to create an AI to play this game. So I want to basically determine what the best move is, given a piece, and a board. I want to iterate over every possible move, and evaluate the board state (I have a method to evaluate how good a given board is), and pick the move that creates the best board state. My current main method is below.

def main():
    pygame.init()
    pygame.mixer.music.load("music.ogg")
    #pygame.mixer.music.play(-1)
    pygame.key.set_repeat(200,100)
    game_state = state.GameState()

    while True:
        game_state.apply_gravity()
        game_state.check_collision(place_if_collision=True)
        game_state.time += 1
        if game_state.hardDrop:
            continue
        game_state.check_for_full_rows()
        game_state.print_state()
        pygame.display.flip()
        for event in pygame.event.get():
            if event.type == QUIT:
                terminate()
            elif event.type==KEYDOWN:
                if event.key==K_ESCAPE:
                    terminate()
                if event.key==K_LEFT:
                    game_state.save_state()
                    game_state.piece_x -= 1
                    game_state.check_collision()
                if event.key==K_RIGHT:
                    game_state.save_state()
                    game_state.piece_x += 1
                    game_state.check_collision() 
                if event.key==K_DOWN:
                    game_state.save_state()
                    game_state.piece_y += 1
                    game_state.check_collision(place_if_collision=True)
                if event.key==K_UP:
                    game_state.save_state()
                    game_state.curr_piece.rotate_cw()
                    game_state.check_collision()
                    game_state.print_state()
                if event.key==K_SPACE:
                    game_state.hardDrop = True

How do I figure out what a state would look like without actually modifying the state/rewriting a bunch of code? I can provide more code as needed. The purpose of this is so that I may use a genetic algorithm to train a neural network, to play tetris.

Was it helpful?

Solution

Very interesting and unique problem, couldn't you just create an independent copy and run your tests on that copy and delete it once you are done.

from copy import deepcopy

#some other code...

temp_state = deepcopy(original_state)

You then run your tests on temp_state and once you are done using it: del temp_state

As for your second problem, you could make the bot analyze a piece's placement once it have reached 2 blocks down or whatever to solve for your problem. Or, you could have an unseeable few extra lines at the top (beyond the screen) that the player cannot see but the bot can use for making decisions.

Furthermore, and I'm sure you have already done this, you can use itertools to create the list of strings such as lllllus,llllluus (quoting your comment). In specific, try itertools.product and itertools.combinations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top