Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在战舰python中编写狩猎阶段代码_Python_Computer Science - Fatal编程技术网

在战舰python中编写狩猎阶段代码

在战舰python中编写狩猎阶段代码,python,computer-science,Python,Computer Science,大家好,我正在战舰中编写一个狩猎阶段(人工智能执行棋盘格模式)。在此之后,我必须做一个目标阶段。但我的狩猎阶段的问题是,当连续两次命中时,游戏只是在一个无限循环中运行,我不确定我哪里出错了。这是我目前掌握的电脑移动代码: def compMove(game): moveLigit = False pick = True while (not moveLigit): while game.previousTurn == '#' and pick == True: if game.two

大家好,我正在战舰中编写一个狩猎阶段(人工智能执行棋盘格模式)。在此之后,我必须做一个目标阶段。但我的狩猎阶段的问题是,当连续两次命中时,游戏只是在一个无限循环中运行,我不确定我哪里出错了。这是我目前掌握的电脑移动代码:

def compMove(game):
  moveLigit = False
pick = True
while (not moveLigit):
  while game.previousTurn == '#'
and pick == True:
  if game.twohits == 'no':
  for i in range(10):
  for j in range(10):
  if game.userBoard[i][j] == '#':
  c = random.randint(0, 1)
if c == 0:
  x = random.randint(i - 1, i + 1)
y = j
game.twohits = 'yes'
pick = False
else :
  x = i
y = random.randint(j - 1, j + 1)
game.twohits = 'sure'
pick = False
if game.twohits == 'yes':
  for i in range(10):
  for j in range(10):
  if game.userBoard[i][j] == '#':
  x = random.randint(i - 1, i + 1)
y = j
pick = False
else :
  for i in range(10):
  for j in range(10):
  if game.userBoard[i][j] == '#':
  x = i
y = random.randint(j - 1, j + 1)
pick = False
pick = False



if game.previousTurn != '#':
  x = random.choice([1, 3, 5, 7, 9]) - 1# Make a checkerboard pattern of random selection
y = random.choice([2, 4, 6, 8, 10]) - 1

beforeDropped = game.makeA_Move(True, x, y)# displaying current boards
game.drawBoards(True)
labels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
if beforeDropped == "*"
or beforeDropped == "#":
  moveLigit = False
elif beforeDropped == " ":
  print("Sorry computer, " + labels[x] + " " + str(y + 1) + " is a miss.")
game.twohits = 'no'
moveLigit = True

else :
  print("Computer did a Hit at " + labels[x] + " " + str(y + 1))
if game.checkIfSunk(True, beforeDropped):
  print(whatShip(beforeDropped) + " sunk")
game.twohits = 'no'
game.previousTurn = ' '

moveLigit = True


return game.checkWinning(True)

在此方面的任何帮助都将不胜感激。提前感谢。

我不知道如何在stackoverflow中进行tab更正,但moveLegit的tab需要重新设置,并且所有这些都正确地标记到了我程序中的函数中。我发誓,为所有代码设置一个巨大的函数是一个可怕的想法,虽然刚开始时很诱人,但这会让阅读和调试变得非常混乱。在这段代码中,我发现有很多地方可能需要一些修正,但是如果没有一个最小的、可验证的示例(),这是非常困难的。我只是在一般情况下搞不清楚如何正确地创建棋盘格模式,而不在这里使用所有这些错误的废话“无限循环”会向我表明你在使用循环时使用了太多的
,请检查
以获得更深入的循环。不要使用选项卡。使用配置为使用空格的编辑器(如记事本+),并将任何选项卡转换为空格。正确缩进并解决这个问题。这是不可读的。