Java Implementation Simulation RPG fighting _java

Source: Internet
Author: User
Tags rand getsimple

Three hero roles in PK

Each hero has the following attributes: Life value (0 o'clock Hero Fall), attack (each attack with the deduction of each other's life points), attacks interval (after each attack to wait for the interval of time for the next attack, before the first attack must wait for interval time)

In addition, each hero has two skills: attack skills and defensive skills, attack skills in the attack on the other side of a certain probability of launch, defensive skills in the attack by the other side has a certain probability to launch, the specific parameters are as follows

Bm:
Life 650 attacking 40 attack interval 1.5s
Attack skills (Jump split): 30% chance of double damage per attack
Defensive Skills (Bounce): Every time we are attacked, 30% chance to bounce back our injuries to each other, such as we are attacked, the other attack 30, deduct our 30 life value, if the skill is started, then the other side also deduct 30 point of life value, The injury can only bounce once (two bm each other PK time does not appear continuously rebound)

DH: Life 600 attack 30 attacks interval 1s
Attack skills (sucking blood): 30% chance to turn the damage into your own life (damage to the attacker) each time you attack, and the attack damage into their own life value), but can not exceed the upper limit, such as our attack, minus 30 of the life of each other, but also to increase their own 30 points of life value
Defensive Skills (Dodge): 30% chance of getting out of harm's dodge every time you're attacked.

Mk:
Life 700 attacking 50 attack interval 2.5s
Attack skills (swipe): Each attack has a 30% chance of dizziness 3s effect (the other side is injured after the additional vertigo), the other hero dizzy period can not launch attacks, can only be beaten, attack can not launch defensive skills, and vertigo after the other hero to wait for the attack interval, dizziness time can not be superimposed, If the other person is already dizzy, we launch the attack skills, then the other dizziness time to start the calculation again
Defensive Skills (Tenjin): 60% chance of defending half of the damage each time you are attacked, for example, we are attacked, the opposing attack is 40, and if the skill is started, we only deduct 20 points of life.

1. After the program is started, listen to the console input
2. Enter any two hero names (comma separated) to initiate PK, format: BM,DH
3. System output PK detailed process until one side wins, the format is as follows:
BM attack DH,BM launch attack skills, DH does not launch defensive skills, bm:350->350,dh:280->200
....
BM wins

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 time int fight;   Attack int Chancehit; The probability of initiating the active skill int chancedefense;  The probability of initiating defensive skills is abstract void hit (person p); Attack Skill abstract int defense (person P);
    Defensive skills, return injured points} class DH extends person {public DH () {val = 600;
    Coldtime = 1.0;
    Fight = 30;   Chancehit = 3;
    30% probability chancedefense = 3;
  waittime = 0;
  } Random rand = new Random ();   Boolean hitflag = false; Identification of the active skill launch Boolean defenseflag = false; Defensive Skill launch identification public void hit (person p) {if (Rand.nextint) < Chancehit) {//launch active skills 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 > val = 600;   Hitflag = true;
      Mark Active skill has been started} else {//perform 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? "Launch attack skill": "Not launch attack skill") + P.getclass () getsimplename () + (This.defenseflag?) Launch defensive skills ":" Not to launch defensive skills ") + This.getclass (). Getsimplename () +": "+ This.val +", "+ P.getclass (). getsimple
    Name () + ":" + p.val);   Hitflag = false; Defenseflag = false; Reset tag, next reuse} public int defense (person p) {if (Rand.nextint) < Chancedefense) {Defenseflag = True  ;
    Mark defense skills have been launched return 0;
    else {return p.fight; }
  }
}
Class BM extends Person {The public bm () {val = 650;
    Coldtime = 1.5;
    Fight = 40;
    Chancehit = 3;
    Chancedefense = 3;
  waittime = 0;  int count = 0;  Number of defensive skills launched int temp = 40;
  Attack, value with fight Boolean Hitflag = false;
  Boolean defenseflag = false;
 
  Random rand = new Random ();  public void Hit (person p) {if (Rand.nextint) < Chancehit) {fight = fight * 2;
    Launch double Attack hitflag = true;
    int hurt = P.defense (this);
    P.val = P.val-hurt;   Fight = temp;
      Revert to single 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 () + "," + thi S.getclass (). Getsimplename () + (This.hitflag? "Launch attack skill": "Not launch attack skill") + P.getclass () getsimplename () + (This.defenseflag?) Launch defensive skills ":" Not to launch defensive skills ") + This.getclass (). Getsimplename () +": "+ This.val + "," + P.getclass (). Getsimplename () + ":" + p.val);
    Hitflag = false;
  Defenseflag = false; public int Defense (person p) {if (Rand.nextint) < 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) < Chancehit) {p.waittime = 3;
    Make each other dizzy 3s 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 () + "," + thi S.getclass (). Getsimplename () + (This.hitflag? "Launch attack skill": "Not launch attack skill") + P.getclass () getsimplename () + (This.defenseflag?) Launch defensive skills ":" Not to launch defensive skills ") + This.getclass (). Getsimplename () +": "+ This.val +", "+ P.getclass (). getsimple
    Name () + ":" + p.val);
    Hitflag = false;
  Defenseflag = false;
      public int Defense (person p) {if (Rand.nextint) < Chancedefense) {Defenseflag = true;  return P.FIGHT/2;
  Defensive skills launch, damage halved} return p.fight; }
}
public class Rpg {@SuppressWarnings (' unchecked ') public static void main (string[] args) throws Exception {Syste
    M.out.println ("Enter two characters for PK, separated by commas in English: [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 ("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.b
      M ");
    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 the attack SYSTEM.OUT.PRINTLN ("---game start---");
 
        while (true) {Long currentime = System.currenttimemillis ();
          if (NextTime1 < Currentime) {//Time to launch attack P1.hit (P2); NextTime1 + + p1.coldtime*1000 + p1.waittime*1000; Next Attack time = cooldown time + Dizzy Time p1.waittime = 0;
          Turn over, reset the stun time to 0} if (NextTime2 < Currentime) {P2.hit (P1);
          NextTime2 + + p2.coldtime*1000 + p2.waittime*1000;
        P2.waittime = 0;
    (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 the entire contents of this article, I hope you can enjoy.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.