'''
Created on July 9, 2013

@author: Jared
'''

import random

# This class describes a warrior with a laser sword.
# The warrior must recharge his/her sword with a battery.
class LaserSwordsman:
    FULL_CHARGE = 3
    
    # Initializes name to the given name, initializes battery, charges sword, and sets wins to 0 
    def __init__(self, name="Default"):
        self.name = name
        self.battery = self.FULL_CHARGE
        self.charged = True
        self.wins = 0
    
    # If there is enough battery charge and sword is not already charged, charges sword.
    # Prints out which condition failed if any
    def charge_sword(self):
        if (self.battery > 0):
            if (not(self.charged)):
                self.battery -= 1
                self.charged = True
                print(self.name + "'s sword is now charged.")
            else:
                print(self.name + "'s sword is already charged.")
        else:
            print(self.name + "'s battery is empty.")
    
    # Replaces battery if necessary
    def replace_battery(self):
        if (self.battery != self.FULL_CHARGE):
            self.battery = self.FULL_CHARGE
            print(self.name + "'s battery is now full.")
        else:
            print(self.name + "'s batter is already full.")
    
    # Makes two warriors duel only if both swords are charged.
    # Each warrior has a 50% chance of winning.
    def duel(self, other):
        if (self.charged and other.charged):
            print(self.name + " and " + other.name + " are dueling.")
            if (random.randint(1, 2) == 1):
                winner = self
                loser = other
            else:
                winner = other
                loser = self
                
            print(winner.name + " won!\n" + loser.name + " has brought shame to the village.")
            winner.wins += 1
            self.charged = False
            other.charged = False
        else:
            print("These warriors cannot duel because at least one sword is uncharged.")
            
    # Prints out the number of wins
    def print_wins(self):
        print(self.name + " won " + str(self.wins) + " times.")


# Make two warriors and let them duel
warrior1 = LaserSwordsman("Yoda")
warrior2 = LaserSwordsman("Darth")

# check if the methods say the sword is already charged and battery is full
warrior1.charge_sword()
warrior1.replace_battery()

# duel four times
for i in range(4):
    warrior1.duel(warrior2)
    warrior1.charge_sword()
    warrior2.charge_sword()

# this duel should fail
warrior1.duel(warrior2)

# this charge should fail
warrior1.charge_sword()

# replace batteries and recharge
warrior1.replace_battery()
warrior2.replace_battery()
warrior1.charge_sword()
warrior2.charge_sword()

# one final duel
warrior1.duel(warrior2)

# print wins
warrior1.print_wins()
warrior2.print_wins()
