为什么它会问你两次你想把战列舰放在哪里? 我不知道它做了什么。
无论如何,在这个链接中你可以看到完整的代码,因为我不知道是否有必要。 http://speedy.sh/QYJWp/battleship-goed.txt
我认为问题发生在//_________________________________________________________//
部分
board1 = []
board2 = []
for x in range(10):
board1.append(["O"] * 10)
for x in range(10):
board2.append(["O"] * 10)
def print_board1(board):
for row in board:
print " ".join(row)
def print_board2(board):
for row in board:
print " ".join(row)
print "Board User 1"
print_board1(board1)
print "----------------------------------------------"
print "Board User 2"
print_board2(board2)
print "Let's play Battleship!"
print "Try to destroy all your opponents battleship!"
print"Good luck!"
print " "
print " "
def U1_Input_row1(board1):
x = float(raw_input("User 1, in what row do you want to place your first ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U1_Input_row1(board1)
def U1_Input_col1(board1):
x = float(raw_input("User 1, in what col do you want to place your first ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U1_Input_col1(board1)
ship1 = [U1_Input_row1(board1), U1_Input_col1(board1)]
def U1_Input_row2(board1):
x = float(raw_input("User 1, in what row do you want to place your second ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U1_Input_row2(board1)
def U1_Input_col2(board1):
x = float(raw_input("User 1, in what col do you want to place your second ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U1_Input_col2(board1)
ship2 = [U1_Input_row2(board1), U1_Input_col2(board1)]
def U1_Input_row3(board1):
x = float(raw_input("User 1, in what row do you want to place your third ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U1_Input_row3(board1)
def U1_Input_col3(board1):
x = float(raw_input("User 1, in what col do you want to place your third ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U1_Input_col3(board1)
ship3 = [U1_Input_row3(board1), U1_Input_col3(board1)]
def U1_Input_row4(board1):
x = float(raw_input("User 1, in what row do you want to place your fourth ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U1_Input_row4(board1)
def U1_Input_col4(board1):
x = float(raw_input("User 1, in what col do you want to place your fourth ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U1_Input_col4(board1)
ship4 = [U1_Input_row4(board1), U1_Input_col4(board1)]
if ship1 == ship2 or ship1 == ship3 or ship1 == ship4 or ship2 == ship3 or ship2 == ship4 or ship3 == ship4:
print "YOU CANT PLACE 2 SHIPS IN THE SAME SPOT"
U1_Input_row1(board1)
U1_Input_col1(board1)
U1_Input_row2(board1)
U1_Input_col2(board1)
U1_Input_row3(board1)
U1_Input_col3(board1)
U1_Input_row4(board1)
U1_Input_col4(board1)
def U2_Input_row1(board2):
x = float(raw_input("User 2, in what row do you want to place your first ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U2_Input_row1(board2)
def U2_Input_col1(board2):
x = float(raw_input("User 2, in what col do you want to place your first ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U2_Input_col1(board2)
ship1u2 = [U2_Input_row1(board1), U2_Input_col1(board1)]
def U2_Input_row2(board2):
x = float(raw_input("User 2, in what row do you want to place your second ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U2_Input_row2(board2)
def U2_Input_col2(board2):
x = float(raw_input("User 2, in what col do you want to place your second ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U2_Input_col2(board2)
ship2u2 = [U2_Input_row2(board1), U2_Input_col2(board1)]
def U2_Input_row3(board2):
x = float(raw_input("User 2, in what row do you want to place your third ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U2_Input_row3(board2)
def U2_Input_col3(board2):
x = float(raw_input("User 2, in what col do you want to place your third ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U2_Input_col3(board2)
ship3u2 = [U2_Input_row3(board1), U2_Input_col3(board1)]
def U2_Input_row4(board2):
x = float(raw_input("User 2, in what row do you want to place your fourth ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U2_Input_row4(board2)
def U2_Input_col4(board2):
x = float(raw_input("User 2, in what col do you want to place your fourth ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U2_Input_col4(board2)
ship4u2 = [U2_Input_row4(board1), U2_Input_col4(board1)]
if ship1u2 == ship2u2 or ship1u2 == ship3u2 or ship1u2 == ship4u2 or ship2u2 == ship3u2 or ship2u2 == ship4u2 or ship3u2 == ship4u2:
print "YOU CANT PLACE 2 SHIPS IN THE SAME SPOT"
U2_Input_row1(board2)
U2_Input_col1(board2)
U2_Input_row2(board2)
U2_Input_col2(board2)
U2_Input_row3(board2)
U2_Input_col3(board2)
U2_Input_row4(board2)
U2_Input_col4(board2)
U1_Input_row1 = U1_Input_row1(board1)
U1_Input_col1 = U1_Input_col1(board1)
U1_Input_row2 = U1_Input_row2(board1)
U1_Input_col2 = U1_Input_col2(board1)
U1_Input_row3 = U1_Input_row3(board1)
U1_Input_col3 = U1_Input_col3(board1)
U1_Input_row4 = U1_Input_row4(board1)
U1_Input_col4 = U1_Input_col4(board1)
U2_Input_row1 = U2_Input_row1(board2)
U2_Input_col1 = U2_Input_col1(board2)
U2_Input_row2 = U2_Input_row2(board2)
U2_Input_col2 = U2_Input_col2(board2)
U2_Input_row3 = U2_Input_row3(board2)
U2_Input_col3 = U2_Input_col3(board2)
U2_Input_row4 = U2_Input_row4(board2)
U2_Input_col4 = U2_Input_col4(board2)
答案 0 :(得分:1)
它要求它两次,因为它通过脚本并在函数之间点击它们:
ship1 = [U1_Input_row1(board1), U1_Input_col1(board1)]
ship2 = [U1_Input_row2(board1), U1_Input_col2(board1)]
ship3 = [U1_Input_row3(board1), U1_Input_col3(board1)]
ship4 = [U1_Input_row4(board1), U1_Input_col4(board1)]
ship1u2 = [U2_Input_row1(board1), U2_Input_col1(board1)]
ship2u2 = [U2_Input_row2(board1), U2_Input_col2(board1)]
ship3u2 = [U2_Input_row3(board1), U2_Input_col3(board1)]
ship4u2 = [U2_Input_row4(board1), U2_Input_col4(board1)]
然后在最后它再次请求输入这些调用:
U1_Input_row1 = U1_Input_row1(board1)
U1_Input_col1 = U1_Input_col1(board1)
U1_Input_row2 = U1_Input_row2(board1)
U1_Input_col2 = U1_Input_col2(board1)
U1_Input_row3 = U1_Input_row3(board1)
U1_Input_col3 = U1_Input_col3(board1)
U1_Input_row4 = U1_Input_row4(board1)
U1_Input_col4 = U1_Input_col4(board1)
U2_Input_row1 = U2_Input_row1(board2)
U2_Input_col1 = U2_Input_col1(board2)
U2_Input_row2 = U2_Input_row2(board2)
U2_Input_col2 = U2_Input_col2(board2)
U2_Input_row3 = U2_Input_row3(board2)
U2_Input_col3 = U2_Input_col3(board2)
U2_Input_row4 = U2_Input_row4(board2)
U2_Input_col4 = U2_Input_col4(board2)
但是这个脚本很难想象,因为大多数这些函数可能只是填充到一个函数中以节省更少的代码行。
这对用户来说很烦人,因为他必须经历输入船位的整个过程,即使其中有7/8是可以的。
if ship1u2 == ship2u2 or ship1u2 == ship3u2 or ship1u2 == ship4u2 or ship2u2 == ship3u2 or ship2u2 == ship4u2 or ship3u2 == ship4u2:
如果用户输入错误,您还可以使用while
语句继续询问问题。现在这样做的方式有点奇怪。
如果用户输入一堆字母或其他东西,脚本会立即崩溃,因为那里没有错误检查。在转换为isdigit()
之前,使用int
可以帮助确定输入是否为整数,如果字段只是空的话,则可以使用try
except
。< / p>
希望这有助于:)
答案 1 :(得分:0)
我不知道它会在你所描述的地方做什么,但我认为你应该使用数组而不是使用ship1u1 ...它会让你更容易阅读你的代码,它会为你节省几行。