2

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.

Nezo
  • 567
  • 4
  • 18
  • 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` `temp_state = deepcopy(original_state)` You then run your tests on `temp_state` and once you are done using it, `del temp_state` – sshashank124 Mar 18 '14 at 08:23
  • That sounds pretty good, but a problem I'm having is figuring out which moves to try. Because i can create a list of strings like lllllus,llllluus, ..., etc. Where each string corresponds to a list of commands but there are problems duch as not being able to rotate an i piece as soon as it spawns, because it would be off the top of the screen, so you would have to wait until it falls two levels. Or how if you time it right you can t spin or slide the piece into a gap from the side. So basically figuring out how to figure out and decode the possible moves or board states after the move. – Nezo Mar 18 '14 at 13:11

1 Answers1

2

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.

sshashank124
  • 31,495
  • 9
  • 67
  • 76