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.