来自this的跟进:
我想现在随机化AI船的位置。另外,我们可以尝试做两艘船吗?
如果你不想点击上面的链接,这是代码:
def drawboard(hitboard):
print('| | | |')
print('| ' + hitboard[7] + ' | ' + hitboard[8] + ' | ' + hitboard[9] + ' |')
print('| | | |')
print('-------------')
print('| | | |')
print('| ' + hitboard[4] + ' | ' + hitboard[5] + ' | ' + hitboard[6] + ' |')
print('| | | |')
print('-------------')
print('| | | |')
print('| ' + hitboard[1] + ' | ' + hitboard[2] + ' | ' + hitboard[3] + ' |')
print('| | | |')
def aicorners(hitboard,spot_hit):
if spot_hit == 1 or spot_hit == 2 or spot_hit == 3:
hitboard[spot_hit] = 'x'
else:
hitboard[spot_hit] = 'o'
print(drawboard(hitboard))
def aiedges(hitboard,spot_hit):
if spot_hit == 1 or spot_hit == 2 or spot_hit == 3:
hitboard[spot_hit] = 'x'
else:
hitboard[spot_hit] = 'o'
print(drawboard(hitboard))
def aimiddle(hitboard,spot_hit):
if spot_hit == 1 or spot_hit == 2 or spot_hit == 3:
hitboard[spot_hit] = 'x'
else:
hitboard[spot_hit] = 'o'
print(drawboard(hitboard))
def hitplayer():
pass
def main():
gameisplaying = True
while gameisplaying:
hitboard = [' ' for i in range(10)]
userready = input('Place your ships. Type done when you finished placing it.')
while not userready == 'done':
userready = input('Type done when you locate your ship. ')
shipissunk = False
while shipissunk == False:
spot_hit = input('Where\'s the hit?: 1-9 ')
while not (spot_hit in '1 2 3 4 5 6 7 8 9'.split()):
spot_hit = input ('Please tell me where the hit is: 1-9 ')
spot_hit = int(spot_hit)
if (spot_hit in [1,3,7,9]):
aicorners(hitboard,spot_hit)
elif (spot_hit in [2,4,6,8]):
aiedges(hitboard,spot_hit)
else:
aimiddle(hitboard,spot_hit)
main()
注意我只有一条船,它的设置位置为[1,2,3]。我想在每次用户播放时更改位置。
以下更新的部分代码:(我应该替换原始代码吗?)
def aicorners(hitboard,spot_hit,ship_spots,ship_spots2):
if spot_hit in ship_spots or spot_hit in ship_spots2:
hitboard[spot_hit] = 'x'
else:
hitboard[spot_hit] = 'o'
print(drawboard(hitboard))
def main():
import random
possible_spots = [[1,2,3], [4,5,6], [7,8,9], [7,4,1], [8,5,2], [9,6,3]]
possible_spots2 = [[1,2],[2,3],[4,5],[5,6],[7,8],[8,9],[1,4],[4,7],[2,5],[5,8],[3,6],[6,9]]
ship_spots = random.choice(possible_spots)
ship_spots2 = random.choice(possible_spots2)
gameisplaying = True
while gameisplaying:
hitboard = [' ' for i in range(10)]
userready = input('Place your ships. Type done when you finished placing it.')
while not userready == 'done':
userready = input('Type done when you locate your ship. ')
shipissunk = False
while shipissunk == False:
spot_hit = input('Where\'s the hit?: 1-9 ')
while not (spot_hit in '1 2 3 4 5 6 7 8 9'.split()):
spot_hit = input ('Please tell me where the hit is: 1-9 ')
spot_hit = int(spot_hit)
aicorners(hitboard,spot_hit,ship_spots,ship_spots2)
main()
非常感谢帮助!
答案 0 :(得分:1)
现在你正在对支票进行硬编码(在三个相同的函数中使用不同的名称 - 一种神秘的方法!):
if spot_hit == 1 or spot_hit == 2 or spot_hit == 3:
这需要变得像
if spot_hit in ship_spots:
其中ship_spots
是一个全局变量,您在开始时随机设置为船舶可能的一组位置。
我不知道你想要选择哪些位置(从未在3乘3的板上玩过战舰! - )但是例如:
import random
possible_spots = [[1,2,3], [4,5,6], [7,8,9]]
ship_spots = random.choice(possible_spots)
会给你三种横向可能性中的一种。
将main
放在gameisplaying = True
import random
之前(possible_spots
应该更优雅地移动到模块的顶部)并且就在那里。
当然,如果船只不需要水平,你可以延长possible_spots = [[1,2,3], [4,5,6], [7,8,9],
[7,4,1], [8,5,2], [9,6,3]]
,例如
{{1}}
将允许三个垂直放置以及三个水平放置。