2015-11-01 2 views
0

У меня есть вопрос, почему он спрашивает вас дважды, где вы хотите поставить вам линкор? У меня нет подсказки, так оно и есть.Линейный корабль Python, удвоить проблему

В любом случае по этой ссылке вы можете увидеть полный код, потому что я не знаю, если это необходимо. 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 заявление, чтобы задать вопрос, если пользователь вводит что-то не так. То, как это делается сейчас, немного странно.

Если пользователь вводит кучу букв или что-то, что скрипт сразу же сработает, потому что там нет ошибок. Использование isdigit() может помочь определить, является ли ввод целым, прежде чем преобразовать его в int, и использовать tryexcept, если поле просто пусто.

Надеюсь, что это поможет :)

0

Я не вижу, где он будет делать то, что вы описали, но я думаю, вы должны использовать массив вместо того, чтобы делать ship1u1 ... Это упростило бы чтение вашего кода, и это сэкономит вам пару строк.

+0

Это самый большой ответ: –

Смежные вопросы