# Module Player2 
# Defines a class handling one player for a
# text-based D&D style game.
# Author: Dana Vrajitoru
# Class: C463/B551/I400 Fall 2025

from random import *

class Player:
    strengthRange = 10
    intelligenceRange = 10
    resourceRange = 5
    healthMax = 5
    
    def __init__(self, human = False, idn = 0):
        self.human = human
        self.strength = randint(1, Player.strengthRange)
        self.intelligence = randint(1, Player.intelligenceRange)
        self.homeBase = 1
        self.health = Player.healthMax
        self.food = 0
        self.wood = 0
        self.brick = 0
        self.idn = idn

    def __str__(self):
        if self.human:
            return "Hum Str: %2d Intel: %2d Hlth: %2d Home: %2d Wood: %2d Brick: %2d Food: %d" \
               %(self.strength, self.intelligence, self.health, self.homeBase, \
                 self.wood, self.brick, self.food)
        else:
            return "NPC Str: %2d Intel: %2d Hlth: %2d Home: %2d Wood: %2d Brick: %2d Food: %d" \
               %(self.strength, self.intelligence, self.health, self.homeBase, \
                 self.wood, self.brick, self.food)
  
    def collect(self):
        which = randint(1, 3)
        howMuch = randint(1, Player.resourceRange)
        if which == 1:
            self.wood += howMuch
            self.strength += 1
            print("collected %d wood" %(howMuch))
        elif which == 2:
            self.brick += howMuch
            self.strength += 1
            print("collected %d brick" %(howMuch))
        else:
            self.food += howMuch
            self.intelligence += 1
            print("collected %d food" %(howMuch))

    def build(self):
        if self.brick >= self.homeBase and self.wood >= self.homeBase:
            self.brick -= self.homeBase
            self.wood -= self.homeBase
            self.homeBase += 1
            print("Home base updated to level", self.homeBase)

    def heal(self):
        if self.health < Player.healthMax + self.homeBase:
            intake = Player.healthMax + self.homeBase - self.health
            if intake > self.food:
                intake = self.food
            self.health += intake
            self.food -= intake
            print("Healed for", intake)

    def attack(self, target):
        if target.health == 0: #self == target or
            return
        print(f"Player {self.idn} attacked player {target.idn}")
        selfCapacity = self.strength + 2 * self.intelligence + self.homeBase
        targetCapacity = target.strength + 2 * target.intelligence + target.homeBase
        attack1 = randint(1, selfCapacity)
        attack2 = randint(1, targetCapacity)
        if attack1 > attack2:
            print(f"player {self.idn} wins")
            self.intelligence += 1
            target.health -= (attack1 - attack2 + 1) // 2
            if target.health < 0:
                target.health = 0
        elif attack2 > attack1:
            print(f"player {target.idn} wins")
            target.intelligence += 1
            self.health -= (attack2 - attack1 + 1) // 2
            if self.health < 0:
                self.health = 0
        else:
            print("it's a draw")

    def random_choice(self, players):
        choice = randint(1, 4)
        who = -1
        if choice == 4:
            who = randint(0, len(players)-1)
        return (choice, who)

    def strategy_choice(self, players):
        return self.random_choice(players)
        
    def play(self, players):
        if self.health == 0:
            return
        choice = who = 0
        if self.human:
            print("Select an action: Collect [1] Build the home base [2] Heal [3] Attack [4]")
            while choice == 0:
                try:
                    choice = int(input())
                except:
                    print("Please enter an integer:")
            if choice == 4:
                print("Which player do you want to attack? [1-4]")
                who = int(input())
        else:
            choice, who = self.strategy_choice(players)
        if choice == 1: # Collect resource
            self.collect()
        elif choice == 2: # Build home base
            self.build()
        elif choice == 3:
            self.heal()
        elif choice == 4:
            self.attack(players[who])
        else:
            print("Wrong choice: ", choice)

            
