简单对战游戏Java版源程序

简单对战游戏Java版

红警大战Java简单版仅供大家交流使用,有用的请点个赞或者留个言,没用的也不要踩啊,谢谢!!!

项目结构如下:
简单对战游戏Java版源程序_第1张图片

GameTest类

import java.util.Scanner;

public class GameTest {//主函数
    public static void main(String[] args) {
        Scanner se = new Scanner(System.in);
        int armNums;//部队数量
        int nums;//我军军队序号
        int numsd;//敌军军队序号

        //设置部队数量
        System.out.println("Welcome!  Game Start!!请输入你需要的部队数量--");
        armNums = se.nextInt();

        Gun [] gun = new Gun [armNums];
        Soldiers [] soldiers = new Soldiers [armNums];//建立我方军队存储数组

        Gun [] gun1 = new Gun [armNums];
        Soldiers [] soldiers1 = new Soldiers [armNums];//建立敌方军队存储数组

        //配置我方部队种类

        for (int i = 0;iout.println("部队序号"+(i+1));
            System.out.println("请输入我方兵种:——————1:手枪兵  2:机枪兵     3:钢炮兵");
            int selectSoMe=se.nextInt();
            System.out.println("请输入我方武器:——————1:手枪      2:机枪         3:钢炮");
            int selectGunMe=se.nextInt();
            gun[i] = Calc.getsGun(selectGunMe,i);
            soldiers[i] = Calc.getsSo(selectSoMe,i);

        }
        //配置敌方部队种类
        for (int i = 0;iout.println("部队序号"+(i+1));
        System.out.println("请输入敌方兵种:——————1:手枪兵  2:机枪兵     3:钢炮兵");
        int selectSoOther=se.nextInt();
        System.out.println("请输入敌方武器:——————1:手枪      2:机枪         3:钢炮");
        int selectGunOther=se.nextInt();
        gun1[i] = Calc.getsGun(selectSoOther,i);
        soldiers1[i] = Calc.getsSo(selectGunOther,i);

        }

        //派出某一士兵对敌方任意士兵进行作战
        System.out.println("请输入我军派出的士兵序号");
        nums = se.nextInt()-1;
        System.out.println("请输入需要攻击敌军的士兵序号");
        numsd = se.nextInt()-1;
        Calc.play(soldiers[nums], gun[nums],soldiers1[numsd],gun1[numsd]);

    }
}

Gun 类


public class Gun {//武器类

    private String name;  //兵器名称
    private int attack; //攻击力
    private int num; //子弹数量
    public Gun(String name, int attack, int num) {
        super();
        this.name = name;
        this.attack = attack;
        this.num = num;
    }


    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAttack() {
        return attack;
    }
    public void setAttack(int attack) {
        this.attack = attack;
    }
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
}

Soldiers类


public class Soldiers {//士兵类

    private String name;  //姓名
    private String arms;  //兵种
    private int blood; //血量

    public Soldiers(String name, String arms, int blood) {
        super();
        this.name = name;
        this.arms = arms;
        this.blood = blood;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getArms() {
        return arms;
    }
    public void setArms(String arms) {
        this.arms = arms;
    }
    public int getBlood() {
        return blood;
    }
    public void setBlood(int blood) {
        this.blood = blood;
    }


}

Calc类

import java.util.Scanner;

public class Calc {// 计算类

    // 创建士兵
    public static Soldiers getsSo(int select, int i) {
        switch (select) {
        case 1:
            return new Soldiers("小兵" + (i + 1) + "号", "手枪兵", 30);
        case 2:
            return new Soldiers("小兵" + (i + 1) + "号", "机枪兵", 40);
        case 3:
            return new Soldiers("小兵" + (i + 1) + "号", "钢炮兵", 50);
        }
        return null;

    }

    // 创建士兵武器
    public static Gun getsGun(int select, int i) {
        switch (select) {
        case 1:
            return new Gun("手枪" + (i + 1) + "号", 10, 30);
        case 2:
            return new Gun("机枪" + (i + 1) + "号", 20, 20);
        case 3:
            return new Gun("钢炮" + (i + 1) + "号", 30, 10);
        }
        return null;

    }

    // 攻击处理
    public static void attack(Soldiers s1, Gun g1, Soldiers s2, Gun g2) {// (攻击方,攻击方攻击力,被攻击方,被攻击方攻击力)
        s2.setBlood(s2.getBlood() - g1.getAttack());
        g1.setNum(g1.getNum() - g1.getAttack());
    }

    // 双方状态显示处理
    public static void show(Soldiers s1, Gun g1, Soldiers s2, Gun g2) {
        System.out.println("我方士兵:\t" + s1.getName() + "\t|兵种: " + s1.getArms() + "\t|所持武器: " + g1.getName()
                + "\t|生命值 : " + s1.getBlood() + "\t|弹药数量 : " + g1.getNum());
        System.out.println("敌方士兵: \t" + s2.getName() + "\t|兵种: " + s2.getArms() + "\t|所持武器: " + g2.getName()
                + "\t|生命值 : " + s2.getBlood() + "\t|弹药数量 : " + g2.getNum() + "\n");
    }

    public static void GameOver() {//游戏结束
        System.out.println("游戏结束");
        Scanner sc = new Scanner(System.in);
        int s = sc.nextInt();
    }

    public static void AddGun(Soldiers g ,Gun s) {//加弹药
        System.out.println("加弹药成功+10,代价为血量减少8");
        s.setNum(s.getNum()+10);
        g.setBlood(g.getBlood()-8);
    }


    public static boolean decide(Soldiers s, Gun p, Soldiers s1, Gun p1) {
        if (s.getBlood() <= 0) {
            System.out.println("我方血量不足");
            GameOver();
        }       
        if (s1.getBlood() <= 0) {
            System.out.println("敌方血量不足");
            GameOver();
        }
        if (p.getNum() <= 0) {
            System.out.println("我方弹药不足");
            AddGun(s1,p);
            return true;
        }
        if (p1.getNum() <= 0) {
            System.out.println("敌方弹药不足");
            AddGun(s,p1);
            return true;
        }
        return true;

    }

    // 游戏开始
    public static void play(Soldiers s, Gun p, Soldiers s1, Gun p1) {

        Scanner sc = new Scanner(System.in);
        System.out.println("派出的两方士兵初始化状态");
        Calc.show(s, p, s1, p1);
        System.out.println("游戏开始,对战中。。。。。。。。。");

        for (int i = 0; i < 16; i++) {// 最多進行16個回合
            while(decide( s,  p,  s1,  p1)){//判断两方弹药以及血量情况
                System.out.println("请按任意键回车进行下一步指令---输入“2”回车敌方进行攻击---输入“1”回车我方进行攻击-----输入“0”回车进行休息-------------");
                int a = sc.nextInt();
            if (a == 1) {// 我方进行攻击
                System.out.println("第" + (i + 1) + "回合状态");
                Calc.attack(s, p, s1, p1);
                Calc.show(s, p, s1, p1);
            } else if (a == 2) {// 敌方进行攻击
                System.out.println("第" + (i + 1) + "回合状态");
                Calc.attack(s1, p1, s, p);
                Calc.show(s1, p1, s, p);
            } else {
                System.out.println("第" + (i + 1) + "回合状态");
                i++;
                Calc.show(s, p, s1, p1);
            }
            }
        }
        GameOver();
    }
}

你可能感兴趣的:(有趣的小项目)