Three heroic characters participate in PK
Each hero has the following attributes: health (the hero falls when it is 0), attack power (the opponent's health points are deducted for each attack), and attack interval (you must wait for the interval after each attack before proceeding) attack, you must also wait for the interval before attacking for the first time)
In addition, each hero has two skills: attack skills and defensive skills. Attack skills have a certain probability of being activated when attacking the opponent, and defensive skills have a certain probability of being activated when being attacked by the opponent. The specific parameters are as follows
BM:
HP 650, attack power 40, attack interval 1.5s
Attack skill (jump chop): There is a 30% chance of causing double damage every time you attack. Defense skill (rebound): Every time you are attacked, there is a 30% chance of reflecting the damage we received to the opponent. For example, if you are attacked, The opponent's attack power is 30, and 30 points of our health will be deducted. If the skill is activated, the opponent will also deduct 30 points of health, and the damage can only be rebounded once (there will be no continuous rebound when two BMs PK each other).
DH: HP 600, attack power 30, attack interval 1s
Attack skill (blood-sucking): Each time you attack, there is a 30% chance of converting the damage caused into your own health (causing damage to the attacked person and converting the attack damage into your own health), but it cannot exceed the upper limit, for example When we attack, we will deduct 30 points of the opponent's health, and at the same time add 30 points of health to ourselves. Defense skill (dodge): every time we are attacked, there is a 30% chance to dodge and not be harmed.
MK:
HP 700, attack power 50, attack interval 2.5s
Attack skill (heavy hit): Each time you attack, there is a 30% chance of causing the opponent to be stunned for 3 seconds (the opponent will be stunned for 3 seconds after receiving damage). The opponent's hero cannot attack during the period of stun, but can only be beaten, and cannot initiate defensive skills when being attacked. , and after the stun ends, the opponent's hero must wait for the attack interval again, and the stun time does not It can be superimposed. If the opponent is already stunned and we launch an attack skill, the opponent's stun time will start counting again. Defense skill (God): There is a 60% chance to defend half of the damage every time we are attacked. For example, if we are attacked, the opponent will The attack power is 40. If the skill is activated, only 20 points of our health will be deducted.
1. After the program starts, monitor the console input
2. Enter any two hero names (separated by commas) to initiate PK, format: BM, DH
3. The system outputs the detailed PK process until one side wins. The format is as follows:
BM attacks DH, BM activates attack skills, DH does not activate 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; //Health value double coldTime; // Cooling time int waitTime; //Stun time int fight; //Attack power int chanceHit; //Probability of initiating active skills int chanceDefense; //Probability of initiating defensive skills abstract void hit(Person p); //Attack skills abstract int defense(Person p); //Defense skills, return the number of damage points} class DH extends Person { public DH() { val = 600; coldTime = 1.0; fight = 30; chanceHit = 3 ; //Indicates a 30% probability chanceDefense = 3; waitTime = 0; } Random rand = new Random(); boolean hitFlag = false; //Active skill activation flag boolean defenseFlag = false; //Defense skill activation flag public void hit(Person p) { if (rand.nextInt(10) < chanceHit) { //Active active skill activation 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 active skill has been activated} 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 ? "Attack skill launched" : "Attack skill not launched") + p.getClass().getSimpleName() + (this.defenseFlag ? "Defense skills activated" : "Defense skills not activated") + this.getClass().getSimpleName() + ":" + this.val + "," + p.getClass().getSimpleName() + ":" + p.val); hitFlag = false; // defenseFlag = false; //Reset the flag and reuse it next time} public int defense(Person p) { if (rand.nextInt(10) < chanceDefense) { defenseFlag = true; //Mark defensive skills have 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 defensive skills activated int temp = 40; //Attack power, the same value as 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 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() + "," + this. getClass().getSimpleName() + (this.hitFlag ? "Attack skill launched" : "Attack skill not launched") + p.getClass().getSimpleName() + (this.defenseFlag ? "Activate defense skills" : "Defense skills are not activated") + 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 for 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() + "," + this.getClass().getSimpleName() + (this.hitFlag ? "Attack skills launched" : "Attack skills not launched") + p.getClass().getSimpleName() + (this.defenseFlag ? "Defense skills launched" : "Defense skills not launched") + 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; //Defensive skills are activated, damage is halved} return p.fight; } }
public class Rpg { @SuppressWarnings("unchecked") public static void main(String[] args) throws Exception { System.out.println("Enter two characters here for 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("Input format is wrong, press the 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); / /The time to launch the attack System.out.println("---Game starts---"); while (true) { long currenTime = System.currentTimeMillis(); if (nextTime1 < currenTime) { //Launch attack when time is up p1.hit(p2); nextTime1 += p1.coldTime*1000 + p1.waitTime*1000; //Next attack time = cooling time + stunned time p1 .waitTime = 0; //At the end of the round, reset the stunned time to 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 the entire content of this article, I hope you all like it.