B-ship游戏:X和O都搞砸了。(Python)
我正在玩一个游戏,但我无法找到合适的位置 在我再打一次之后,安打就不会停在那里了 到目前为止,这只是用户端 这是我的密码B-ship游戏:X和O都搞砸了。(Python),python,python-3.x,Python,Python 3.x,我正在玩一个游戏,但我无法找到合适的位置 在我再打一次之后,安打就不会停在那里了 到目前为止,这只是用户端 这是我的密码 def drawboard(hitboard): print('| | | |') print('| ' + hitboard[7] + ' | ' + hitboard[8] + ' | ' + hitboard[9] + ' |') print('| | | |') print('-------------')
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纸上时,你输入“完成”,然后你猜计算机的飞船
我输入2,结果显示为X,这很好。加上3会删除之前的X,并在3中加上一个X,所以仍然只有1个X。这不好。然而,当我把它放在4,5,6,7,8或9中时,它保持不变,但有些是X不好的,有些是O好的。X=命中O=未命中
谢谢你的帮助 一个错误可能还有其他错误:
hitboard = [' ']* 10
通过这样做,可以创建10个单元,这些单元都指向同一个位置。当一个人改变时,其他人也会改变
改用:
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在1,2,3上移动一样!我觉得现在还不错!如果还有别的事,我会开一家新的。
x = aiships(hitboard)