Python 巨蟒战舰
我知道这是很多代码,但我不知道什么对这个问题很重要。 我不知道为什么程序在插入第一行后停止;之后就不会再重复了Python 巨蟒战舰,python,python-2.x,Python,Python 2.x,我知道这是很多代码,但我不知道什么对这个问题很重要。 我不知道为什么程序在插入第一行后停止;之后就不会再重复了 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 " ".j
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 you 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 < 10 and x%1 == 0:
return x - 1
else:
print "Please enter an integer"
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 < 10 and x%1 == 0:
return x - 1
else:
print "Please enter an integer"
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 < 10 and x%1 == 0:
return x - 1
else:
print "Please enter an integer"
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 < 10 and x%1 == 0:
return x - 1
else:
print "Please enter an integer"
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 < 10 and x%1 == 0:
return x - 1
else:
print "Please enter an integer"
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 < 10 and x%1 == 0:
return x - 1
else:
print "Please enter an integer"
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 < 10 and x%1 == 0:
return x - 1
else:
print "Please enter an integer"
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 < 10 and x%1 == 0:
return x - 1
else:
print "Please enter an integer"
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 < 10 and x%1 == 0:
return x - 1
else:
print "Please enter an integer"
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 < 10 and x%1 == 0:
return x - 1
else:
print "Please enter an integer"
U2_Input_col1(board2)
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 < 10 and x%1 == 0:
return x - 1
else:
print "Please enter an integer"
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 < 10 and x%1 == 0:
return x - 1
else:
print "Please enter an integer"
U2_Input_col2(board2)
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 < 10 and x%1 == 0:
return x - 1
else:
print "Please enter an integer"
U2_Input_row3(board2)
def U2_Input_col3(board2):
x = float(raw_input("User 12 in what col do you want to place your third ship?"))
if x > 0 and x < 10 and x%1 == 0:
return x - 1
else:
print "Please enter an integer"
U2_Input_col3(board2)
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 < 10 and x%1 == 0:
return x - 1
else:
print "Please enter an integer"
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 < 10 and x%1 == 0:
return x - 1
else:
print "Please enter an integer"
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)
User_Turn = 1
total_ships2 = 4
total_ships1 = 4
if User_Turn == 21:
print "Both players failed"
else:
if User_Turn % 2== 20:
for turn in range(1):
U2_Guess_row = int(raw_input("User 2, in what row do you think an enemy ship is?")) - 1
U2_Guess_col = int(raw_input("User 2, in what col do you think an enemy ship is?")) - 1
if U2_Guess_row == U1_Input_row1 and U2_Guess_col == U1_Input_col1 or U2_Guess_row == U1_Input_row2 and U2_Guess_col == U1_Input_col2 or U2_Guess_row == U1_Input_row3 and U2_Guess_col == U1_Input_col3 or U2_Guess_row == U1_Input_row4 and U2_Guess_col == U1_Input_col4:
print "Congratulations! You sunk an enemy battleship"
board1[guess_row][guess_col] = "#"
total_ships2 -= 1
print "Board User 1"
print_board1(board1)
print "----------------------------------------------"
print "Board User 2"
print_board2(board2)
if total_ships2 == 0:
print "User 2, you won the game!"
print "You destroyed all hostile ships!"
break
else:
if (U2_Guess_row < 0 or U2_Guess_row > 9) or (U2_Guess_col < 0 or U2_Guess_col > 9):
User_Turn += 1
print User_Turn
print "Oops, that's not even in the ocean."
elif (board1[U2_Guess_row][U2_Guess_col] == "X"):
User_Turn += 1
print User_Turn
print "You guessed that one already."
else:
User_Turn += 1
print User_Turn
print "You missed my battleship!"
if turn == 9:
print "GAME OVER"
if turn == 9:
print "Game Over"
board1[U2_Guess_row][U2_Guess_col] = "X"
print "Turn", turn + 1
print "Board User 1"
print_board1(board1)
print "----------------------------------------------"
print "Board User 2"
print_board2(board2)
else:
for turn in range(1):
U1_Guess_row = int(raw_input("User 1, in what row do you think an enemy ship is?")) - 1
U1_Guess_col = int(raw_input("User 1, in what col do you think an enemy ship is?")) - 1
if U1_Guess_row == U2_Input_row1 and U1_Guess_col == U2_Input_col1 or U1_Guess_row == U2_Input_row2 and U1_Guess_col == U2_Input_col2 or U1_Guess_row == U2_Input_row3 and U1_Guess_col == U2_Input_col3 or U1_Guess_row == U2_Input_row4 and U1_Guess_col == U2_Input_col4:
board2[U1_Guess_row][U1_Guess_col] = "#"
total_ships1 -= 1
print "Board User 1"
print_board1(board1)
print "----------------------------------------------"
print "Board User 2"
print_board2(board2)
print "Congratulations! You sunk an enemy battleship"
if total_ships1 == 0:
print "User 1, you won the game!"
print "You destroyed all hostile ships!"
print "Board User 1"
print_board1(board1)
print "----------------------------------------------"
print "Board User 2"
print_board2(board2)
break
else:
if (U1_Guess_row < 0 or U1_Guess_row > 9) or (U1_Guess_col < 0 or U1_Guess_col > 9):
print "Oops, that's not even in the ocean."
User_Turn += 1
print User_Turn
elif (board2[U1_Guess_row][U1_Guess_col] == "X"):
User_Turn += 1
print User_Turn
print "You guessed that one already."
else:
User_Turn += 1
print User_Turn
print "You missed my battleship!"
if turn == 9:
print "GAME OVER"
if turn == 9:
print "Game Over"
board2[U1_Guess_row][U1_Guess_col] = "X"
print "Turn", turn + 1
print "Board User 1"
print_board1(board1)
print "----------------------------------------------"
print "Board User 2"
print_board2(board2)
你必须在代码中循环才能继续 下面是代码的第一次传递逻辑:
User_Turn = 1
...
if User_Turn == 21: # it's not
...
else:
if User_Turn % 2== 20: # it's not...actually can only be 0 or 1 never 20.
...
else:
for turn in range(1):
... # do all this stuff exactly once.
我知道这是很多代码,但我不知道什么对问题很重要-然后进行一些调试并找出答案,这样您就可以返回一个;你已经触犯了离题违规1。我看到函数。。。但我认为你需要学会如何使用它们。提示:如果您必须复制并粘贴代码,那么您就错了。