java完成模仿RPG搏斗。本站提示廣大學習愛好者:(java完成模仿RPG搏斗)文章只能為提供參考,不一定能成為您想要的結果。以下是java完成模仿RPG搏斗正文
三個豪傑腳色介入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(); } } }
以上所述就是本文的全體內容了,願望年夜家可以或許愛好。