java simple simulation player

package Baoji;

public class Player {
    
    
    /**
     * 玩家类
     *  属性:名字,类型,血量,防御,攻击
     *  行为:自我介绍、pk
     */
    //封装:将属性设置为private 提供公共的get和set方法间接访问
    private String name;//名字
    private String type;//玩家类型
    private int life;//生命
    private int defense;//防御
    private int attack;//攻击力

    //介绍
    public void say(){
    
    
        System.out.println("我叫"+name+",是一个"+type+",我的生命值"+life+",防御"+defense+",攻击力"+attack);
    }

    /**
     * pk的方法,和另一个玩家pk
     * @param
     */
    public void pk(Player p){
    
       //对手
        //定义一个标记,0代表我方进攻,1代表对方进攻
        int flag = 0;//默认我方先进攻
        //回合制pk,直到一方死亡
        while (true){
    
    
            //每次显示剩余的生命值
            this.say();
            p.say();

            if (flag==0){
    
    
                //我方进攻:敌方生命值-(我方攻击力-敌方防御力)
                int harm = (this.attack-p.defense);//得伤害
                //暴击:伤害翻倍
                int sj = (int) Math.round(Math.random()*(2-1)+1);
                if (sj ==2){
    
    
                    System.out.println(p.name+"被暴击了!掉血"+harm*2);
                }else{
    
    
                    System.out.println(p.name+"掉血"+harm);
                }
                p.setLife(p.life-harm*sj);//敌方掉血
                flag = 1;//改变进攻
            }else {
    
    
                //敌方进攻=(敌方攻击力-我方防御力)
                int harm = p.attack-this.defense;
                int sj = (int) Math.round(Math.random()*(10-1)+1);
                if (sj == 3 || sj == 9){
    
    //如果随机的是3或9表示暴击
                    System.out.println(this.name+"被暴击了!掉血"+harm*2);
                    this.setLife(this.life-harm*2);//我掉血

                }else {
    
    
                    System.out.println(this.life+"掉血"+harm);
                    this.setLife(this.life-harm);//我掉血
                    flag = 0;//改变进攻方

                }

                //判别血量
                if (this.life<=0){
    
    
                    System.out.println(this.name+"被KO了");
                    break;//倒下停止战斗
                }
                if (p.life<=0){
    
    
                    System.out.println(p.name+"被ko了");
                    break;
                }
            }

        }
    }

    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 getDefense() {
    
    
        return defense;
    }

    public void setDefense(int defense) {
    
    
        this.defense = defense;
    }

    public int getAttack() {
    
    
        return attack;
    }

    public void setAttack(int attack) {
    
    
        this.attack = attack;
    }
}

package Baoji;

public class PlayerTest {
    
    
    public static void main(String[] args) {
    
    
        //创建玩家对象
        Player p1 = new Player();
        p1.setName("龙");
        p1.setType("战士");
        p1.setLife(100);
        p1.setDefense(20);
        p1.setAttack(30);

        Player p2 = new Player();
        p2.setName("天");
        p2.setType("法师");
        p2.setLife(100);
        p2.setDefense(15);
        p2.setAttack(50);

        //开始pk
        p1.pk(p2);
    }
}

Guess you like

Origin blog.csdn.net/BO2345/article/details/125696650