B 舰游戏:X 和 O 只是搞砸了。 (Python)
B-ship game: X's and O's are just screwing up. (Python)
我正在制作 B-ship 的游戏,但无法计算出位置。
在我调用另一个命中后,命中不会停留在那里。
到目前为止,这只是用户端。
这是我的代码。
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 aiships(hitboard):
hitboard[1], hitboard[2], hitboard[3] = ' ',' ',' '
#One of the AI's ships
def aicorners(hitboard,spot_hit):
if hitboard[spot_hit] == hitboard[1] or hitboard[spot_hit] == hitboard[2] or hitboard[spot_hit] == hitboard[3]:
hitboard[spot_hit] = 'x'
else:
hitboard[spot_hit] = 'o'
print(drawboard(hitboard))
def aiedges(hitboard,spot_hit):
if hitboard[spot_hit] == hitboard[1] or hitboard[spot_hit] == hitboard[2] or hitboard[spot_hit] == hitboard[3]:
hitboard[spot_hit] = 'x'
else:
hitboard[spot_hit] = 'o'
print(drawboard(hitboard))
def aimiddle(hitboard,spot_hit):
if hitboard[spot_hit] == hitboard[1] or hitboard[spot_hit] == hitboard[2] or hitboard[spot_hit] == hitboard[3]:
hitboard[spot_hit] = 'x'
else:
hitboard[spot_hit] = 'o'
print(drawboard(hitboard))
def main():
gameisplaying = True
while gameisplaying:
hitboard = [' ']* 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)
x = aiships(hitboard)
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()
你应该得到你自己的一张纸。当你把你的船放在你的 IRL 纸上时,你输入 'done' 然后你猜计算机的船。
我输入了 2,它显示为 X,很好。放入 3 会擦除之前的 X,并在 3 中放入一个 X,所以仍然只有 1 个 X。这不好。然而,当我把它放在 4、5、6、7、8 或 9 中时,它仍然存在,但有些是 X(不好),有些是 O(好)。 X = 命中 O = 未命中
感谢帮助!
一个错误(也许还有其他错误)是声明:
hitboard = [' ']* 10
通过这样做,您创建了“10 个单元格”,它们都指向同一个地方 " "
。当 "one" 发生变化时 - 所有其他人也会发生变化。
改用:
hitboard = [' ' for i in range(10)]
另一个错误是函数 aiships()
,它混淆了位置 1、2 和 3。只需删除以下行:
def aiships(hitboard):
hitboard[1], hitboard[2], hitboard[3] = ' ',' ',' '
#One of the AI's ships
和:
x = aiships(hitboard)
我正在制作 B-ship 的游戏,但无法计算出位置。
在我调用另一个命中后,命中不会停留在那里。
到目前为止,这只是用户端。
这是我的代码。
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 aiships(hitboard):
hitboard[1], hitboard[2], hitboard[3] = ' ',' ',' '
#One of the AI's ships
def aicorners(hitboard,spot_hit):
if hitboard[spot_hit] == hitboard[1] or hitboard[spot_hit] == hitboard[2] or hitboard[spot_hit] == hitboard[3]:
hitboard[spot_hit] = 'x'
else:
hitboard[spot_hit] = 'o'
print(drawboard(hitboard))
def aiedges(hitboard,spot_hit):
if hitboard[spot_hit] == hitboard[1] or hitboard[spot_hit] == hitboard[2] or hitboard[spot_hit] == hitboard[3]:
hitboard[spot_hit] = 'x'
else:
hitboard[spot_hit] = 'o'
print(drawboard(hitboard))
def aimiddle(hitboard,spot_hit):
if hitboard[spot_hit] == hitboard[1] or hitboard[spot_hit] == hitboard[2] or hitboard[spot_hit] == hitboard[3]:
hitboard[spot_hit] = 'x'
else:
hitboard[spot_hit] = 'o'
print(drawboard(hitboard))
def main():
gameisplaying = True
while gameisplaying:
hitboard = [' ']* 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)
x = aiships(hitboard)
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()
你应该得到你自己的一张纸。当你把你的船放在你的 IRL 纸上时,你输入 'done' 然后你猜计算机的船。
我输入了 2,它显示为 X,很好。放入 3 会擦除之前的 X,并在 3 中放入一个 X,所以仍然只有 1 个 X。这不好。然而,当我把它放在 4、5、6、7、8 或 9 中时,它仍然存在,但有些是 X(不好),有些是 O(好)。 X = 命中 O = 未命中
感谢帮助!
一个错误(也许还有其他错误)是声明:
hitboard = [' ']* 10
通过这样做,您创建了“10 个单元格”,它们都指向同一个地方 " "
。当 "one" 发生变化时 - 所有其他人也会发生变化。
改用:
hitboard = [' ' for i in range(10)]
另一个错误是函数 aiships()
,它混淆了位置 1、2 和 3。只需删除以下行:
def aiships(hitboard):
hitboard[1], hitboard[2], hitboard[3] = ' ',' ',' '
#One of the AI's ships
和:
x = aiships(hitboard)