面向对象入门(2)

来源:互联网 发布:淘宝复制软件 编辑:程序博客网 时间:2024/09/21 09:20

面向对象入门

练习:模拟奥特曼打小怪兽

package com.ck_study;/** * 奥特曼 * @author ck */public class Ultraman {    private static final int MAX_MP = 100;    private String name;    private int hp;    private int mp;    /**     * 构造器     * @param name  奥特曼的名字     */    public Ultraman(String name) {        this.name = name;        this.hp = 300;        this.mp = MAX_MP;    }    /**     * 攻击     * @param m攻击的目标     */    public void attack(Monster m) {        int injury = (int) (Math.random() * 21 + 10);        m.setHp(m.getHp() - injury);    }    /**     * 究极必杀技     * @param m 小怪兽     */    public void hugeAttack(Monster m) {        m.setHp(m.getHp() - 50);    }    /**     * 魔法攻击     * @param ms  一群小怪兽     */    public void magicAttack(Monster[] ms) {        mp -= 30;        for (int i = 0; i < ms.length; i++) {            if (mp >= 30) {                ms[i].setHp(ms[i].getHp() - 20);            }        }    }    public int getHp() {        return hp;    }    public int getMp() {        return mp >= 30 ? mp : MAX_MP;    }    public void setMp(int mp) {        this.mp = mp;    }    public void setHp(int hp) {        this.hp = hp > 0 ? hp : 0;    }    @Override    public String toString() {        return "奥特曼" + name + "的血量为:" + hp;    }}
package com.ck_study;/** * 小怪兽 * @author ck */public class Monster {    private String name;    private int hp;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getHp() {        return hp;    }    public void setHp(int hp) {        this.hp = hp = hp > 0 ? hp : 0;    }    /**     * 构造器     * @param name 小怪兽的名字     */    public Monster(String name) {        this.name = name;        this.hp = 100;    }    /**     * 攻击     * @param u攻击的目标(谁)     */    public void attack(Ultraman u) {        int injury = (int) (Math.random() * 5 + 10);        u.setHp(u.getHp() - injury);    }    public boolean isDie(Monster[] ms) {        return true;    }    @Override    public String toString() {        return "小怪兽" + name + "的血量为:" + hp;    }}
package com.ck_study;public class Pk {    public static boolean hasAliveMonster(Monster[] ms) {        for (int i = 0; i < ms.length; i++) {            if (ms[i].isDie(ms)) {                return true;            }        }        return false;    }    public static void main(String[] args) {        Ultraman u = new Ultraman("ck");        Monster[] ms = { new Monster("xgs1"), new Monster("xgs2"),                new Monster("xgs3"), new Monster("xgs4"), new Monster("xgs5") };        int round = 1;        do {            System.out.println("=====第" + round++ + "回合=====");            System.out.println("小怪兽攻击奥特曼");            Monster m = null;            do {                int index = (int) (Math.random() * ms.length);                m = ms[index];            } while (!m.isDie(ms));            m.attack(u);            System.out.println(u);            if (u.getHp() > 0) {                u.setMp(u.getMp() + 5);                double rate = Math.random();                if (rate <= 0.8) {                    System.out.println("奥特曼攻击小怪兽");                    u.attack(m);                } else if (rate <= 0.9) {                    System.out.println("奥特曼使用究极必杀技");                    u.attack(m);                } else {                    if (u.getMp() >= 30) {                        System.out.println("奥特曼使用魔法攻击");                        for (int i = 0; i < ms.length; i++) {                            u.magicAttack(ms);                        }                    } else {                        System.out.println("奥特曼使用魔法攻击失败");                    }                }            }            for (Monster temp : ms) {                System.out.println(temp);            }        } while (u.getHp() > 0 && hasAliveMonster(ms));        if (u.getHp() > 0) {            System.out.println("奥特曼胜利!!!");        } else {            System.out.println("小怪兽胜利!!!");        }    }}
0 0
原创粉丝点击