B 船游戏跟进:如何让计算机随机化他们的船?
B-ship game follow up: How to have the computer randomnize their ships?
从跟进:
我现在想随机化 AI 船的位置。另外,我们可以尝试做两条船吗?
如果你不想点击上面的代码,这里是代码link:
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()
非常感谢帮助!
现在您正在对支票进行硬编码(在三个具有不同名称的相同函数中 - 一种神秘的方法!)如:
if spot_hit == 1 or spot_hit == 2 or spot_hit == 3:
这需要变成类似
的东西
if spot_hit in ship_spots:
其中 ship_spots
是一个全局变量,您在开始时将其随机设置为船舶可能的一组位置之一。
我不知道你想选择哪一组位置(从来没有在 3 x 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]]
将允许三个垂直放置以及三个水平放置。
从
我现在想随机化 AI 船的位置。另外,我们可以尝试做两条船吗?
如果你不想点击上面的代码,这里是代码link:
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()
非常感谢帮助!
现在您正在对支票进行硬编码(在三个具有不同名称的相同函数中 - 一种神秘的方法!)如:
if spot_hit == 1 or spot_hit == 2 or spot_hit == 3:
这需要变成类似
的东西if spot_hit in ship_spots:
其中 ship_spots
是一个全局变量,您在开始时将其随机设置为船舶可能的一组位置之一。
我不知道你想选择哪一组位置(从来没有在 3 x 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]]
将允许三个垂直放置以及三个水平放置。