所以我用 python 编写了一个俄罗斯方 block 机器人,它使用 pygame 事件来响应键盘输入。现在我正在尝试创建一个人工智能来玩这个游戏。所以我想基本上确定给定棋子和棋盘的最佳走法是什么。我想迭代每一个可能的棋步,并评估棋盘状态(我有一种方法来评估给定棋盘的好坏),并选择创建最佳棋盘状态的棋步。我当前的主要方法如下。
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
如何在不实际修改状态/重写一堆代码的情况下弄清楚状态会是什么样子?我可以根据需要提供更多代码。这样做的目的是为了让我可以用遗传算法来训练神经网络,玩俄罗斯方 block 。
最佳答案
非常有趣且独特的问题,您不能创建一个独立副本并在该副本上运行测试并在完成后将其删除。
从复制导入深复制
#一些其他代码...
temp_state = deepcopy(original_state)
然后,您可以在 temp_state
上运行测试,使用完毕后:
删除临时状态
至于你的第二个问题,你可以让机器人分析一个棋子的位置,一旦它达到了 2 个街区,或者解决你的问题。或者,您可能会在顶部(屏幕之外)有一些看不见的额外线条,玩家看不到,但机器人可以用来做出决定。
此外,我确信您已经完成了此操作,您可以使用 itertools
创建字符串列表,例如 lllllus,llllluus
(引用您的评论)。具体来说,请尝试 itertools.product
和 itertools.combinations
。
关于python - 如何用 Python 创建自动化俄罗斯方 block 机器人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22469361/