[英]Transforming flowchart into python code (AI for battleship)
这不是我想做的方式,但考虑到你是初学者,我认为这是你在看到它时可以理解的东西:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.