Multiple Ships Troubleshooting

tuples version test run
================ RESTART: D:/cc/discuss/battleship_tuples.py ================
Let's play Battleship!
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
row: 3
col: 3
O O O O O
O O O O O
O O O O O
O O O X O
O O O O O
row: 2
col: 2
O O O O O
O O O O O
O O X O O
O O O X O
O O O O O
row: 1
col: 1
O O O O O
O X O O O
O O X O O
O O O X O
O O O O O
row: 0
col: 0
X O O O O
O X O O O
O O X O O
O O O X O
O O O O O
row: 4
col: 4
X O O O O
O X O O O
O O X O O
O O O X O
O O O O X
row: 0
col: 4
X O O O X
O X O O O
O O X O O
O O O X O
O O O O X
row: 1
col: 3
X O O O X
O X O X O
O O X O O
O O O X O
O O O O X
row: 3
col: 1
X O O O X
O X O X O
O O X O O
O X O X O
O O O O X
row: 4
col: 0
X O O O X
O X O X O
O O X O O
O X O X O
X O O O X
row: 0
col: 0
You guessed that one already
row: 0
col: 1
X X O O X
O X O X O
O O X O O
O X O X O
X O O O X
row: 0
col: 2
X X X O X
O X O X O
O O X O O
O X O X O
X O O O X
row: 0
col: 3
X X X X X
O X O X O
O O X O O
O X O X O
X O O O X
row: 1
col: 1
You guessed that one already
row: 4
col: 1
X X X X X
O X O X O
O O X O O
O X O X O
X X O O X
row: 4
col: 2
X X X X X
O X O X O
O O X O O
O X O X O
X X X O X
row: 4
col: 3
X X X X X
O X O X O
O O X O O
O X O X O
X X X X X
row: 1
col: 0
X X X X X
X X O X O
O O X O O
O X O X O
X X X X X
row: 1
col: 2
X X X X X
X X X X O
O O X O O
O X O X O
X X X X X
row: 1
col: 4
X X X X X
X X X X X
O O X O O
O X O X O
X X X X X
row: 2
col: 0
X X X X X
X X X X X
X O X O O
O X O X O
X X X X X
row: 2
col: 1
X X X X X
X X X X X
X X X O O
O X O X O
X X X X X
row: 2
col: 3
You have sunk Ship A
X X X X X
X X X X X
X X X A O
O X O X O
X X X X X
row: 2
col: 4
X X X X X
X X X X X
X X X A X
O X O X O
X X X X X
row: 3
col: 0
X X X X X
X X X X X
X X X A X
X X O X O
X X X X X
row: 3
col: 2
X X X X X
X X X X X
X X X A X
X X X X O
X X X X X
row: 3
col: 4
You have sunk Ship B
X X X X X
X X X X X
X X X A X
X X X X B
X X X X X
You have won the game!
>>> 

The code is written without a turns limit for testing purposes.

tuples test code

from random import randint

board =

for x in range(5):
board.append([“O”] * 5)

def print_board(board):
for row in board:
print (" ".join(row))

print (“Let’s play Battleship!”)

a = randint(0, 24)
while True:
b = randint(0, 24)
if b != a: break

shipa = (int(a / 5), a % 5)
shipb = (int(b / 5), b % 5)
ship_status = [True, True]

while True in ship_status:
print_board(board)
while True:
#guess = (int(raw_input('row: ')), int(raw_input('col: ')))
guess = (int(input('row: ')), int(input('col: ')))
if guess[0] in range(5) and guess[1] in range(5):
if board[guess[0]][guess[1]] == ‘X’:
print (“You guessed that one already”)
else: break

# Now for testing...

if guess == shipa:
    print ("You have sunk Ship A")
    board[guess[0]][guess[1]] = 'A'
    ship_status[0] = False
elif guess == shipb:
    print ("You have sunk Ship B")
    board[guess[0]][guess[1]] = 'B'
    ship_status[1] = False
else:
    board[guess[0]][guess[1]] = 'X'

print_board(board)
print (“You have won the game!”)

The above is written in Python 3 but will run in Python 2 if you swap the input lines. It should still run regardless, but in Python 2 we should not use input() since it runs eval().

Tuples

Tuples are like ordered lists but they are immutable. In the above code we do not see the unpacking that is done in the background when comparing the tuples. When we compare tuples, the corresponding elements are unpacked and compared.

Eg.

a = (3, 4)
b = (3, 4)
print a == b    # <-  True

Unpacking a tuple looks something like this…

>>> t = (4, 2)
>>> u, v = t
>>> u
4
>>> v
2
>>> 

while statements are very powerful control flow constructs, as we can witness above. They are open ended so can be infinite, which is why we need a break condition on the inner loop.

"Another test run
============= RESTART: D:/cc/discuss/battleship_tuples_count.py =============
Let's play Battleship!
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
row: 0
col: 0
X O O O O
O O O O O
O O O O O
O O O O O
O O O O O
row: 2
col: 2
You have sunk Ship A
X O O O O
O O O O O
O O A O O
O O O O O
O O O O O
row: 4
col: 4
X O O O O
O O O O O
O O A O O
O O O O O
O O O O X
row: 3
col: 4
X O O O O
O O O O O
O O A O O
O O O O X
O O O O X
row: 3
col: 1
You have sunk Ship B
You have won the game!
X O O O O
O O O O O
O O A O O
O B O O X
O O O O X
>>> 

Off to add a turns limit…

turns limit test run
Let's play Battleship!
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
row: 0
col: 0
X O O O O
O O O O O
O O O O O
O O O O O
O O O O O
row: 1
col: 1
X O O O O
O X O O O
O O O O O
O O O O O
O O O O O
row: 2
col: 2
X O O O O
O X O O O
O O X O O
O O O O O
O O O O O
row: 3
col: 3
X O O O O
O X O O O
O O X O O
O O O X O
O O O O O
row: 4
col: 4
X O O O O
O X O O O
O O X O O
O O O X O
O O O O X
row: 0
col: 4
X O O O X
O X O O O
O O X O O
O O O X O
O O O O X
row: 1
col: 3
X O O O X
O X O X O
O O X O O
O O O X O
O O O O X
row: 3
col: 1
X O O O X
O X O X O
O O X O O
O X O X O
O O O O X
row: 4
col: 0
X O O O X
O X O X O
O O X O O
O X O X O
X O O O X
row: 0
col: 2
Sorry, you lose!
X O X O X
O X O X O
O O X O O
O X O X O
X O O O X
>>> 

Python 3 code

battleship_tuples_count.py
from random import randint
board = []
for x in range(5):
    board.append(["O"] * 5)
def print_board(board):
    for row in board:
        print (" ".join(row))
print ("Let's play Battleship!")
a = randint(0, 24)
while True:
    b = randint(0, 24)
    if b != a: break      
shipa = (int(a / 5), a % 5)
shipb = (int(b / 5), b % 5)
ship_status = [True, True]; count = 0
while True in ship_status:
    print_board(board)
    while True:
        guess = (int(input('row: ')), int(input('col: ')))
        if guess[0] in range(5) and guess[1] in range(5): 
            if board[guess[0]][guess[1]] == 'X':
                print ("You guessed that one already")
            else: break
        else: print ("That's not in the ocean!"); count -= 1
    if guess == shipa:
        print ("You have sunk Ship A")
        board[guess[0]][guess[1]] = 'A'
        ship_status[0] = False
    elif guess == shipb:
        print ("You have sunk Ship B")
        board[guess[0]][guess[1]] = 'B'
        ship_status[1] = False
    else:
        board[guess[0]][guess[1]] = 'X'
    count += 1
    if count > 9:
        print ("Sorry, you lose!")
        break
else:
    print ("You have won the game!")
print_board(board)

One last test run

test run with win
============= RESTART: D:/cc/discuss/battleship_tuples_count.py =============
Let's play Battleship!
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
row: 1
col: 1
O O O O O
O X O O O
O O O O O
O O O O O
O O O O O
row: 2
col: 2
O O O O O
O X O O O
O O X O O
O O O O O
O O O O O
row: 3
col: 3
O O O O O
O X O O O
O O X O O
O O O X O
O O O O O
row: 4
col: 4
O O O O O
O X O O O
O O X O O
O O O X O
O O O O X
row: 0
col: 0
X O O O O
O X O O O
O O X O O
O O O X O
O O O O X
row: 4
col: 0
X O O O O
O X O O O
O O X O O
O O O X O
X O O O X
row: 3
col: 1
X O O O O
O X O O O
O O X O O
O X O X O
X O O O X
row: 1
col: 3
You have sunk Ship B
X O O O O
O X O B O
O O X O O
O X O X O
X O O O X
row: 0
col: 4
You have sunk Ship A
You have won the game!
X O O O A
O X O B O
O O X O O
O X O X O
X O O O X
>>>