Python战舰,麻烦加倍

Python battleship, double the trouble

为什么它问你两次你要把你的战舰放在哪里? 我不知道该怎么做。

无论如何在这个 link 你可以看到完整的代码,因为我不知道是否有必要。 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)

我看不出它会在哪里执行您描述的操作,但我认为您应该使用数组而不是执行 ship1u1...这会使您的代码更容易阅读,并且会节省您的时间情侣线。

它问了两次,因为它遍历了脚本并在函数之间点击了这些:

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

希望这对您有所帮助:)