67,513
社区成员
发帖
与我相关
我的任务
分享
class Monster {
private int monsterHp;
public Monster(int monsterHp) {
this.monsterHp = monsterHp;
}
public int getMonsterHp() {
return this.monsterHp;
}
public void setMonsterHp(int monsterHp) {
this.monsterHp = monsterHp;
}
public boolean weatherDead() {
if(monsterHp <= 0) return true;
return false;
}
}
//修改部分---------------------------------------------------------
class Weapon {
public static final int WOODENSWORD = 0; //木剑
public static final int IRONNSWORD = 1; //铁剑
public static final int SHADOWBANE = 2; //魔剑
private int attackValue;
public int getAttackValue() {
return this.attackValue;
}
public Weapon(int weaponStyle) {
if(weaponStyle==WOODENSWORD) attackValue = 20; //木剑的攻击力
if(weaponStyle==IRONNSWORD) attackValue = 30; //铁剑攻击力
if(weaponStyle==SHADOWBANE) attackValue = 100;//魔剑攻击力
}
}
class Role {
private Weapon weapon;
public Weapon getWeapon() {
return weapon;
}
public void setWeapon(Weapon weapon) {
this.weapon = weapon;
}
public Role(Weapon weapon) {
this.weapon = weapon;
}
public void attack(Monster monster) { //方法说明:攻击一次,扣除怪物血量一次
int AttackValue = 0;
if(Math.random() > 0.5) { //设置魔剑暴击概率
AttackValue = weapon.getAttackValue()*2; //暴击,丢失200hp
System.out.print("暴击!");
} else {
AttackValue = weapon.getAttackValue(); //普通攻击,丢失100hp
}
int hp = monster.getMonsterHp() - AttackValue;
monster.setMonsterHp(hp);
if(monster.weatherDead()) {
System.out.println("攻击怪物,怪物丢失" + AttackValue + "点血量,剩余血量 0点。怪物已杀死");
} else {
System.out.println("攻击怪物,怪物丢失" + AttackValue + "点血量,剩余血量" + hp + "点");
}
}
public void autoAttack(Monster monster) { //方法说明:调用攻击方法,进行自动攻击,直到怪物死亡
while(!monster.weatherDead()) {
attack(monster);
}
}
}
public class RpgGame {
public static void main(String[] args) {
Monster monster = new Monster(500);
Weapon weapon = new Weapon(Weapon.IRONNSWORD);
Role role = new Role(weapon);
role.autoAttack(monster);
}
}
class Monster {
private int monsterHp;
public Monster(int monsterHp) {
this.monsterHp = monsterHp;
}
public int getMonsterHp() {
return this.monsterHp;
}
public void setMonsterHp(int monsterHp) {
this.monsterHp = monsterHp;
}
public boolean weatherDead() {
if(monsterHp <= 0) return true;
return false;
}
}
class Weapon {
private int attackValue;
public int getAttackValue() {
return this.attackValue;
}
public Weapon(int i) {
if(i<=0) attackValue = 20; //木剑的攻击力
if(i==1) attackValue = 30; //铁剑攻击力
if(i>=2) attackValue = 100;//魔剑攻击力
}
}
class Role {
private Weapon weapon;
public Weapon getWeapon() {
return weapon;
}
public void setWeapon(Weapon weapon) {
this.weapon = weapon;
}
public Role(Weapon weapon) {
this.weapon = weapon;
}
public void attack(Monster monster) { //方法说明:攻击一次,扣除怪物血量一次
int AttackValue = 0;
if(Math.random() > 0.5) { //设置魔剑暴击概率
AttackValue = weapon.getAttackValue()*2; //暴击,丢失200hp
System.out.print("暴击!");
} else {
AttackValue = weapon.getAttackValue(); //普通攻击,丢失100hp
}
int hp = monster.getMonsterHp() - AttackValue;
monster.setMonsterHp(hp);
if(monster.weatherDead()) {
System.out.println("攻击怪物,怪物丢失" + AttackValue + "点血量,剩余血量 0点。怪物已杀死");
} else {
System.out.println("攻击怪物,怪物丢失" + AttackValue + "点血量,剩余血量" + hp + "点");
}
}
public void autoAttack(Monster monster) { //方法说明:调用攻击方法,进行自动攻击,直到怪物死亡
while(!monster.weatherDead()) {
attack(monster);
}
}
}
import java.util.*;
public class GameTest
{
public static void main(String[] args)
{
Player player = new Player();
boolean cont = true;
Scanner input = new Scanner(System.in);
while(cont)
{
Monster monster = new Monster((int)(Math.random() * 1000) + 200);
System.out.println("前方出现一个怪物,开始攻击怪物。。。");
System.out.print("请选择攻击武器(0:木剑 , 1 :铁剑 2:魔剑:)");
while(!(player.attack(monster , input.nextInt())))
{
System.out.print("请选择攻击武器(0:木剑 , 1 :铁剑 2:魔剑:)");
}
System.out.println("怪物被打死,是否继续打怪?: 1:是 2:否");
if(input.nextInt() != 1)
{
cont = false;
}
}
}
}
class Player
{
public boolean attack(Monster monster,int i)
{
return monster.beAttack(i);
}
}
class Monster
{
private int HP = 1000;
public Monster(int hp)
{
HP = hp;
// TODO 自动生成构造函数存根
}
public boolean beAttack(int type)
{
switch(type)
{
case 0:
HP -= 20;
break;
case 1:
HP -= 50;
break;
default:
if((Math.random() * 10) > 8)
{
HP -= 200;
}
else
HP-= 100;
}
if(HP < 0)
{
System.out.println("当前怪物已死亡");
return true;
}
System.out.println("当前怪物血量剩余" + HP);
return false;
}
}
package com.linapex.test;
import java.util.Random;
/**
* @author :LinApex
* @date :2011-11-9上午10:05:42
* @function:
*/
class Monster {
public Monster(int hp) {
this.hp = hp;
}
public int hp;
}
class Hero {
public int weapon; // 武器
private int[] attackArr = { 20, 50, 100 }; // 攻击力
private String[] weaponNameArr = { "木剑", "铁剑", "魔剑" }; // 攻击力
public Hero(int weapon, Monster monster) {
this.weapon = weapon;
this.attack(monster);
}
public void attack(Monster monster) {
while (monster.hp > 0) {
int weaponAttack = attackArr[weapon]; // 得到攻击力
if (weapon == 2) {
boolean crit = new Random().nextBoolean(); // 得到暴击率
weaponAttack = crit ? weaponAttack * 2 : weaponAttack; // 攻击力计算
System.out.print(crit ? "暴击!" : "");
}
monster.hp = monster.hp - weaponAttack < 0 ? 0 : monster.hp - weaponAttack;
System.out.println("玩家使用" + weaponNameArr[weapon] + "攻击怪物(-"
+ weaponAttack + "),怪物剩余血量"+monster.hp);
}
System.out.println("怪物死亡,英雄胜利");
}
}
public class Attack {
public static void main(String[] args) {
new Hero(2, new Monster(1000));
}
}
public static void main(String args[]){
System.out.println("楼主出场");
System.out.println("怪物悲剧");
System.out.println("此据结束,龙套观众继续");
}
//这个是英雄类
public class Hero {
private Weapon weapon;
private String name;
public Hero(String name) {
this.name = name;
}
public void equip(Weapon weapon) {
this.weapon = weapon;
}
public int attack(Monster monster) {
int basicAttack = weapon.getAttack();
int actualAttack = basicAttack * getAttackFactor();
monster.addDamage(actualAttack);
return actualAttack;
}
public String getName() {
return name;
}
//暴击的计算
private int getAttackFactor() {
double rand = Math.random();
int res = 1;
if (rand >= 0.0 && rand < 0.5) {
res = 2;
}
return res;
}
}
//怪物的接口
public interface Monster {
public void addDamage(int damage);
public boolean isAlive();
}
//武器的接口
public interface Weapon {
public int getAttack();
}
//武器:木剑
public class WoodSword implements Weapon{
private final int ATTACK_POINT = 20;
@Override
public int getAttack() {
return ATTACK_POINT;
}
}
//一个普通的怪物
public class CommonMonster implements Monster {
private int heartPoint;
private boolean alive;
public CommonMonster(int hp) {
heartPoint = hp;
alive = true;
}
@Override
public void addDamage(int damage) {
heartPoint -= damage;
if (heartPoint <= 0) {
heartPoint = 0;
alive = false;
}
}
@Override
public boolean isAlive() {
return alive;
}
}
public class TestGame {
public static void main(String[] args) {
Hero hero = new Hero("Hero");
Monster monster = new CommonMonster(1000);
Weapon woodSword = new IronSword();//武器可以换成其他你喜欢的
hero.equip(woodSword);
while(monster.isAlive()) {
int attack = hero.attack(monster);
System.out.println("击掉怪物" + attack + "点血");
if(!monster.isAlive()) {
System.out.println(hero.getName() + "打败了monster");
break;
}
}
}
}