2 Ships


#1


This line imports random

from random import randint

board = []

adds 5 Zeros to the list and then does this 5 times in for loop leaving 5 lists with 5 Zeros in each list

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

This line defines the function toprint the board and removes list parenthesis and adds a space (" ") between the Zeros

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

print ("Let's play Battleship!")
print_board(board)

The next few lines define the computer picking a random number for col and row. The len must be -1 as the for loop a the top uses range which starts from 1 whereas randint starts from 0

def random_row(board):
return randint(0, len(board) - 1)

def random_col(board):
return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)
ship2_row = random_row(board)
ship2_col = random_col(board)
print(ship_row)
print(ship_col)

while ship2_row == ship_row and ship2_col==ship_col:
ship2_row = random_row(board)
ship2_col = random_col(board)

print(ship2_row)
print(ship2_col)
1

1st for loop defines 10 turns for game

for turn in range(10):
turn = 0
#user input for guess
guess_row = int(input("Guess Row:"))
guess_col = int(input("Guess Col:"))

if guess_row == ship_row and guess_col == ship_col:
    print ("Congratulations! You sunk battleship 1!")
    board[guess_row][guess_col] = "X"
    if board[ship_row][ship_col] == "X" and board[ship2_row][ship2_col] == "X":
        print("YOU WIN!")
        break
elif guess_row == ship2_row and guess_col == ship2_col:
    print ("Congratulations! You sunk battleship 2!")
    board[guess_row][guess_col] = "X"
    if board[ship_row][ship_col] == "X" and board[ship2_row][ship2_col] == "X":
        print("YOU WIN!") 
        break
else:
    if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
        print ("Oops, that's not even in the ocean.")
        #This line checks that the user guess isnt already an X, if it is it processes this part of code
    elif(board[guess_row][guess_col] == "X"):
        print ("You guessed that one already.")
        #This is for if user has all 10 turns
    elif turn == 9:
        print("HAHAHA You missed for the last time")
        print ("Game Over")
        board[guess_row][guess_col] = "X"
    else:
        print ("You missed my battleship!")
        board[guess_row][guess_col] = "X"
        #Next line adds +1 to turn so user can see which turn he is on then prints the new board with X on the missed guess
turn += 1
print(turn)
print_board(board)


Why does this code not reprint the turn with an extra integer each time? Every time I run the game it always prints as "1" for the variable turn and does not increase by 1 on each loop like it should. What am I missing?
PS. I have added comments to this code to help me remember what various lines do.


Replace this line with your code.


#3

At the beginning of each "for loop", you defined turn=0. At each turn : turn =0 , turn +=1, print (turn) thus : 1


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.