Skip to content
Snippets Groups Projects
Forked from jez04-vyuka / java-efrei / efrei-lab04
3 commits behind the upstream repository.
Hero.java 1.17 KiB
package cz.vsb.fei.efrei.lab03;

import java.util.Random;

public class Hero implements Fighter{

	private static final Random RANDOM = new Random();

	private String name;
	private int strenght;
	private int lives;

	public Hero(String name, int strenght) {
		if(name == null || name.length() < 2 ||
				strenght < 30) {
			throw new IllegalArgumentException(
					"Sorry wrong arguments!!");
		}
		this.name = name;
		this.strenght = strenght;
		this.lives = 100;
	}

	public Hero(String name) {
		this(name, RANDOM.nextInt(50) + 50);
	}

	@Override
	public void attackedBy(Fighter fighter) 
			throws AlreadyDeadException {
		if(!isAlive()) {
			throw new AlreadyDeadException(this);
		}
		System.out.println(fighter.getName() + " attack " + getName() + "!");
		if (RANDOM.nextBoolean()) {
			System.out.println("Attack was blocked!! Ha. Ha. Ha.");
			return;
		}
		lives -= fighter.getStrenght();
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public int getStrenght() {
		return strenght;
	}

	@Override
	public int getLives() {
		return lives;
	}

	@Override
	public String toString() {
		return "Hero " + name + "(" + strenght + ") - has " + lives + " lives.";
	}

}