java特种部队案例

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;
    }
    /**
     * 判断是否持枪
     *
     * @param gun
     */
    public void holdGun(Gun gun) {
        this.gun = gun;
    }
    /**
     * 射击
     *
     * @param enemy 对手
     */
    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();
    }
    /**
     * 装弹夹
     *
     * @param clip
     */
    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;
    }
    /**
     * 装弹夹
     * @param clip
     */
    public void loadClip(Clip clip){
        this.clip = clip;
    }
    /**
     * 开枪
     * @param enemy
     */
    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()); // 调用pushBullet方法将子弹装填到弹夹中
            try {
                Thread.sleep(1); // 线程休眠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 {
            //为了确保两个线程都执行完毕,使用 join 方法来等待这两个线程的结束。join 方法会阻塞当前线程,直到被调用的线程执行完毕。
            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.shootEnemy(p2);
                p1.loadClip(clip1);
            }else {
                p1.holdGun(null);
            }
            System.out.println("玩家2是否持枪");
            int   isnoo = in.nextInt();
            if(isnoo==1){
                p2.holdGun(gun2);
           //     p2.shootEnemy(p1);
                p2.loadClip(clip2);
            }else {
                p2.holdGun(null);
            }
            System.out.print("是否继续对战(输入y继续,其他键退出)?");
            if(!(in.next().equals("y"))){
                System.out.println("退出江湖!");
                return;
            }
        }
    }
}



  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值