将流程图转换为python代码(战舰专用AI)

时间:2019-03-17 13:19:17

标签: python artificial-intelligence

我想为战舰游戏创建AI。作为一个初学者,我在将流程图转换为Python代码时遇到了一些麻烦。我该如何开始?

这是我的流程图: flowchart

1 个答案:

答案 0 :(得分:0)

这不是我会做的方式,但是考虑到您是一个初学者,我认为这是您在看书时可以理解的东西:

from random import randrange


def next_shot(shot, touch):
    if shot == touch:
        print("HIT!")
    elif shot == (touch + 1):
        print("HIT (+1)")
        return True
    elif shot == (touch - 1):
        print("HIT (- 1)")
        return True
    elif shot == (touch + 10):
        print("HIT (+ 10)")
        return True
    # You can add more else-if statements to declare other conditions
    elif shot == (touch - 10):
        print("HIT (- 10)")
        return True
    # If nothing hits, we return False
    return False


# Start of your script
game_status = 'running'

while game_status == 'running':
    shot = randrange(0, 99)
    touch = randrange(0, 99)
    print(f"Current shot is: {shot}\nCurrent touch is: {touch}")

    if next_shot(shot, touch):
        game_status = "not running"
    else:
        print("No hit, starting over")

示例输出:

Current shot is: 51
Current touch is: 33
No hit, starting over
Current shot is: 0
Current touch is: 81
No hit, starting over
Current shot is: 84
Current touch is: 9
No hit, starting over
Current shot is: 29
Current touch is: 56
No hit, starting over
Current shot is: 91
Current touch is: 29
No hit, starting over
Current shot is: 92
Current touch is: 37
No hit, starting over
Current shot is: 71
Current touch is: 61
HIT (- 10)