三个英雄角色参与PK
每个英雄具有以下几个属性:生命值(为0时英雄倒下)、攻击力(每次攻击时扣除对方的生命值点数)、攻击间隔(每次攻击过后都要等待间隔时间才能进行下次攻击,首次攻击之前也要先等待间隔时间)
另外,每个英雄都拥有两个技能:攻击技能和防御技能,攻击技能在攻击对方时有一定概率发动,防御技能在被对方攻击时有一定概率发动,具体参数如下
BM:
生命650 攻击力40 攻击间隔1.5s
攻击技能(跳劈):每次攻击时有30%几率造成双倍伤害
防御技能(反弹):每次被攻击时有30%几率把我方受到的伤害反弹给对方,例如我方被攻击,对方攻击力30,扣除我方30点生命值,如果技能发动,则对方也要扣除30点生命值,伤害只能反弹一次(两个BM相互PK的时候不出现连续反弹)
DH:生命600 攻击力30 攻击间隔1s
攻击技能(吸血):每次攻击时有30%几率把造成的伤害转变为自己的生命值(对被攻击者造成伤害,并且将攻击伤害转变成自己的生命值),但不能超过上限,例如我方攻击,扣除对方30的生命值,同时给自己增加30点生命值
防御技能(闪避):每次被攻击时有30%几率闪避不受伤害
MK:
生命700 攻击力50 攻击间隔2.5s
攻击技能(重击):每次攻击时有30%几率造成对方眩晕3s的效果(对方受到伤害后附加眩晕),对方英雄眩晕期间不能发起攻击,只能挨打,被攻击时也不能发起防御技能,且眩晕结束后对方英雄要重新等待攻击间隔,眩晕时间不能叠加,如果对方已经处于眩晕,我方又发动攻击技能,那么对方眩晕时间重新开始计算
防御技能(天神):每次被攻击时有60%的几率防御一半伤害,例如我方被攻击,对方攻击力为40,如果技能发动,则只扣除我方20点生命值
1.程序启动后,监听控制台输入
2.输入任意两个英雄名称(逗号分隔)发起PK,格式:BM,DH
3.系统输出PK详细过程,直到有一方胜出,格式如下:
BM攻击DH,BM发动攻击技能,DH未发动防御技能,BM:350->350,DH:280->200
….
BM胜出
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; } } }
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; } }
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(); } } }
以上所述就是本文的全部内容了,希望大家能够喜欢。
评论(0)