奥特曼打小怪兽

 1 /**
 2  * OUTMAN附
 3  * 
 4  * @author 王启文
 5  *
 6  */
 7 public class One {
 8     private String name;
 9     private int hp;
10     private int mp;
11     private int damage;
12     private int defense;
13     private int intelligence;
14 
15     public One(String name, int hp, int mp, int damage, int defense,
16             int intelligence) {
17         this.name = name;
18         this.hp = hp;
19         this.mp = mp;
20         this.damage = damage;
21         this.defense = defense;
22         this.intelligence = intelligence;
23     }
24 
25     public String getName() {
26         return this.name;
27     }
28 
29     public boolean isAlive() {
30         return this.hp > 0;
31     }
32 
33     public boolean hasMp() {
34         return this.mp > 0;
35     }
36 
37     public String getInfo() {
38         return "姓名: " + name + "\n生命值: " + hp + "\t" + "魔法值: " + mp;
39     }
40 
41     public void physicsAttack(One other) {
42         int effectiveDamage = (int) (Math.random() * damage / 5 + 4 * damage / 5);
43         int effectiveDefense = (int) (Math.random() * defense / 4 + 3 * defense / 4);
44         if (effectiveDamage - effectiveDefense > 0) {
45             other.hp -= effectiveDamage - effectiveDefense;
46             if (other.hp <= 0) {
47                 other.hp = 0;
48             }
49         } else {
50             other.hp -= 1;
51         }
52     }
53 
54     public void magicAttack(One other) {
55         int effectiveMagicAttack = (int) (Math.random() * intelligence / 2 + intelligence);
56         other.hp -= effectiveMagicAttack;
57         mp -= 40;
58         if (other.hp <= 0) {
59             other.hp = 0;
60         }
61         if (mp <= 0) {
62             mp = 0;
63         }
64     }
65 
66     public void outburst() {
67         double sz = Math.random();
68         if (sz < 0.1 && this.hp < hp / 10) {
69             this.hp *= 2;
70             this.damage *= 2;
71             this.defense *= 0.5;
72         }
73     }
74 
75     // public boolean escape(){
76     // double sc = Math.random();
77     // if (this.hp<hp/10&&sc<0.1) {
78     // this.hp=0;
79     // }
80     // return false;
81     // }
82 }
 1 /**
 2  * OUTMAN
 3  * 
 4  * @author 王启文
 5  *
 6  */
 7 public class fight {
 8     public static void main(String[] args) {
 9         One a1 = new One("Ultraman", 600, 280, 99, 55, 120);
10         One a2 = new One("Monster", 1200, 200, 78, 70, 100);
11 
12         int times = 1;
13 
14         System.out.println(a1.getInfo());
15         System.out.println(a2.getInfo());
16 
17         while (a1.isAlive() && a2.isAlive()) {
18             System.out.println("第" + times + "次");
19             double a = Math.random();
20             if (a < 0.8) {
21                 a1.physicsAttack(a2);
22                 if (a2.isAlive()) {
23                     double b = Math.random();
24                     if (b < 0.8) {
25                         a2.physicsAttack(a1);
26                     } else {
27                         if (a2.hasMp()) {
28                             a2.magicAttack(a1);
29                         } else
30                             a2.physicsAttack(a1);
31                     }
32                 }
33             } else {
34                 if (a1.hasMp()) {
35                     a1.magicAttack(a2);
36                     if (a2.isAlive()) {
37                         double b = Math.random();
38                         if (b < 0.8) {
39                             a2.physicsAttack(a1);
40                         } else {
41                             if (a2.hasMp()) {
42                                 a2.magicAttack(a1);
43                             } else {
44                                 a2.physicsAttack(a1);
45                             }
46                         }
47                     }
48                 } else {
49                     a1.physicsAttack(a2);
50                     if (a2.isAlive()) {
51                         double b = Math.random();
52                         if (b < 0.8) {
53                             a2.physicsAttack(a1);
54                         } else {
55                             if (a2.hasMp()) {
56                                 a2.magicAttack(a1);
57                             } else
58                                 a2.physicsAttack(a1);
59                         }
60                     }
61                 }
62             }
63 
64             System.out.println(a1.getInfo());
65             System.out.println(a2.getInfo());
66             System.out.println("----------------");
67             times++;
68         }
69 
70         if (a1.isAlive()) {
71             System.out.println(a1.getName() + "获胜!!!");
72         } else {
73             System.out.println(a2.getName() + "获胜!!!");
74         }
75     }
76 }

未完待续......

posted @ 2014-10-26 20:47  Wqw道  阅读(226)  评论(0编辑  收藏  举报