import?java.io.BufferedReader;
import?java.io.InputStreamReader;
public?class?Rpg?{
@SuppressWarnings("unchecked")
public?static?void?main(String[]?args)?throws?Exception?{
System.out.println("在這裏輸入兩個人物進行PK,以英文逗號分隔:?[BM,DH,MK]");
BufferedReader?br?=?new?BufferedReader(new?InputStreamReader(System.in));
Class<Person>?c1;
Class<Person>?c2;
try?{
String?temp?=?br.readLine();
String[]?str?=?temp.split(",");
if?(str.length?!=?2)?{
throw?new?Exception("輸入格式有誤,按默認PK");
}
c1?=?(Class<Person>)?Class.forName("com.lxi."
+?str[0].toUpperCase());
c2?=?(Class<Person>)?Class.forName("com.lxi."
+?str[1].toUpperCase());
}?catch?(Exception?e)?{
//?TODO?Auto-generated?catch?block
c1?=?(Class<Person>)?Class.forName("com.lxi.BM");
c2?=?(Class<Person>)?Class.forName("com.lxi.DH");
}
try?{
Person?p1?=?c1.newInstance();
Person?p2?=?c2.newInstance();
long?time?=?System.currentTimeMillis();
long?nextTime1?=?(long)?(time?+?p1.coldTime*1000);?//
long?nextTime2?=?(long)?(time?+?p2.coldTime*1000);?//發動攻擊的時間
System.out.println("---遊戲開始---");
while?(true)?{
long?currenTime?=?System.currentTimeMillis();
if?(nextTime1?<?currenTime)?{?//時間到則發動攻擊
p1.hit(p2);
nextTime1?+=?p1.coldTime*1000?+?p1.waitTime*1000;?//下次攻擊時間=冷卻時間+被暈眩時間
p1.waitTime?=?0;?//回合結束,重置被暈眩時間為0
}
if?(nextTime2?<?currenTime)?{
p2.hit(p1);
nextTime2?+=?p2.coldTime*1000?+?p2.waitTime*1000;
p2.waitTime?=?0;
}
}
}?catch?(ClassCastException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}?catch?(InstantiationException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}?catch?(IllegalAccessException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
}package?com.lxi;
import?java.util.Random;
class?BM?extends?Person?{
public?BM()?{
val?=?650;
coldTime?=?1.5;
fight?=?40;
chanceHit?=?3;
chanceDefense?=?3;
waitTime?=?0;
}
int?count?=?0;//防禦技能發動的次數
int?temp?=?40;//攻擊力,值同fight
boolean?hitFlag?=?false;
boolean?defenseFlag?=?false;
Random?rand?=?new?Random();
public?void?hit(Person?p)?{
if?(rand.nextInt(10)?<?chanceHit)?{
fight?=?fight?*?2;//發動雙倍攻擊
hitFlag?=?true;
}
int?hurt?=?p.defense(this);
p.val?=?p.val?-?hurt;
fight?=?temp;?//還原為單倍攻擊
if?(p.val?<=?0)?{
System.out.println(this.getClass().getSimpleName()?+?"勝出!");
System.exit(0);
}
System.out.println(this.getClass().getSimpleName()?+?"攻擊"
+?p.getClass().getSimpleName()?+?","
+?this.getClass().getSimpleName()
+?(this.hitFlag"發動攻擊技能"?:?"未發動攻擊技能")
+?p.getClass().getSimpleName()
+?(this.defenseFlag"發動防禦技能"?:?"未發動防禦技能")
+?this.getClass().getSimpleName()?+?":"?+?this.val?+?","
+?p.getClass().getSimpleName()?+?":"?+?p.val);
hitFlag?=?false;
defenseFlag?=?false;
}
public?int?defense(Person?p)?{
if?(rand.nextInt(10)?<?chanceDefense)?{
if?(count?!=?0)?{
p.val?=?p.val?-?p.fight;
count++;
defenseFlag?=?true;
if?(p.val?<=?0)?{
System.out.println(this.getClass().getSimpleName()?+?"勝出!");
System.exit(0);
}
}
}
return?p.fight;
}
}
class?MK?extends?Person?{
public?MK()?{
val?=?700;
coldTime?=?2.5;
fight?=?50;
chanceDefense?=?6;
chanceHit?=?3;
waitTime?=?0;
}
boolean?hitFlag?=?false;
boolean?defenseFlag?=?false;
Random?rand?=?new?Random();
public?void?hit(Person?p)?{
if?(rand.nextInt(10)?<?chanceHit)?{
p.waitTime?=?3;//使對方暈眩3s
hitFlag?=?true;
}
int?hurt?=?p.defense(this);
p.val?=?p.val?-?hurt;
if?(p.val?<=?0)?{
System.out.println(this.getClass().getSimpleName()?+?"勝出!");
System.exit(0);
}
System.out.println(this.getClass().getSimpleName()?+?"攻擊"
+?p.getClass().getSimpleName()?+?","
+?this.getClass().getSimpleName()
+?(this.hitFlag"發動攻擊技能"?:?"未發動攻擊技能")
+?p.getClass().getSimpleName()
+?(this.defenseFlag"發動防禦技能"?:?"未發動防禦技能")
+?this.getClass().getSimpleName()?+?":"?+?this.val?+?","
+?p.getClass().getSimpleName()?+?":"?+?p.val);
hitFlag?=?false;
defenseFlag?=?false;
}
public?int?defense(Person?p)?{
if?(rand.nextInt(10)?<?chanceDefense)?{
defenseFlag?=?true;
return?p.fight?/?2;//防禦技能發動,傷害減半
}
return?p.fight;
}
}package?com.lxi;
import?java.io.BufferedReader;
import?java.io.InputStreamReader;
import?java.util.Random;
//三個人物的基類
abstract?class?Person?{
int?val;?//生命值
double?coldTime;?//冷卻時間
int?waitTime;//暈眩時間
int?fight;?//攻擊力
int?chanceHit;?//發起主動技能的概率
int?chanceDefense;?//發起防禦技能的概率
abstract?void?hit(Person?p);//攻擊技能
abstract?int?defense(Person?p);?//防禦技能,返回被傷害點數
}
class?DH?extends?Person?{
public?DH()?{
val?=?600;
coldTime?=?1.0;
fight?=?30;
chanceHit?=?3;?//表示30%的概率
chanceDefense?=?3;
waitTime?=?0;
}
Random?rand?=?new?Random();
boolean?hitFlag?=?false;?//主動技能發動的標識
boolean?defenseFlag?=?false;?//防禦技能發動的標識
public?void?hit(Person?p)?{
if?(rand.nextInt(10)?<?chanceHit)?{?//發動主動技能
int?hurt?=?p.defense(this);
p.val?=?p.val?-?hurt;
if?(p.val?<=?0)?{
System.out.println(this.getClass().getSimpleName()?+?"勝出!");
System.exit(0);
}
val?=?val?+?hurt;
if?(val?>?600)
val?=?600;
hitFlag?=?true;?//標記主動技能已經發動
}?else?{?//進行普通攻擊
int?hurt?=?p.defense(this);
p.val?=?p.val?-?hurt;
if?(p.val?<=?0)?{
System.out.println(this.getClass().getSimpleName()?+?"勝出!");
System.exit(0);
}
}
System.out.println(this.getClass().getSimpleName()?+?"攻擊"
+?p.getClass().getSimpleName()?+?","
+?this.getClass().getSimpleName()
+?(this.hitFlag"發動攻擊技能"?:?"未發動攻擊技能")
+?p.getClass().getSimpleName()
+?(this.defenseFlag"發動防禦技能"?:?"未發動防禦技能")
+?this.getClass().getSimpleName()?+?":"?+?this.val?+?","
+?p.getClass().getSimpleName()?+?":"?+?p.val);
hitFlag?=?false;?//
defenseFlag?=?false;?//重置標記,下次重用
}
public?int?defense(Person?p)?{
if?(rand.nextInt(10)?<?chanceDefense)?{
defenseFlag?=?true;//標記防禦技能已經發動
return?0;
}?else?{
return?p.fight;
}
}
}