java特种部队案例(多线程添加弹夹)
package Attect;
import java.util.Random;
public class Player {
private String name;
private int blood = 100;
public void setBlood(int blood) {
this.blood = blood;
}
private Gun gun;
public int getBlood() {
return blood;
}
public String getName() {
return name;
}
public Player() {
}
public Player(String name) {
this(name, 100);
}
public Player(String name, int blood) {
this.name = name;
this.blood = blood;
}
public void holdGun(Gun gun) {
this.gun = gun;
}
public void shootEnemy(Player enemy) {
if (gun == null) {
System.out.println(">>>" + this.getName() + "没有枪,无法进行射击!");
Quan(enemy);
return;
}
System.out.printf("%s向%s开了一枪\n", this.getName(), enemy.name);
gun.shootEnemy(enemy);
}
String[] attacks_desc = {
"%s使出了一招【背心钉】,转到对方的身后,一掌向%s背心的灵台穴拍去。",
"%s使出了一招[游空探爪],飞起身形自半空中变掌为抓锁向%s。",
"%s大喝一声,身形下伏,一招[跨雷坠地],捶向%s双腿。",
"%s运气于掌,一瞬间掌心变得血红,一式[掌心雷],推向%s。",
"%s阴手翻起阳手跟进,一招[没延拦],结结实实的捶向%s。",
"%s上步抢身,招中套招,一招[劈挂连环],连环攻向%s"
};
String[] injureds_desc = {
"结果%s退了半步,毫发无损",
"结果给%s造成一处瘀伤",
"结果一击命中,%s痛得弯下腰",
"结果%s痛苦地闷哼了一声,显然受了点内伤",
"结果%s摇摇晃晃,一跤摔倒在地",
"结果%s脸色一下变得惨白,连退了好几步,",
"结果[轰] 的一声,%s口中鲜血狂喷而出",
"结果%s一声惨叫,像滩软泥般塌了下去"
};
public void Quan(Player enemy) {
Random r = new Random();
int infex = r.nextInt(attacks_desc.length);
String kf = attacks_desc[infex];
int i = 15;
int hurt = r.nextInt(i) + 1;
int remainBoold = enemy.getBlood() - hurt;
remainBoold = remainBoold < 0 ? 0 : remainBoold;
enemy.setBlood(remainBoold);
System.out.printf(kf, this.getName(), enemy.getName() + "造成了" + hurt + "点伤害," + enemy.getName() + "还剩下了" + remainBoold + "点血");
System.out.println();
if (remainBoold > 90) {
System.out.printf(injureds_desc[0], enemy.getName());
} else if (remainBoold > 80 && remainBoold <= 90) {
System.out.printf(injureds_desc[1], enemy.getName());
} else if (remainBoold > 70 && remainBoold <= 80) {
System.out.printf(injureds_desc[2], enemy.getName());
} else if (remainBoold > 60 && remainBoold <= 70) {
System.out.printf(injureds_desc[3], enemy.getName());
} else if (remainBoold > 40 && remainBoold <= 60) {
System.out.printf(injureds_desc[4], enemy.getName());
} else if (remainBoold > 20 && remainBoold <= 40) {
System.out.printf(injureds_desc[5], enemy.getName());
} else if (remainBoold > 10 && remainBoold <= 20) {
System.out.printf(injureds_desc[6], enemy.getName());
} else {
System.out.printf(injureds_desc[7], enemy.getName());
}
System.out.println();
}
public void loadClip(Clip clip) {
if (gun ==null) {
System.out.println(">>>" + this.getName() + "没有枪,无法装弹夹!");
return;
}
gun.loadClip(clip);
}
public void damage(int hurt) {
blood -= hurt;
if (blood <= 0) {
blood = 0;
System.out.println(">>>" + this.getName() + "已死亡");
}
}
public void showPlayer() {
boolean isHoldGun = gun != null;
System.out.printf(">>>玩家信息: 姓名=%s,血量=%d,是否持枪=%b\n", this.getName(), blood, isHoldGun);
}
}
package Attect;
public class Bullet {
public Bullet(){
}
public void hitEnemy(Player enemy){
int hurt = (int)(Math.random()*1+1);
enemy.damage(hurt);
}
}
package Attect;
public class Clip {
private Bullet[] dc;
private int rl = 30;
private int sy=0;
public Clip(){
this(30);
}
public Clip(int rl){
this.dc = new Bullet[rl];
this.sy = 0;
}
public void pushBullet(Bullet bullet){
if(sy == rl){
System.out.println(">>>弹夹已满,请勿重复装弹!");
return;
}
dc[sy] = bullet;
sy++;
}
public Bullet popBullet(){
if(sy == 0){
System.out.println(">>>弹夹已空,无法弹出子弹!");
return null;
}
Bullet bullet = dc[sy - 1];
dc[sy - 1] = null;
sy--;
showClip();
return bullet;
}
public void showClip(){
System.out.printf(">>>弹夹状态:%d/%d\n",sy,rl);
}
}
package Attect;
public class Gun {
private Clip clip;
public Gun(){
this(null);
}
public Gun(Clip clip){
this.clip = clip;
}
public void loadClip(Clip clip){
this.clip = clip;
}
public void shootEnemy(Player enemy){
if(clip == null){
System.out.println(">>>没有弹夹,放空枪!");
return;
}
Bullet bullet = clip.popBullet();
if(bullet == null){
System.out.println(">>>枪械的弹夹已空,放了一个空枪!");
return;
}
bullet.hitEnemy(enemy);
}
public void showGun(){
if(clip!=null){
System.out.println(">>>枪械信息:有弹夹");
}else{
System.out.println(">>>枪械信息:无弹夹");
}
}
}
```java
package Attect;
public class LoadBulletThread extends Thread{
private Clip clip;
public LoadBulletThread(Clip clip) {
this.clip = clip;
}
public void Run() {
for (int i = 0; i < 30; i++) {
clip.pushBullet(new Bullet());
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package Attect;
import java.util.Scanner;
public class TestGungame {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("欢迎来到特种部队!");
System.out.println("********战前准备********");
Player p1 =new Player();
System.out.print("请录入昵称:");
String name = in.next();
System.out.print("1.雨潇生 2.醉清风 3.如梦令 请选择对战玩家(1~3):");
int num = in.nextInt();
switch(num){
case 1:
p1 = new Player("雨潇生",100);
break;
case 2:
p1 = new Player("醉清风",100);
break;
case 3:
p1 = new Player("如梦令",100);
break;
default:
System.out.println("您的输入有误!");
break;
}
Player p2 = new Player(name,100);
Gun gun1 = new Gun();
Gun gun2 = new Gun();
p1.holdGun(null);
p2.holdGun(null);
Clip clip1 = new Clip();
Clip clip2 = new Clip();
LoadBulletThread thread1 = new LoadBulletThread(clip1);
LoadBulletThread thread2 = new LoadBulletThread(clip2);
thread1.Run();
thread2.Run();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("子弹装填完成");
p1.showPlayer();
p2.showPlayer();
int i = 0;
System.out.println("********开始对战********");
boolean isExit = false;
while(true){
System.out.println("---第"+(i+1)+"回合---");
i++;
p1.shootEnemy(p2);
if(p2.getBlood()==0) {
System.out.println(p1.getName()+"获胜!");
System.out.println("退出江湖!");
return;
}
p2.shootEnemy(p1);
if(p1.getBlood()==0) {
System.out.println(p2.getName()+"获胜!");
System.out.println("退出江湖!");
return;
}
System.out.println();
p1.showPlayer();
p2.showPlayer();
System.out.println(" 玩家1是否持枪 1.true 2.false");
int isno = in.nextInt();
if(isno==1){
p1.holdGun(gun1);
p1.loadClip(clip1);
}else {
p1.holdGun(null);
}
System.out.println("玩家2是否持枪");
int isnoo = in.nextInt();
if(isnoo==1){
p2.holdGun(gun2);
p2.loadClip(clip2);
}else {
p2.holdGun(null);
}
System.out.print("是否继续对战(输入y继续,其他键退出)?");
if(!(in.next().equals("y"))){
System.out.println("退出江湖!");
return;
}
}
}
}