Simulated RPG combat

Source: Internet
Author: User

Three hero roles participate in PK

Each hero has the following attributes: The life value (0 when the hero falls down), attack strength (each attack deducts the other's life points) attack interval (wait for the interval to start the next attack after each attack, and wait for the interval before the first attack)

In addition, each hero has two skills: Attack skill and defense skill. The attack skill may launch a certain probability when attacking the other party, and the Defense Skill may launch a certain probability when being attacked by the other party. The specific parameters are as follows:

BM:
Lifecycle 650 attack force 40 attack interval 1.5 s
Attack skill (split): Each attack has a 30% probability of double damage
Defense skills (rebound): Each attack has a 30% chance of Rebounding the damage we suffered to the other party. For example, if we are attacked, the other party's attack strength is 30, excluding our 30-point life value, if the skill starts, the other party will deduct the 30-point life value, and the injury can only be rebounded once (when the two BM players compete with each other, there will be no continuous rebound)

DH: lifecycle 600 attack force 30 attack interval 1 s
Attack skill (blood-sucking): Each attack has a 30% chance to convert the damage to your own life (damage to the attacker and change the attack damage to your own life ), however, the limit cannot be exceeded. For example, if we initiate an attack, deduct the other party's 30 lifecycle values, and add a 30-point lifecycle value to ourselves.
Defense skills (Dodge): Each attack has a 30% probability of Dodge from injury.

MK:
Lifecycle 700 attack power 50 attack interval 2.5 s
Attack skill (severe attack): Each attack has a 30% probability of causing Vertigo to the other party for 3 seconds (the other party is attached with vertigo after being hurt). During the vertigo of the other hero, no attacks can be initiated, but only attacks can be made, no defense skills can be initiated during attacks. After the vertigo ends, the other hero will wait for the attack interval again. The vertigo time cannot be superimposed. If the other hero is already dizzy, we will launch another attack skill, then the opponent's vertigo time is re-computed
Defense skills (Tian Shen): Each attack has a 60% chance of defending against half of the damage. For example, if we are attacked, the attack capability of the other party is 40. If the skill is launched, we will only deduct our 20 points of life.

1. After the program is started, enter
2. Enter any two hero names (separated by commas) to initiate a PK. Format: BM, DH
3. The system outputs the detailed PK process until one party wins. The format is as follows:
BM attack DH, BM attack skill, DH no defense skill, BM: 350-> 350, DH: 280-> 200
....
BM wins
1. [Code] [Java] Code
Package com. LXI;
 
Import java. Io. bufferedreader;
Import java. Io. inputstreamreader;
Import java. util. Random;
 
// Base class of the three characters
Abstract class person {
Int val; // life value
Double coldtime; // cool-down time
Int waittime; // The dizzy time.
Int fight; // attack power
Int chancehit; // probability of initiating an active skill
Int chancedefense; // probability of initiating defense skills
 
Abstract void hit (person P); // attack skill
 
Abstract int defense (person P); // defense skill, returns the number of injured points
}
 
Class DH extends person {
Public DH (){
Val = 600;
Coldtime = 1.0;
Fight = 30;
Chancehit = 3; // indicates the probability of 30%
Chancedefense = 3;
Waittime = 0;
}
 
Random Rand = new random ();
Boolean hitflag = false; // ID of an active skill launch
Boolean defenseflag = false; // The identifier of the defensive skill launch.
 
Public void hit (person p ){
If (RAND. nextint (10) <chancehit) {// initiate an active skill
Int hurt = P. Defense (this );
P. Val = P. Val-hurt;
If (P. Val <= 0 ){
System. Out. println (this. getclass (). getsimplename () + "wins! ");
System. Exit (0 );
}
Val = Val + hurt;
If (Val & gt; 600)
Val = 600;
Hitflag = true; // indicates that the active skill has been started.
} Else {// perform common attacks
Int hurt = P. Defense (this );
P. Val = P. Val-hurt;
If (P. Val <= 0 ){
System. Out. println (this. getclass (). getsimplename () + "wins! ");
System. Exit (0 );
}
}
System. Out. println (this. getclass (). getsimplename () + "attack"
+ P. getclass (). getsimplename () + ","
+ This. getclass (). getsimplename ()
+ (This. hitflag? "Launching attack skills": "not launching attack skills ")
+ P. getclass (). getsimplename ()
+ (This. defenseflag? "Launching defense skills": "not launching defense skills ")
+ This. getclass (). getsimplename () + ":" + this. Val + ","
+ P. getclass (). getsimplename () + ":" + P. Val );
Hitflag = false ;//
Defenseflag = false; // reset the tag and reuse it next time.
}
 
Public int defense (person p ){
If (RAND. nextint (10) <chancedefense ){
Defenseflag = true; // mark that the defense skill has been launched
Return 0;
} Else {
Return P. fight;
}
}
}
2. [Code] continued

Class BM extends person {
Public BM (){
Val = 650;
Coldtime = 1.5;
Fight = 40;
Chancehit = 3;
Chancedefense = 3;
Waittime = 0;
}
 
Int COUNT = 0; // number of times the defense skill was initiated
Int temp = 40; // attack power, with 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; // double attack
Hitflag = true;
}
Int hurt = P. Defense (this );
P. Val = P. Val-hurt;
Fight = temp; // restore to a single attack
If (P. Val <= 0 ){
System. Out. println (this. getclass (). getsimplename () + "wins! ");
System. Exit (0 );
}
System. Out. println (this. getclass (). getsimplename () + "attack"
+ P. getclass (). getsimplename () + ","
+ This. getclass (). getsimplename ()
+ (This. hitflag? "Launching attack skills": "not launching attack skills ")
+ P. getclass (). getsimplename ()
+ (This. defenseflag? "Launching defense skills": "not launching defense skills ")
+ This. getclass (). getsimplename () + ":" + this. Val + ","
+ P. getclass (). getsimplename () + ":" + P. Val );
Hitflag = false; flash plug-in
Defenseflag = false;
} Http://www.huiyi8.com/flashchajian/
 
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 () + "wins! ");
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; // make the other party dizzy for 3 S
Hitflag = true;
}
Int hurt = P. Defense (this );
P. Val = P. Val-hurt;
If (P. Val <= 0 ){
System. Out. println (this. getclass (). getsimplename () + "wins! ");
System. Exit (0 );
}
System. Out. println (this. getclass (). getsimplename () + "attack"
+ P. getclass (). getsimplename () + ","
+ This. getclass (). getsimplename ()
+ (This. hitflag? "Launching attack skills": "not launching attack skills ")
+ P. getclass (). getsimplename ()
+ (This. defenseflag? "Launching defense skills": "not launching defense skills ")
+ 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; // The Defense Skill starts and the damage is halved.
}
Return P. fight;
}
}
3. [Code] continued 2
Public class RPG {
@ Suppresswarnings ("unchecked ")
Public static void main (string [] ARGs) throws exception {
System. Out. println ("enter two characters for PK here and separate them with 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 ("incorrect input format, by 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 when the attack was initiated
System. Out. println ("--- game start ---");
While (true ){
Long currentime = system. currenttimemillis ();
 
If (nexttime1 <currentime) {// The attack is triggered when the time reaches
P1.hit (P2 );
Nexttime1 + = p1.coldtime * 1000 + p1.waittime * 1000; // next attack time = cool-down time + dizzy time
P1.waittime = 0; // the end of the Round. The time for the reset to be dizzy is 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 ();
}
}
}

Simulated RPG combat

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.