Arad import random
import os
import sys
# Constants
WIDTH = 10
HEIGHT = 10
NUM_IDEAS = 5
NUM_SHADOWS = 3
def random_empty_cell(occupied):
"""Select a random empty cell that's not in occupied set."""
while True:
x = random.randint(0, WIDTH - 1)
y = random.randint(0, HEIGHT - 1)
cell = (x, y)
if cell not in occupied:
return cell
def move_position(pos, direction):
"""Move the player based on input direction."""
x, y = pos
if direction == 'w' and y > 0: # Up
return (x, y - 1)
elif direction == 'a' and x > 0: # Left
return (x - 1, y)
elif direction == 's' and y < HEIGHT - 1: # Down
return (x, y + 1)
elif direction == 'd' and x < WIDTH - 1: # Right
return (x + 1, y)
return pos # Return same position if move is invalid
def shadow_step_towards(shadow, player):
"""Move shadow one step closer to player (Manhattan distance)."""
sx, sy = shadow
px, py = player
if abs(sx - px) > abs(sy - py):
if sx < px and sx < WIDTH - 1:
return (sx + 1, sy)
elif sx > px and sx > 0:
return (sx - 1, sy)
else:
if sy < py and sy < HEIGHT - 1:
return (sx, sy + 1)
elif sy > py and sy > 0:
return (sx, sy - 1)
return shadow
def draw(player, ideas, shadows, turns, collected):
"""Draw the game board."""
os.system('cls' if os.name == 'nt' else 'clear')
print(f"Turns: {turns} | Ideas collected: {collected}/{NUM_IDEAS}")
for y in range(HEIGHT):
row = ""
for x in range(WIDTH):
cell = (x, y)
if cell == player:
row += "P "
elif cell in ideas:
row += "I "
elif cell in shadows:
row += "S "
else:
row += ". "
print(row)
print("Use w/a/s/d to move, q to quit.")
def main():
# Initialize unique positions
occupied = set()
player = random_empty_cell(occupied)
occupied.add(player)
ideas = set()
for _ in range(NUM_IDEAS):
cell = random_empty_cell(occupied)
ideas.add(cell)
occupied.add(cell)
shadows = set() # Changed to set for efficiency
for _ in range(NUM_SHADOWS):
cell = random_empty_cell(occupied)
shadows.add(cell)
occupied.add(cell)
turns = 0
collected = 0
while True:
draw(player, ideas, shadows, turns, collected)
if collected >= NUM_IDEAS:
print("\nYou collected all ideas! You win!")
break
move = input("Your move: ").strip().lower()
if not move:
continue
if move == 'q':
print("Goodbye!")
break
if move[0] not in 'wasd':
print("Invalid move! Use w/a/s/d.")
continue
# Move player
player = move_position(player, move[0])
turns += 1
# Collect idea if on it
if player in ideas:
ideas.remove(player)
collected += 1
print("You found an idea!")
if collected < NUM_IDEAS:
input("Press Enter to continue...")
# Move shadows
new_shadows = set()
for s in shadows:
ns = shadow_step_towards(s, player)
# Avoid duplicates in new_shadows
if ns not in new_shadows:
new_shadows.add(ns)
else:
new_shadows.add(s) # Keep old position if new one is taken
# If a shadow steps on an idea, respawn it
if ns in ideas:
ideas.remove(ns)
occupied_now = new_shadows | ideas | {player}
new_cell = random_empty_cell(occupied_now)
ideas.add(new_cell)
occupied_now.add(new_cell)
shadows = new_shadows
# Update occupied set
occupied = shadows | ideas | {player}
# Lose condition
if player in shadows:
draw(player, ideas, shadows, turns, collected)
print("\nA shadow caught you. Game over.")
break
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print("\nGame interrupted. Bye!")
sys.exit(0)