来自this
我一直在进行b-ship游戏,但我遇到了一个问题。它是3x3,所以它是实际游戏的缩小版。我们现在正在使用一艘船。
我目前已经让用户完成了AI部分,但是如何在一次击中后切换到Ai的转身?
当轮到他时,人工智能知道用户在[4]中拥有一艘船的一部分,并且现在将保持这种状态。然后他试图击中[7],但如果那不起作用,试试[1],或者如果那不起作用,试试[5]。我怎么能为任何边缘做到这一点??
import random
def drawboard(hitboard,hitboard2):
print(' Opponent\'s Your')
print(' Ships Ships')
print('| | | | | | | |')
print('| ' + hitboard[7] + ' | ' + hitboard[8] + ' | ' + hitboard[9] + ' | | ' + hitboard2[7] + ' | ' + hitboard2[8] + ' | ' + hitboard2[9] + ' |')
print('| | | | | | | |')
print('------------- -------------')
print('| | | | | | | |')
print('| ' + hitboard[4] + ' | ' + hitboard[5] + ' | ' + hitboard[6] + ' | | ' + hitboard2[4] + ' | ' + hitboard2[5] + ' | ' + hitboard2[6] + ' |')
print('| | | | | | | |')
print('------------- -------------')
print('| | | | | | | |')
print('| ' + hitboard[1] + ' | ' + hitboard[2] + ' | ' + hitboard[3] + ' | | ' + hitboard2[1] + ' | ' + hitboard2[2] + ' | ' + hitboard2[4] + ' |')
print('| | | | | | | |')
def aiships(hitboard,spot_hit,shipspots,hitboard2):
if spot_hit in shipspots:
hitboard[1] = 'x'
else:
hitboard[7] = 'o'
drawboard(hitboard,hitboard2)
def playerships(hitboard,hitboard2, spot_hit, usershipspots):
hitboard2[7] = 'x'
print("\nComputer's turn.\n")
spot_hit = random.choice(usershipspots)
hitboard2[spot_hit] = 'x'
if spot_hit not in usershipspots:
hitboard2[spot_hit] = 'o'
drawboard(hitboard,hitboard2)
def main():
possiblespots = [[1,2],[2,3],[4,5],[5,6],[7,8],[8,9],[1,4],[4,7],[2,5],[5,8],[3,6],[6,9]]
shipspots = random.choice(possiblespots)
userspots = [[4,7],[4,1],[4,5]]
usershipspots = random.choice(userspots)
gameisplaying = True
ai_spots = [4, 7, 1, 5]
ai_index = 0
while gameisplaying:
hitboard = [' ' for i in range(10)]
hitboard2 = [' ' for i in range(10)]
hitboard2[usershipspots[0]] = 's'
hitboard2[usershipspots[1]] = 's'
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. ')
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)
aiships(hitboard, spot_hit, shipspots, hitboard2)
playerships(hitboard, hitboard2, ai_spots[ai_index], shipspots)
ai_index += 1
main()
但等等,还有更多! (billy mays参考)
无论我输入什么号码,7空间都会有一个O.除非我输入球队的坐标(这很奇怪),这将在1个空间中创造一个X.总会有一个'在你的船只的3个空间里#39;板。 (使用数字小键盘)
答案 0 :(得分:1)
所以在两个当前的陈述之间
aiships(hitboard,spot_hit,shipspots,hitboard2)
playerships(hitboard, hitboard2,spot_hit,shipspots)
你需要重新计算spot_hit
所以它是第一次的4,然后是7,然后是1,然后是5(我们会担心其他未来的“任何对冲”Q,好吗? - )。
为此目的,在while
:
ai_spots = [4, 7, 1, 5]
ai_index = 0
然后将这两个语句转换为:
aiships(hitboard, spot_hit, shipspots, hitboard2)
playerships(hitboard, hitboard2, ai_spots[ai_index], shipspots)
ai_index += 1
我希望它清楚如何在这里工作。顺便说一句,两个函数中随机不同的参数顺序令人困惑,没有任何好处 - 重新排序事物,以便它们在两种情况下都是相同的!
对于“所有对冲”,大概需要ai_spots
更长的列表,并且如果尝试不成功,则需要将ai_index
增加一个以上 - 这反过来需要{{1给你一个布尔返回值,告诉你尝试是否成功,这样你就可以用它来确定改变playerships
的数量。
然而,这还为时尚早,因为你现在还有许多更大的错误需要考虑。例如,请考虑代码段:
ai_index
spot_hit = random.choice(usershipspots)
if spot_hit in usershipspots:
总是返回其参数中的一个项目 - 因此检查其返回值是否确实是其中一个项目是完全多余的 - 它将始终为{ {1}} random.choice
子句的主体将始终执行。
毫无疑问,您希望删除True
作为随机选择的重新计算,并接受您传递的参数!
if
当然可以保留,在函数结束时你可以spot_hit
,这正是布尔告诉你命中是否成功。