import random
# --- Character Data ---
characters = {
"Luffy": {"HP": 100, "AttackRange": 1, "AttackPower": 15, "MovementRange": 3, "X": 0, "Y": 0}, #Added Starting position
"Zoro": {"HP": 80, "AttackRange": 1, "AttackPower": 20, "MovementRange": 2, "X": 0, "Y": 0}, #Added Starting Position
"Nami": {"HP": 60, "AttackRange": 2, "AttackPower": 10, "MovementRange": 3, "X": 0, "Y": 0} #Added Starting position
}
# --- Map Data ---
MAP_WIDTH = 6
MAP_HEIGHT = 6
map_grid = [['.' for _ in range(MAP_WIDTH)] for _ in range(MAP_HEIGHT)] # . is empty space
# Function to print map
def print_map():
for row in map_grid:
print(''.join(row))
# Function to place characters on the map
def place_character(char_name, x, y):
"""Places a character on the map at the specified coordinates."""
characters[char_name]["X"] = x
characters[char_name]["Y"] = y
map_grid[y][x] = char_name[0] #Uses the first letter
# Function to check if the move is valid
def is_valid_move(x, y):
"""Checks if a move is within the map boundaries and to an empty space."""
if 0 <= x < MAP_WIDTH and 0 <= y < MAP_HEIGHT and map_grid[y][x] == '.':
return True
return False
# Function to move the character
def move_character(char_name, new_x, new_y):
"""Moves a character to a new position on the map."""
old_x = characters[char_name]["X"]
old_y = characters[char_name]["Y"]
if is_valid_move(new_x, new_y):
characters[char_name]["X"] = new_x
characters[char_name]["Y"] = new_y
map_grid[new_y][new_x] = char_name[0] # Update map with new position
map_grid[old_y][old_x] = '.' # Clear old position
return True
else:
print("Invalid move!")
return False
# Function for attack
def attack(attacker, defender):
"""Simulates an attack. Returns True if defender is defeated, False otherwise."""
damage = characters[attacker]["AttackPower"]
characters[defender]["HP"] -= damage
print(f"{attacker} attacks {defender} for {damage} damage!")
print(f"{defender}'s HP: {characters[defender]['HP']}")
if characters[defender]["HP"] <= 0:
print(f"{defender} has been defeated!")
map_grid[characters[defender]["Y"]][characters[defender]["X"]] = "." # clear from the map
return True # Defender is defeated
return False # Defender is still alive
# Function to check if a character is within attack range
def is_within_attack_range(attacker, defender):
"""Checks if the defender is within the attacker's attack range."""
attacker_x = characters[attacker]["X"]
attacker_y = characters[attacker]["Y"]
defender_x = characters[defender]["X"]
defender_y = characters[defender]["Y"]
distance = max(abs(attacker_x - defender_x), abs(attacker_y - defender_y)) #Chebyshev distance for range
return distance <= characters[attacker]["AttackRange"]
# Function to get user input for move
def get_move(char_name):
"""Gets the desired move direction from the player."""
while True:
direction = input(f"Enter move direction for {char_name} (up, down, left, right, or none): ").lower()
if direction in ["up", "down", "left", "right", "none"]:
return direction
else:
print("Invalid direction. Please enter up, down, left, right, or none.")
# Function to handle movement
def handle_movement(char_name):
"""Handles the movement logic for a character."""
remaining_moves = characters[char_name]["MovementRange"]
while remaining_moves > 0:
print(f"{char_name} has {remaining_moves} move(s) remaining.")
direction = get_move(char_name)
if direction == "none":
break
new_x = characters[char_name]["X"]
new_y = characters[char_name]["Y"]
if direction == "up":
new_y -= 1
elif direction == "down":
new_y += 1
elif direction == "left":
new_x -= 1
elif direction == "right":
new_x += 1
if move_character(char_name, new_x, new_y):
remaining_moves -= 1
print_map()
else:
print("Invalid move. Try again.")
#--- Main Game ---
def main():
# --- Initialization ---
player1_chars = ["Luffy"] # Example
player2_chars = ["Zoro"] #Example
# Random placement on the map
for char in player1_chars:
while True:
x = random.randint(0, MAP_WIDTH - 1)
y = random.randint(0, MAP_HEIGHT - 1)
if map_grid[y][x] == '.':
place_character(char, x, y)
break
for char in player2_chars:
while True:
x = random.randint(0, MAP_WIDTH - 1)
y = random.randint(0, MAP_HEIGHT - 1)
if map_grid[y][x] == '.':
place_character(char, x, y)
break
print_map()
# --- Game Loop ---
turn = 1
while True:
print(f"\n--- Turn {turn} ---")
# Player 1's Turn
print("\nPlayer 1's Turn:")
for char in player1_chars:
if characters[char]["HP"] > 0: #If the char is alive
print(f"\n{char}'s turn:")
handle_movement(char)
# Check for attack opportunity
target = None
for enemy in player2_chars:
if characters[enemy]["HP"] > 0 and is_within_attack_range(char, enemy): #If the enemy is alive and in range
target = enemy
break
if target:
if attack(char, target):
player2_chars.remove(target) #If the enemy is defeated remove from the list.
print_map() # update the map
# Check for win condition for Player 1
if not player2_chars:
print("Player 1 wins!")
break
# Player 2's Turn
print("\nPlayer 2's Turn:")
for char in player2_chars:
if characters[char]["HP"] > 0: #If the char is alive
print(f"\n{char}'s turn:")
handle_movement(char)
# Check for attack opportunity
target = None
for enemy in player1_chars:
if characters[enemy]["HP"] > 0 and is_within_attack_range(char, enemy): #If the enemy is alive and in range
target = enemy
break
if target:
if attack(char, target):
player1_chars.remove(target) #If the enemy is defeated remove from the list
print_map()
# Check for win condition for Player 2
if not player1_chars:
print("Player 2 wins!")
break
turn += 1
if __name__ == "__main__":
main()