我在战舰游戏中遇到了我的船一代问题! 有时,当我运行代码时,我收到错误:
RuntimeError: maximum recursion depth exceeded while calling a Python object
以下是代码,如果可以提供帮助,我会全部给你。 问题出在" pick"功能
print "Welcome to Battleships!!!"
board = []
import os
from random import randint
for x in range(10):
board.append(["O"] * 10)
def print_board(board):
for row in board:
print " | ".join(row)
ships = {"battleship" : [5], "cruiser1" : [4], "cruiser2" : [4], "frigate1" : [3], "frigate2" : [3], "frigate3" : [3], "frigate4" : [3], "minesweeper1" : [2], "minesweeper2" : [2], "minesweeper3" : [2], "minesweeper4" : [2]}
numbers1 = []
numbers2 = []
numbers = []
def setup_nums1():
for count1 in range(6):
for count2 in range(10):
number1 = (count1)*100
number2 = count2
numbers1.append(number1 + number2)
def setup_nums2():
for count1 in range(10):
for count2 in range(6):
number1 = (count1)*100
number2 = count2
numbers2.append(number1 + number2)
def setup_nums():
for count1 in range(10):
for count2 in range(10):
number1 = (count1)*100
number2 = count2
numbers.append(number1 + number2)
def pick1(info, ship):
random = numbers1[randint (0,len(numbers1) - 1)]
if not info[0] + random in numbers:
pick1(info, ship)
elif not info[0] + random - 1 in numbers:
pick1(info, ship)
elif not random + 1 in numbers:
pick1(info, ship)
elif not random + 2 in numbers:
pick1(info, ship)
elif not random in numbers:
pick1(info, ship)
else:
ships[ship].append(random)
y = info[1]
print y
numbers1.remove(y)
if y in numbers2:
numbers2.remove(y)
numbers.remove(y)
for n in range(info[0] - 1):
ships[ship].append(y + n + 1)
if (y + n + 1) in numbers1:
numbers1.remove(y + n + 1)
if (y + n + 1) in numbers2:
numbers2.remove(y + n + 1)
numbers.remove(y + n + 1)
def pick2(info, ship):
random = numbers2[randint (0,len(numbers2) - 1)]
if not info[0]*100 + random in numbers:
pick2(info, ship)
elif not info[0]*100 + random - 100 in numbers:
pick2(info, ship)
elif not random + 100 in numbers:
pick2(info, ship)
elif not random + 200 in numbers:
pick2(info, ship)
elif not random in numbers:
pick2(info, ship)
else:
ships[ship].append(random)
y = info[1]
print y
if y in numbers1:
numbers1.remove(y)
if y in numbers2:
numbers2.remove(y)
numbers.remove(y)
for n in range(info[0] - 1):
ships[ship].append(y + n*100 + 100)
if (y + n*100 + 100) in numbers1:
numbers1.remove(y + n*100 + 100)
if (y + n*100 + 100) in numbers2:
numbers2.remove(y + n*100 + 100)
numbers.remove(y + n*100 + 100)
def setup_numbers():
setup_nums()
setup_nums1()
setup_nums2()
def setup_ships():
for ship, info in ships.items():
direction = randint(1,2)
if direction == 1:
pick1(info, ship)
elif direction == 2:
pick2(info, ship)
setup_numbers()
setup_ships()
print numbers1
print numbers2
print ships
有人可以帮我吗?
答案 0 :(得分:0)
将递归更改为循环。这里有一点改变了pick1功能:
from collections import deque
def need_to_process_now(info, ship):
random = numbers1[randint (0,len(numbers1) - 1)]
if not info[0] + random in numbers:
return (random, False)
elif not info[0] + random - 1 in numbers:
return (random, False)
elif not random + 1 in numbers:
return (random, False)
elif not random + 2 in numbers:
return (random, False)
elif not random in numbers:
return (random, False)
return (random, True)
def pick1(info, ship):
infos = deque()
infos.append((info, ship))
while len(infos) > 0:
curr = infos.pop()
res = need_to_process_now(curr[0], curr[1])
if not res[1]:
infos.append(curr)
else:
ships[ship].append(res[0])
y = curr[0][1]
print y
numbers1.remove(y)
if y in numbers2:
numbers2.remove(y)
numbers.remove(y)
for n in range(info[0] - 1):
ships[ship].append(y + n + 1)
if (y + n + 1) in numbers1:
numbers1.remove(y + n + 1)
if (y + n + 1) in numbers2:
numbers2.remove(y + n + 1)
numbers.remove(y + n + 1)
P.S。 我认为您需要重新格式化代码,不容易阅读您的代码。