Java to simulate RPG fighting

  • 2020-04-01 03:46:26
  • OfStack

Three heroic characters participate in PK

Each hero has the following attributes: health (when the hero falls at 0), attack power (the number of health points deducted from each attack), and attack interval (after each attack, you have to wait for the interval time before the next attack, and also wait for the interval time before the first attack).

In addition, each hero has two abilities: attack skill and defense skill. The attack skill has a certain probability to attack the opponent, and the defense skill has a certain probability to attack the opponent. The specific parameters are as follows

BM:
Hit 650, damage 40, attack interval 1.5s
Attack skill (jump hack) : has a 30% chance to do double damage per attack
Defense skill (rebound) : every time be attacks have a 30% chance to damage our rally to each other, we are attacked, for example, each other damage 30, 30 points deducted from our life value, if the skills, the other is to subtract 30 points in the life value, damage only one rebound (two BM PK continuous bounce don't)

DH: health 600 damage 30 seconds
Attack ability (bloodsucking) : there is a 30% chance to convert damage to your own health per attack (dealing damage to the victim and converting attack damage to your own health), but not exceeding the upper limit
Defense ability (dodge) : has a 30% chance to dodge and avoid damage each time an attack occurs

MK:
Hit 700 HP 50 damage interval 2.5s
Attack skills (blow) : every time attacks have a 30% chance to cause each other vertigo effect of 3 s (add after each other be hurt vertigo), the other party cannot attack during hero dizziness, can only be beaten, attacked cannot launch defense skill, and dizzy after the need to wait for the other hero attack cooldown, dizzy of time can't stack, if the person is in vertigo, we attack skill, then the other vertigo time start again
Defensive ability (god) : has a 60% chance to defend against half of the damage each time you are attacked. For example, if you are attacked, the opponent will have 40 damage

1. After the program starts, monitor the console input
2. Enter any two hero names (comma separated) to initiate PK, format: BM,DH
3. Detailed process of system output PK until one party wins, the format is as follows:
BM attacks DH, BM launches attack skill, DH does not launch defense skill, BM:350-> 350, DH: 280 - > 200
.
BM win


package com.lxi;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;
 
//The base class of three characters
abstract class Person {
  int val;       //Life value
  double coldTime;   //Cooling time
  int waitTime;    //Dizzy of time
  int fight;     //damage
  int chanceHit;   //The probability of initiating an active skill
  int chanceDefense; //Probability of initiating a defense ability
 
  abstract void hit(Person p);  //Attack skills
 
  abstract int defense(Person p); //Defense skill that returns damage points
}
 
class DH extends Person {
  public DH() {
    val = 600;
    coldTime = 1.0;
    fight = 30;
    chanceHit = 3;   //That's a 30% probability
    chanceDefense = 3;
    waitTime = 0;
  }
 
  Random rand = new Random();
  boolean hitFlag = false;   //Active skill launch indicator
  boolean defenseFlag = false; //Defense ability to launch the logo
 
  public void hit(Person p) {
    if (rand.nextInt(10) < chanceHit) { //Initiating skill
      int hurt = p.defense(this);
      p.val = p.val - hurt;
      if (p.val <= 0) {
        System.out.println(this.getClass().getSimpleName() + " win !");
        System.exit(0);
      }
      val = val + hurt;
      if (val > 600)
        val = 600;
      hitFlag = true;   //Mark that the active ability has been activated
    } else { //Make a normal attack
      int hurt = p.defense(this);
      p.val = p.val - hurt;
      if (p.val <= 0) {
        System.out.println(this.getClass().getSimpleName() + " win !");
        System.exit(0);
      }
    }
    System.out.println(this.getClass().getSimpleName() + " attack "
        + p.getClass().getSimpleName() + ","
        + this.getClass().getSimpleName()
        + (this.hitFlag ? " a Attack skills  " : " Not a Attack skills  ")
        + p.getClass().getSimpleName()
        + (this.defenseFlag ? " Launch defense   " : " Unactivated defense ability   ")
        + this.getClass().getSimpleName() + ":" + this.val + ","
        + p.getClass().getSimpleName() + ":" + p.val);
    hitFlag = false;   //
    defenseFlag = false; //Reset the tag for reuse next time
  }
 
  public int defense(Person p) {
    if (rand.nextInt(10) < chanceDefense) {
      defenseFlag = true;  //Mark that the defense ability has been activated
      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;  //Number of defense launches
  int temp = 40;  //The force of attack is equal to fight
  boolean hitFlag = false;
  boolean defenseFlag = false;
  Random rand = new Random();
 
  public void hit(Person p) {
    if (rand.nextInt(10) < chanceHit) {
      fight = fight * 2;  //Launch a double attack
      hitFlag = true;
    }
    int hurt = p.defense(this);
    p.val = p.val - hurt;
    fight = temp;   //Restore to haploid attack
    if (p.val <= 0) {
      System.out.println(this.getClass().getSimpleName() + " win !");
      System.exit(0);
    }
    System.out.println(this.getClass().getSimpleName() + " attack "
        + p.getClass().getSimpleName() + ","
        + this.getClass().getSimpleName()
        + (this.hitFlag ? " Strike skill   " : " No attack ability   ")
        + p.getClass().getSimpleName()
        + (this.defenseFlag ? " Launch defense   " : " Unactivated defense ability   ")
        + 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() + " win !");
          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;  //Stun the opponent
      hitFlag = true;
    }
    int hurt = p.defense(this);
    p.val = p.val - hurt;
    if (p.val <= 0) {
      System.out.println(this.getClass().getSimpleName() + " win !");
      System.exit(0);
    }
    System.out.println(this.getClass().getSimpleName() + " attack "
        + p.getClass().getSimpleName() + ","
        + this.getClass().getSimpleName()
        + (this.hitFlag ? " Strike skill   " : " No attack ability   ")
        + p.getClass().getSimpleName()
        + (this.defenseFlag ? " Launch defense   " : " Unactivated defense ability   ")
        + 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;  //Damage halved when defense is active
    }
    return p.fight;
  }
}

public class Rpg {
  @SuppressWarnings("unchecked")
  public static void main(String[] args) throws Exception {
    System.out.println(" Enter two characters here to proceed PK, Separated by English commas:  [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(" The input format is incorrect, press default 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); //Time to launch an attack
      System.out.println("--- The game start ---");
      while (true) {
        long currenTime = System.currentTimeMillis();
 
        if (nextTime1 < currenTime) { //Attack when the time is up
          p1.hit(p2);
          nextTime1 += p1.coldTime*1000 + p1.waitTime*1000; //Next attack time = cooldown + stun time
          p1.waitTime = 0; //At the end of a turn, the reset is stun for 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();
    }
  }
}

The above is all the content of this article, I hope you can enjoy it.


Related articles: