我正在努力改进战舰游戏。原始版本运行良好,没有错误。我编写了代码来帮助克服第一个版本每次都将船放在同一个地方的事实,所以我从一艘船(由两个正方形组成)开始。我通过创建两个函数来做到这一点:第一个生成一个随机坐标......
# Destroyer (2 squares)
def Deploy_Destroyer_1(Player):
rand_col_1 = randint(0,11)
if rand_col_1 <= 5:
rand_row_1 = randint(0,11)
else:
rand_row_1 = randint(6,11)
return rand_col_1
return rand_row_1
if Player[rand_row_1][rand_col_1] == 'X':
Deploy_Destroyer_1(Player)
else:
Deploy_Destroyer_2(Player)
第二次试验这个坐标与条件相匹配(如果它适合板上以及可以放置哪个旋转)。
def Deploy_Destroyer_2(Player):
if rand_col_1 == 5 and rand_row_1 == 6:
#can be 1, 2, 3 or 4... in that order below
rand_position_1 = randint(1,4)
if rand_position_1 == 1:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 + 1][rand_col_1] = 2
if rand_position_1 == 2:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 - 1][rand_col_1] = 2
if rand_position_1 == 3:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 + 1] = 2
if rand_position_1 == 4:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 - 1] = 2
elif rand_col_1 in range(1,4) and rand_row_1 in range(1,10):
#can be any 1, 2, 3 or 4... in that order below
rand_position_1 = randint(1,4)
if rand_position_1 == 1:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 + 1][rand_col_1] = 2
if rand_position_1 == 2:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 - 1][rand_col_1] = 2
if rand_position_1 == 3:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 + 1] = 2
if rand_position_1 == 4:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 - 1] = 2
elif rand_col_1 in range(5,10) and rand_row_1 in range(7,10):
#can be any 1, 2, 3 or 4... in that order below
rand_position_1 = randint(1,4)
if rand_position_1 == 1:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 + 1][rand_col_1] = 2
if rand_position_1 == 2:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 - 1][rand_col_1] = 2
if rand_position_1 == 3:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 + 1] = 2
if rand_position_1 == 4:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 - 1] = 2
elif rand_col_1 == 0 and rand_row_1 == 0:
#can be any 1, 2, 3 or 4... in that order below
rand_position_1 = randint(1,4)
if rand_position_1 == 1:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 + 1][rand_col_1] = 2
if rand_position_1 == 2:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 - 1][rand_col_1] = 2
if rand_position_1 == 3:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 + 1] = 2
if rand_position_1 == 4:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 - 1] = 2
elif (rand_col_1 == 5 and rand_row_1 == 0) or (rand_col_1 == 11 and rand_row_1 ==6):
#can be one or four
#check brackets and booleans here
rand_position_1 = randint(1,2)
if rand_position_1 == 1: #position 1
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 + 1][rand_col_1] = 2
if rand_position_1 == 2: #position 4
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 - 1] = 2
elif rand_col_1 == 0 and rand_row_1 == 11:
#can be 2 or 3
rand_position_1 = randint(2,3)
if rand_position_1 == 2: #position 2
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 - 1][rand_col_1] = 2
if rand_position_1 == 3: #position 3
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 + 1] = 2
elif rand_col_1 == 11 and rand_row_1 == 11:
#can be 2 or 4
rand_position_1 = randint(1,2)
if rand_position_1 == 1: #position 2
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 - 1][rand_col_1] = 2
if rand_position_1 == 2: #position 4
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 - 1] = 2
elif (rand_row_1 == 0 and rand_col_1 in range(1,4)) or (rand_row_1 == 6 and rand_col_1 in range(6,10)):
#can be 1, 3 or 4
#check brackets and booleans here
rand_position_1 = randint(1,3)
if rand_position_1 == 1: #position 1
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 + 1][rand_col_1] = 2
if rand_position_1 == 2: #position 3
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 + 1] = 2
if rand_position_1 == 3: #position 4
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 - 1] = 2
elif (rand_col_1 == 5 and rand_row_1 in range(1,5)) or (rand_col_1 == 11 and rand_row_1 in range(7,10)):
#can be 1, 2 or 4
#check brackets and booleans here
rand_position_1 = randint(1,3)
if rand_position_1 == 1: #position 1
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 + 1][rand_col_1] = 2
if rand_position_1 == 2: #position 2
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 - 1][rand_col_1] = 2
if rand_position_1 == 3: #position 4
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 - 1] = 2
elif rand_col_1 == 0 and rand_row_1 in range(1,10):
#can be 1, 2 or 3... in that order below
rand_position_1 = randint(1,3)
if rand_position_1 == 1:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 + 1][rand_col_1] = 2
if rand_position_1 == 2:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 - 1][rand_col_1] = 2
if rand_position_1 == 3:
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 + 1] = 2
elif rand_col_1 in range(1,10) and rand_row_1 == 11:
#can be 2, 3 or 4
rand_position_1 = randint(1,3)
if rand_position_1 == 2: #position 2
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1 - 1][rand_col_1] = 2
if rand_position_1 == 3: #position 3
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 + 1] = 2
if rand_position_1 == 4: #position 4
Player[rand_row_1][rand_col_1] = 2
Player[rand_row_1][rand_col_1 - 1] = 2
应用我的代码后,我收到此错误。
Traceback (most recent call last):
File "<stdin>", line 310, in <module>
File "<stdin>", line 15, in PrintBoards
TypeError: sequence item 0: expected string, NoneType found
这是PrintBoards
功能
def PrintBoards(Player,Opponent):
print ' '*10, 'PLAYER', ' '*30, 'OPPONENT'
letters = ['A','B','C','D','E','F','G','H','I','J','K','L']
for x in range(6):
print letters[x]," ".join(map(DisplayChar,Player[x]))," "*18,"| "," ".join(map(DisplayChar,Opponent[x]))
for x in range(6,12):
print letters[x]," ".join(map(DisplayChar,Player[x]))," | "," ".join(map(DisplayChar,Opponent[x]))
print " "," ".join(map(str,range(1,10)))," 10 11 12"," "," ".join(map(str,range(1,10)))," 10 11 12"
这是DisplayChar
功能
def DisplayChar(x):
if x==0:
return '?'
elif x==1:
return ' '
elif x==2:
return 'X'
elif x==3:
return ' '
elif x==4:
return '*'
我尝试将上述功能编辑为此...
def DisplayChar(x):
if x==0:
return '?'
elif x==2:
return 'X'
elif x==4:
return '*'
else:
return ' '
但是它给了我这个错误
Traceback (most recent call last):
File "<stdin>", line 309, in <module>
File "<stdin>", line 15, in PrintBoards
TypeError: argument 2 to map() must support iteration
我还尝试在函数之后打印列表 Player 和 OpponentPrintBoards
以确保它们包含DisplayChar
它们所做的 0 和 1(指函数)(插入原始代码时,而不是当我放入新的非常长的代码时)
下一位是对迈克尔的回应
PLAYER OPPONENT
[[1, 1, 1, 1, 1, 1], [1, 2, 2, 2, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1], [1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 1, 1], [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
[0, 0, 0, 0, 0, 0]
A | ? ? ? ? ? ?
<function Deploy_Destroyer_1 at 0x1c2634>
[0, 0, 0, 0, 0, 0]
B
Traceback (most recent call last):
File "<stdin>", line 314, in <module>
File "<stdin>", line 17, in PrintBoards
TypeError: argument 2 to map() must support iteration
在有人好心指出我分配了函数而不是调用它之后,我发现发生了另一个错误(我认为 Python 不喜欢我)
Traceback (most recent call last):
File "<stdin>", line 313, in <module>
File "<stdin>", line 17, in PrintBoards
TypeError: argument 2 to map() must support iteration
下面我还包括了我调用函数的地方,以防我做了一些愚蠢的事情
Player, Opponent = InitBoards()
Player = DeployFleet(Player), Deploy_Destroyer_1(Player)
PrintBoards(Player,Opponent)
我把它改成了 Micheal0x2a 所说的,它运行没有错误,但是代码放置的船消失了
据我了解,该函数通过将列表中的项目映射到PrintBoards
函数来打印板(如果 2 是列表中的项目,它会打印 X 等)。所以我的新手知识告诉我应该在函数中调用该函数(包含在上面)以确保列表中的项目发生更改,因此打印的字符应该更改。Player
DisplayChar
Deploy_Destroyer_1
Player =
Main
我猜我的新代码Deploy_Destroyer_1
(考虑到)。
然而,我也有很大的机会把自己弄糊涂了
我只学了几个星期的 Python,所以如果有人需要更多细节来帮助我,请询问