功能一覽:
實現自由打怪,自動打怪,自動升級,支持升級後的屬性增加。
package com.game.huntervsmonster05;
public class TestGme
{
static int i;
public static void main(String[] args)
{/*
* Hunter hunter = new Hunter(1); for (i = 1; i < 10; i++) { Monster monster
* = new Monster(i); System.out.println(hunter.name + "VS" + monster.type);
* hunter.fight(monster); System.out.println("戰鬥結束"); // monster.show();
* hunter.show(); if (!hunter.isLive) { return; } }
*/
new Game().start(new Hunter(3));
}
}
package com.game.huntervsmonster05;
public class Monster
{
String type;
int maxLife;
int actualLife;
boolean isLive;
int attack;
int defend;
int experience;
int nimble;
public Monster(int i)
{
switch (i)
{
case 1:
{
this.type = "小殭屍";
this.attack = 80;
this.defend = 20;
this.isLive = true;
this.maxLife = 200;
this.actualLife = maxLife;
this.experience = 100;
this.nimble = 5;
}
break;
case 2:
{
this.type = "大殭屍";
this.attack = 130;
this.defend = 50;
this.isLive = true;
this.maxLife = 500;
this.actualLife = maxLife;
this.experience = 200;
this.nimble = 10;
}
break;
case 3:
{
this.type = "殭屍老大";
this.attack = 200;
this.defend = 90;
this.isLive = true;
this.maxLife = 1000;
this.actualLife = maxLife;
this.experience = 300;
this.nimble = 13;
}
break;
case 4:
{
this.type = "小鬼";
this.attack = 210;
this.defend = 100;
this.isLive = true;
this.maxLife = 1200;
this.actualLife = maxLife;
this.experience = 320;
this.nimble = 20;
}
break;
case 5:
{
this.type = "老鬼";
this.attack = 350;
this.defend = 150;
this.isLive = true;
this.maxLife = 2000;
this.actualLife = maxLife;
this.experience = 400;
this.nimble = 25;
}
break;
case 6:
{
this.type = "火鬼王";
this.attack = 500;
this.defend = 200;
this.isLive = true;
this.maxLife = 5000;
this.actualLife = maxLife;
this.experience = 500;
this.nimble = 30;
}
break;
case 7:
{
this.type = "黑無常";
this.attack = 600;
this.defend = 250;
this.isLive = true;
this.maxLife = 8000;
this.actualLife = maxLife;
this.experience = 800;
this.nimble = 35;
}
break;
case 8:
{
this.type = "白無常";
this.attack = 600;
this.defend = 250;
this.isLive = true;
this.maxLife = 8000;
this.actualLife = maxLife;
this.experience = 800;
this.nimble = 40;
}
break;
case 9:
{
this.type = "閻王";
this.attack = 900;
this.defend = 300;
this.isLive = true;
this.maxLife = 10000;
this.actualLife = maxLife;
this.experience = 1000;
this.nimble = 50;
}
break;
}
}
public void fight(Hunter hunter)
{
if (!isLive)
{
return;
}
hunter.injured(this);
}
public void injured(Hunter hunter)
{
int randomNimble = 0;
randomNimble = randomNumber();
if (randomNimble >this.nimble)
{
if (hunter.attack >= this.defend)
this.actualLife = this.actualLife
- (hunter.attack - this.defend);
}
else
System.out.println(this.type + "躲避成功");
if (this.actualLife <= 0)
{
this.dead(hunter);
return;
}
this.show();
this.fight(hunter);
}
void dead(Hunter hunter)
{
this.isLive = false;
hunter.changeExperience(this);
}
int randomNumber()
{
int number = 0;
number = (int) (Math.random() * 100);
return number;
}
void show()
{
if (this.isLive)
System.out.println(this.type + "還有" + this.actualLife + "點血");
else
System.out.println(this.type + "被殺死了!");
}
}
package com.game.huntervsmonster05;
public class GameUtil
{
public static int retrunRandom(int start, int end)
{
while (true)
{
int number = 0;
number = (int) (Math.random() * 10);
if (number >= start && number <= end)
return number;
}
}
public static int retrunBigRandom(int start, int end)
{
while (true)
{
int number = 0;
number = (int) (Math.random() * 100);
if (number >= start && number <=end)
return number;
}
}
public static int retrunLongRandom(int start, int end)
{
while (true)
{
int number = 0;
number = (int) (Math.random() * 100);
if (number >= start && number <=end)
return number;
}
}
}
package com.game.huntervsmonster05;
public class Game
{
Monster m1;
Monster m2;
Monster m3;
Monster m4;
Monster m5;
Monster m6;
Monster m7;
Monster m8;
Monster m9;
public Game()
{
/*
* Hunter hunter=new Hunter(1); Monster monster1=new Monster(1); Monster
* monster2=new Monster(2); Monster monster3=new Monster(3); Monster
* monster4=new Monster(4); Monster monster5=new Monster(5); Monster
* monster6=new Monster(6); Monster monster7=new Monster(7); Monster
* monster8=new Monster(8); Monster monster9=new Monster(9);
*/
m1 = new Monster(1);
m2 = new Monster(2);
m3 = new Monster(3);
m4 = new Monster(4);
m5 = new Monster(5);
m6 = new Monster(6);
m7 = new Monster(7);
m8 = new Monster(8);
m9 = new Monster(9);
}
void start(Hunter hunter)
{
while (m1.isLive || m2.isLive || m3.isLive || m4.isLive || m5.isLive
|| m6.isLive || m7.isLive || m8.isLive || m9.isLive)
{
if (hunter.isLive)
{
int number = GameUtil.retrunRandom(1, 9);
switch (number)
{
case 1:
hunter.fight(m1);
break;
case 2:
hunter.fight(m2);
break;
case 3:
hunter.fight(m3);
break;
case 4:
hunter.fight(m4);
break;
case 5:
hunter.fight(m5);
break;
case 6:
hunter.fight(m6);
break;
case 7:
hunter.fight(m7);
break;
case 8:
hunter.fight(m8);
break;
case 9:
hunter.fight(m9);
break;
}
}
else
break;
}
end(hunter);
}
void end(Hunter hunter)
{
if (hunter.isLive)
{
System.out.println(hunter.name + "宇內無敵!");
}
else
System.out.println("嗚呼,天妒英才!");
}
}
package com.game.huntervsmonster05;
public class Hunter
{
String name;
int maxLife;
int actualLife;
int level;
int attack;
int defend;
int experience;
String weapon;
boolean isLive;
int nimble;
public Hunter(int i)
{
switch (i)
{
case 1:
{
this.name = "李逍遙";
this.attack = 100;
this.defend = 40;
this.isLive = true;
this.maxLife = 200;
this.weapon = "無塵劍";
this.actualLife = maxLife;
this.experience = 0;
this.level = 1;
this.nimble = 90;
}
break;
case 2:
{
this.name = "景天";
this.attack = 500;
this.defend = 200;
this.isLive = true;
this.maxLife = 1000;
this.weapon = "魔劍";
this.actualLife = maxLife;
this.experience = 0;
this.level = 1;
this.nimble = 45;
}
break;
case 3:
{
this.name = "孫悟空";
this.attack = 1000;
this.defend = 400;
this.isLive = true;
this.maxLife = 3000;
this.weapon = "金箍棒";
this.actualLife = maxLife;
this.experience = 0;
this.level = 1;
this.nimble = 50;
}
break;
}
}
void fight(Monster monster)
{
if (!isLive)
{
return;
}
monster.injured(this);
}
void injured(Monster monster)
{
int randomNimble = 0;
randomNimble = randomNumber();
if (randomNimble >this.nimble)
{
if (monster.attack >= this.defend)
this.actualLife = this.actualLife
- (monster.attack - this.defend);
}
else
System.out.println(this.name + "躲避成功");
if (this.actualLife <= 0)
{
this.dead();
return;
}
this.show();
this.fight(monster);
}
void dead()
{
this.isLive = false;
}
void changeExperience(Monster moster)
{
this.experience += moster.experience;
while (this.experience >= (this.level * 200))
{
upgrade();
}
}
void upgrade()
{
this.level += 1;
this.attack *= 1.5;
this.defend *= 1.25;
this.actualLife = (int) (this.maxLife * 1.5);
this.maxLife *= 1.5;
this.nimble += 2;
System.out.println(this.name + ":" + this.level);
}
void show()
{
if (this.isLive)
System.out.println(this.name + "還有" + this.actualLife + "點血");
else
{
System.out.println(this.name + "壯烈犧牲了!");
return;
}
}
int randomNumber()
{
int number = 0;
number = (int) (Math.random() * 100);
return number;
}
}