2015-11-01 43 views
0

我有一個問題,爲什麼它問你兩次你想把你的戰列艦? 我不知道它是做什麼的。蟒蛇戰列艦,再次遇到麻煩

無論如何,在這個鏈接中,你可以看到完整的代碼,因爲我不知道這是否是必要的。 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) 

回答

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語句來不斷地問一個問題,如果用戶輸入一些錯誤。現在做的方式有點奇怪。

如果用戶輸入一堆字母,或者腳本會立即崩潰,因爲這裏沒有錯誤檢查。在轉換爲int之前,使用isdigit()可以幫助確定輸入是否爲整數,如果該字段僅留空,則可以使用tryexcept

希望這有助於:)

0

我看不出它會對你描述了之前發生的問題,但我認爲你應該使用的,而不是做ship1u1數組...這會讓你閱讀你的代碼變得容易很多,它會爲你節省幾行。

+0

這是最大的非回答:) –