1. 程式人生 > >簡單的回合制小遊戲

簡單的回合制小遊戲

== int 一個 等於 out port .sh 計算公式 xtend

設計需求:

(1)怪物的共有的屬性有:名稱、血量、攻擊力、防禦力;共有的方法有:顯示屬性、判定是否死亡、和攻擊

(2)設置兩種怪物:獨眼巨人,樹怪

(3)獨眼巨人特有的屬性有:武器(狼牙棒或釘錘);獨眼巨人具有的攻擊方法是:使用武器(狼牙棒或釘錘)攻擊

(4)樹怪特有的屬性:高度;樹怪的攻擊方式是使用樹枝纏繞攻擊

(5)創建2只獨眼巨人和1只樹怪的對象,放入集合中,並輪流攻擊集合中的下一個對象(0攻擊1,1攻擊2,2攻擊0),直到只剩下一只怪物存活

(6)傷害值計算公式為:攻擊者攻擊力-被攻擊者防禦力,被攻擊者的血量會要扣減調傷害值

(7)死亡判定規則為:血量小於等於0

怪獸父類:

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("怪物名稱:"+name);
		System.out.println("血量:"+hp);
		System.out.println("攻擊力:"+power);
		System.out.println("防禦力:"+defence);
	}
	public void attacMode(Monster monster) {
		
	}
}

 獨眼巨人類:

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+"使用"+this.arm+"進行攻擊,對"+monster.name+"造成"
					+(this.power-monster.defence)+"點傷害");
		}
		else {
			monster.hp = 0;
			System.out.println(monster.name+"死亡");
			monster.live = false;
		}
	}
	public void show() {
		System.out.print("怪物名稱:"+name);
		System.out.print("  血量:"+hp);
		System.out.print("  攻擊力:"+power);
		System.out.print("  防禦力:"+defence);
		System.out.println("  武器:"+arm);
	}
}

樹精類:

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+"使用樹枝纏繞進行攻擊,對"+monster.name+"造成"
					+(this.power-monster.defence)+"點傷害");
		}
		else {
			monster.hp = 0;
			System.out.println(monster.name+"死亡");
			monster.live = false;
		}
	}
	
	public void show() {
		System.out.print("怪物名稱:"+name);
		System.out.print("  血量:"+hp);
		System.out.print("  攻擊力:"+power);
		System.out.print("  防禦力:"+defence);
		System.out.println("  高度:"+Height);
	}

}

測試類:

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

public class Demo {

	public static void main(String[] args) {
		Monser1 kt = new Monser1("獨眼巨人凱特",100,25,10,"釘錘");
		Monser1 km = new Monser1("獨眼巨人卡姆",100,30,7,"狼牙棒");
		Monser2 gy = new Monser2("樹怪蓋亞",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+"獲得勝利");
				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;
			}
		}
	}
}

簡單的回合制小遊戲