package com.alibaba.demo02;
import com.sun.media.jfxmedia.events.PlayerEvent;
import java.util.Random;
public class Player {
private String name;
private String type;
private int life;
private int attack;
private int defense;
public void say(){
System.out.println("我是"+name+",是一个"+type);
System.out.println(",生命值高达"+life+",防御力"+defense+",攻击力"+attack);
}
public void pk(Player p) {
int flag = 0;
while (true) {
this.say();
p.say();
if (flag == 0) {
Random r = new Random();
int a =(int) r.nextInt(2)+1;
// 随机产生一个整数
int harm = (this.attack - p.defense)*a;
if (a==2){
System.out.println(p.name+"暴击了"+this.name + "掉血" + harm);
}else {
System.out.println(this.name + "掉血" + harm);
}
this.life -= harm;
flag = 1;
} else {
Random r = new Random();
int b=(int) r.nextInt(2)+1;
// 随机产生一个整数
int harm = (this.attack - p.defense)*b;
if (b==2){
System.out.println(this.name+"暴击了"+p.name + "掉血" + harm);
}else {
System.out.println(p.name + "掉血" + harm);
}
p.life -= harm;
flag = 0;
}
if (this.life <= 0) {
System.out.println(p.name + "打败了" + this.name);
Random r = new Random();
int b=(int) r.nextInt(2)+1;
if (b==2){
System.out.println("掉落了武器");
}else {
System.out.println("掉落了防具");
}
break;
}
if (p.life <= 0) {
System.out.println(this.name + "打败了" + p.name);
Random r = new Random();
int b=(int) r.nextInt(2)+1;
if (b==2){
System.out.println("掉落了武器");
}else {
System.out.println("掉落了防具");
}
break;
}
}
}
public Player(String name, String type, int life, int defense, int attack) {
this.name = name;
this.type = type;
this.life = life;
this.defense = defense;
this.attack = attack;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getDefense() {
return defense;
}
public void setDefense(int defense) {
this.defense = defense;
}
}
package com.alibaba.demo02;
public class PlayerText {
public static void main(String[] args) {
Player p1 = new Player("李白","刺客",100,10,40);
Player p2 = new Player("鲁班","射手",50,10,20);
p1.pk(p2);
}
}