import random
# Create a 4x4 grid board. Starts at 0 so I did a 5x5
board = [["*"] * 5 for _ in range(5)]
def print_board(board):
for row in board:
print(" ".join(row))
# Hide a ship on the board.
ship_row = random.randint(1, 5)
ship_col = random.randint(1, 5)
ship_row2 = ship_row + random.randint(-1, 0)
ship_col2 = ship_col + random.randint(-1, 0) # Creating longer ships
#Intrduction to board game
print("Welcome to Christy's Battleship!")
print_board(board)
# You get 5 turns to guess
for turn in range(5):
print(f"\nTurn {turn + 1}")
try:
#Player gets to guess row and column
guess_row = int(input("Guess Row (1-5): "))
guess_col = int(input("Guess Column (1-5): "))
except ValueError:
print("Enter a valid number only!")
continue
if (guess_row == ship_row and guess_col == ship_col) or (guess_row == ship_row2 and guess_col == ship_col2): # Matching for if half a ship is hit
print("You sunk my battleship!")
break
elif 1 <= guess_row < 5 and 1 <= guess_col < 5:
if board[guess_row][guess_col] == "X":
print("You already guessed that spot!")
else:
print("Missed!")
board[guess_row][guess_col] = "X"
else:
print("Try again!")
print_board(board)
if turn == 4:
print("\nGame Over! The ship was at:")
print(f"Row: {ship_row}, Column: {ship_col}")