B-ship游戏:X&O和#O刚刚搞砸了。 (蟒蛇)

时间:2015-02-14 04:37:57

标签: python python-3.x

我正在制作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纸上时,请键入“已完成”。然后你会猜到计算机的船只。

我放入2,它显示为X,这很好。放入3擦除前一个X并将X放入3,所以仍然只有1 X.这不好。然而,当我把它放在4,5,6,7,8或9中时,它会停留,但有些是X(不好),有些是O(好)。 X =命中O =未命中

非常感谢帮助!

1 个答案:

答案 0 :(得分:0)

一个错误(可能还有其他错误)宣告:

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 = aiships(hitboard)