project - Python battleship, doubble the trouble -


i have question, why ask twice want put battleship? have no clue way does.

anyway in link can see full code, because don't know if necessary. http://speedy.sh/qyjwp/battleship-goed.txt

i think problem occurs before //_________________________________________________________// part

    board1 = []     board2 = []       x in range(10):         board1.append(["o"] * 10)      x in range(10):         board2.append(["o"] * 10)      def print_board1(board):         row in board:             print " ".join(row)     def print_board2(board):         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 destroy opponents battleship!"     print"good luck!"     print " "     print " "       def u1_input_row1(board1):                 x = float(raw_input("user 1, in row want place first ship?"))                 if x > 0 , x < 11 , x%1 == 0:                     return x - 1                 else:                     print "please enter integer , number between 1 , 10"                     u1_input_row1(board1)      def u1_input_col1(board1):                 x = float(raw_input("user 1, in col want place first ship?"))                 if x > 0 , x < 11 , x%1 == 0:                     return x - 1                 else:                     print "please enter integer , number between 1 , 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 row want place second ship?"))                 if x > 0 , x < 11 , x%1 == 0:                     return x - 1                 else:                     print "please enter integer , number between 1 , 10"                     u1_input_row2(board1)      def u1_input_col2(board1):                 x = float(raw_input("user 1, in col want place second ship?"))                 if x > 0 , x < 11 , x%1 == 0:                     return x - 1                 else:                     print "please enter integer , number between 1 , 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 row want place third ship?"))                 if x > 0 , x < 11 , x%1 == 0:                     return x - 1                 else:                     print "please enter integer , number between 1 , 10"                     u1_input_row3(board1)      def u1_input_col3(board1):                 x = float(raw_input("user 1, in col want place third ship?"))                 if x > 0 , x < 11 , x%1 == 0:                     return x - 1                 else:                     print "please enter integer , number between 1 , 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 row want place fourth ship?"))                 if x > 0 , x < 11 , x%1 == 0:                     return x - 1                 else:                     print "please enter integer , number between 1 , 10"                     u1_input_row4(board1)      def u1_input_col4(board1):                 x = float(raw_input("user 1, in col want place fourth ship?"))                 if x > 0 , x < 11 , x%1 == 0:                     return x - 1                 else:                     print "please enter integer , number between 1 , 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 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 row want place first ship?"))         if x > 0 , x < 11 , x%1 == 0:             return x - 1         else:             print "please enter integer , number between 1 , 10"             u2_input_row1(board2)      def u2_input_col1(board2):         x = float(raw_input("user 2, in col want place first ship?"))         if x > 0 , x < 11 , x%1 == 0:             return x - 1         else:             print "please enter integer , number between 1 , 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 row want place second ship?"))         if x > 0 , x < 11 , x%1 == 0:             return x - 1         else:             print "please enter integer , number between 1 , 10"             u2_input_row2(board2)      def u2_input_col2(board2):         x = float(raw_input("user 2, in col want place second ship?"))         if x > 0 , x < 11 , x%1 == 0:             return x - 1         else:             print "please enter integer , number between 1 , 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 row want place third ship?"))         if x > 0 , x < 11 , x%1 == 0:             return x - 1         else:             print "please enter integer , number between 1 , 10"             u2_input_row3(board2)      def u2_input_col3(board2):         x = float(raw_input("user 2, in col want place third ship?"))         if x > 0 , x < 11 , x%1 == 0:             return x - 1         else:             print "please enter integer , number between 1 , 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 row want place fourth ship?"))         if x > 0 , x < 11 , x%1 == 0:             return x - 1         else:             print "please enter integer , number between 1 , 10"             u2_input_row4(board2)      def u2_input_col4(board2):         x = float(raw_input("user 2, in col want place fourth ship?"))         if x > 0 , x < 11 , x%1 == 0:             return x - 1         else:             print "please enter integer , number between 1 , 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 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) 

it's asking twice because goes through script , hits these between functions:

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)] 

then @ end asks input again these calls:

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) 

the script poorly thought out though, of these functions have been stuffed single function save fewer lines of code.

this annoying user since have go through whole process of inputting ship positions though 7/8 of them ok.

if ship1u2 == ship2u2 or ship1u2 == ship3u2 or ship1u2 == ship4u2 or ship2u2 == ship3u2 or ship2u2 == ship4u2 or ship3u2 == ship4u2: 

you can use while statement keep asking question if user inputs wrong. way it's being done bit weird.

if user inputs bunch of letters or whatnot script crash right away because there's no error checking there. use of isdigit() can determine if input integer before converting int, , try except can use if field left empty.

hope helps out :)


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -