Files
ai7-m2/Battleships/lesson_1.py
2025-11-06 10:14:37 +03:00

41 lines
1.0 KiB
Python

# BATTLESHIPS - Grade 7 Python Game
# Lesson 2: Full mini-game with 3 ships and turns!
import random
# Step 1: Create 3 hidden ships (as a list of [row, col])
ships = []
while len(ships) < 3:
r = random.randint(0, 4)
c = random.randint(0, 4)
if [r, c] not in ships: # avoid duplicates
ships.append([r, c])
print("3 ships are hidden on a 5x5 grid!")
print("You have 10 turns to find them all.")
hits = 0
turns = 10
# Step 2: Game loop
for turn in range(turns):
print("\nTurn", turn + 1)
# Get guess
guess_row = int(input("Row (0-4): "))
guess_col = int(input("Col (0-4): "))
# Check if guess is a ship
if [guess_row, guess_col] in ships:
print("🎯 HIT!")
ships.remove([guess_row, guess_col]) # remove found ship
hits += 1
if hits == 3:
print("🏆 You found all ships! You win!")
break
else:
print("💦 MISS!")
# Step 3: Game over message
if hits < 3:
print("Game over! You found", hits, "out of 3 ships.")