Simple turn system Games

Design requirements:

(1) The common attributes of monsters are: name, blood volume, attack power and defense power; the common methods are: display attributes, determine whether to die, and attack

(2) Set up two kinds of Monsters: one eyed giant and tree monster

(3) The unique attributes of Cyclops are: Weapons (maces or spikes); the attack method of Cyclops is: attack with weapons (maces or spikes)

(4) Tree monster's unique attribute: height; tree monster's attack method is to use twig to wrap attack

(5) Create 2 Cyclops and 1 Troll objects, put them into the collection, and attack the next object in the collection in turn (0 attack 1, 1 attack 2, 2 attack 0), until only one monster survives

(6) The formula for calculating the damage value is: attacker's attack power - the victim's defense power. The victim's HP will be reduced by adjusting the damage value

(7) Death determination rule: blood volume is less than or equal to 0

Monster fathers:

public class Monster {
	public String name;
	public int hp;
	public int power;
	public int defence;
	public boolean live = true;
	public Monster(String name, int hp, int power, int defence) {
		super();
		this.name = name;
		this.hp = hp;
		this.power = power;
		this.defence = defence;
	}
	public void show() {
		System.out.println("Monster Name:"+name);
		System.out.println("Blood volume:"+hp);
		System.out.println("Aggressivity:"+power);
		System.out.println("Defense:"+defence);
	}
	public void attacMode(Monster monster) {
		
	}
}

Cyclops:

public class Monser1 extends Monster{
	String arm;
	
	public Monser1(String name, int hp, int power, int defence,String arm) {
		super(name, hp, power, defence);
		this.arm = arm;
	}
	
	public void attacMode(Monster monster) {
		monster.hp  = monster.hp - (this.power-monster.defence);
		if(monster.hp>0) {
			System.out.println(this.name+"Use"+this.arm+"Attack on"+monster.name+"cause"
					+(this.power-monster.defence)+"Point injury");
		}
		else {
			monster.hp = 0;
			System.out.println(monster.name+"death");
			monster.live = false;
		}
	}
	public void show() {
		System.out.print("Monster Name:"+name);
		System.out.print("  Blood volume:"+hp);
		System.out.print("  Aggressivity:"+power);
		System.out.print("  Defense:"+defence);
		System.out.println("  Weapons:"+arm);
	}
}

Tree species:

public class Monser2 extends Monster{

	String Height;
	public Monser2(String name, int hp, int power, int defence,String Height) {
		super(name, hp, power, defence);
		this.Height = Height;
	}
	
	public void attacMode(Monster monster) {
		monster.hp  = monster.hp - (this.power-monster.defence);
		if(monster.hp>0) {
			System.out.println(this.name+"Use twig wrapping to attack"+monster.name+"cause"
					+(this.power-monster.defence)+"Point injury");
		}
		else {
			monster.hp = 0;
			System.out.println(monster.name+"death");
			monster.live = false;
		}
	}
	
	public void show() {
		System.out.print("Monster Name:"+name);
		System.out.print("  Blood volume:"+hp);
		System.out.print("  Aggressivity:"+power);
		System.out.print("  Defense:"+defence);
		System.out.println("  Height:"+Height);
	}

}

Test class:

import java.util.ArrayList;
import java.util.List;

public class Demo {

	public static void main(String[] args) {
		Monser1 kt = new Monser1("One eyed giant Kate",100,25,10,"Claw hammer");
		Monser1 km = new Monser1("One eyed giant Kam",100,30,7,"Mace");
		Monser2 gy = new Monser2("Tree Gaia",80,35,12,"10.5");
		List<Monster> list = new ArrayList<Monster>();
		list.add(kt);
		list.add(km);
		list.add(gy);
		for(int i=0;i<list.size();i++) {
			list.get(i).show();
		}
		int size = 0;
		while(true) {
			if(list.size()==1) {
				System.out.println(list.get(0).name+"Win");
				break;
			}
			if(size<list.size()) {
				list.get(size).attacMode(list.get(size+1));
				if(list.get(size+1).hp==0)
					list.remove(size+1);
				size++;
			}
			if(size==list.size()-1) {
				list.get(size).attacMode(list.get(0));
				if(list.get(0).hp==0)
					list.remove(0);
				size = 0;
			}
		}
	}
}

Tags: Java Attribute less

Posted on Wed, 01 Apr 2020 02:27:16 -0700 by prueba123a