2015-10-26 118 views
-1

我知道这是很多代码,但我不知道什么是重要的问题。 我不知道插入第一个​​后程序停止的原因;它不会重复以后Python战列舰

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) 
+2

*“我知道这是一个很大的代码,但我不知道是什么的问题,重要的” * - 然后做一些调试和* *找出**,所以你可以回来一个[mcve]。 – jonrsharpe

+0

另请参阅[主题](http://stackoverflow.com/help/on-topic);你已经违反了主题违规#1。 – Prune

+0

我看到功能......但我认为你需要学习如何使用它们。提示:如果你不得不复制和粘贴代码,你做错了。 –

回答

1

你必须在你的代码循环继续轮流。

这里是你的代码的第一个通过逻辑:

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.