I'm trying to get my battleship game working with a ship of length 2. So far everything works most of the time, but on occasion, the below function returns a value of none, instead of an integer between 0 and 4. This occurs approximately 30-40% of the time.
ship_row1, ship_row2, and ship_col1 always have values before the function plays through, so my understanding is that the code should check:
1. If both parts of the ship are in the same row, check if the column difference is one, and only one. If rows are same and column variance is 1, return value.
2. if the ship is verticle (ie, previous functions grant it 2 differing rows), return the column value for existing component.
3. If none of the other conditions are met, re-run the function (creating a loop until success)
def ship_maker2(): col2 = random_col(board) if ship_row1 == ship_row2: if (col2 == ship_col1 + 1) or (col2 == ship_col1 - 1): print "valid column found" return col2 elif ship_row1 != ship_row2: print "consistent columns" return ship_col1 else: ship_maker2()