Python战舰

时间:2015-10-26 20:35:05

标签: python python-2.x

我知道很多代码,但我不知道这个问题的重要性。 我不知道插入第一个row_guess后程序停止的原因;之后它不会重演

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)  

1 个答案:

答案 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.