用java作一个非常简单的游戏:
要求:
1.角色可以向怪物实施攻击,一次攻击后,怪物损失部分HP,当HP损失完后,怪物死亡。
2.角色可装备部同武器,目前有木剑、铁剑、魔剑三种。
3.木剑每次攻击,怪物损失20HP;铁剑每次攻击怪物损失50HP,魔剑每次攻击,怪物损失100HP,并有50%的概率出现暴击。
注意:暴击指攻击效能增加一倍,既魔剑若出现暴击怪物损失200HP。
不能超过三个构造方法,代码70行以内。
要求:
1.角色可以向怪物实施攻击,一次攻击后,怪物损失部分HP,当HP损失完后,怪物死亡。
2.角色可装备部同武器,目前有木剑、铁剑、魔剑三种。
3.木剑每次攻击,怪物损失20HP;铁剑每次攻击怪物损失50HP,魔剑每次攻击,怪物损失100HP,并有50%的概率出现暴击。
注意:暴击指攻击效能增加一倍,既魔剑若出现暴击怪物损失200HP。
不能超过三个构造方法,代码70行以内。
解决方案 »
- 一个错误:web工程可以启动,但试图进入网页时就报这个错。
- 请各们帮帮小弟不知道是什么错误
- jsp 编译流程问题
- Panel类不能使用MouseListener吗?
- 用jsp生成图形验证码的问题,报java.lang.NullPointerException错误
- java实现键盘功能!
- sqlserver2005连接oracle时问题
- tomcoat提问
- 请问:如何用JAVA将图片文件或者MIDI文件转换成二进制文件?
- 时间类转换int
- fusioncharts IE6 显示不了
- JBoss 出错了ERROR [URLDeploymentScanner] Incomplete Deployment listing:
private void show() {
Hero hero = new Hero();
hero.weaponType = 3;
Monster monster = new Monster();
monster.blood = 1000;
while (monster.blood > 0)
attack(hero, monster);
} private void attack(Hero hero, Monster monster) {
String weaponName = "";
int power = 0;
boolean crit = new Random().nextBoolean();
switch (hero.weaponType) {
case 1:
weaponName = "木剑";
power = crit ? 20 * 2 : 20;
break;
case 2:
weaponName = "铁剑";
power = crit ? 50 * 2 : 50;
break;
case 3:
weaponName = "魔剑";
boolean b = new Random().nextBoolean();
power = crit ? 100 * 2 : 100;
break;
default:
break;
}
monster.blood -= power;
if (monster.blood > 0)
System.out.println((crit ? "暴击!" : "") + "玩家使用" + weaponName
+ "对怪物造成了" + power + "点伤害!");
else System.out.println("怪物被击毙");
} class Hero {public int weaponType;} class Monster {public int blood;}
}
Monstor monstor = MonstorFactory.getMonstor();while(monstor.isAlive()){ AttackAttribute attr = hero.attack();
monstor.getattack(attr);}
package test;import java.util.Random;public class Main{ public void show(){
Hero h = new Hero();
Monster monster = new Monster();
monster.blood=1000;
h.attack(0, monster);
}
class Hero {
public String[] weapon={"木剑","铁剑","魔剑"};
public int [] power = {20,50,100};
public void attack(int weaponType,Monster monster){
boolean crit = new Random().nextBoolean();
monster.blood -=power[weaponType];
if (monster.blood > 0){
System.out.println((crit ? "暴击!" : "") + "玩家使用" + weapon[weaponType]
+ "对怪物造成了" + power[weaponType] + "点伤害!");
attack(new Random().nextInt(3),monster);
}else System.out.println("怪物被击毙");
}
}
class Monster {
public int blood;
}
}在 1楼基础上改的
输出结果
玩家使用木剑对怪物造成了20点伤害!
玩家使用魔剑对怪物造成了100点伤害!
暴击!玩家使用木剑对怪物造成了20点伤害!
玩家使用铁剑对怪物造成了50点伤害!
暴击!玩家使用魔剑对怪物造成了100点伤害!
暴击!玩家使用木剑对怪物造成了20点伤害!
暴击!玩家使用木剑对怪物造成了20点伤害!
玩家使用魔剑对怪物造成了100点伤害!
玩家使用木剑对怪物造成了20点伤害!
暴击!玩家使用木剑对怪物造成了20点伤害!
玩家使用木剑对怪物造成了20点伤害!
玩家使用魔剑对怪物造成了100点伤害!
暴击!玩家使用木剑对怪物造成了20点伤害!
玩家使用铁剑对怪物造成了50点伤害!
暴击!玩家使用木剑对怪物造成了20点伤害!
玩家使用铁剑对怪物造成了50点伤害!
玩家使用魔剑对怪物造成了100点伤害!
暴击!玩家使用木剑对怪物造成了20点伤害!
暴击!玩家使用铁剑对怪物造成了50点伤害!
暴击!玩家使用铁剑对怪物造成了50点伤害!
怪物被击毙
Hero h = new Hero();
Monster monster = new Monster();
monster.blood=1000;
h.attack(0, monster);
}
/** 测试
public static void main(String[]a){
Main m = new Main();
m.show();
}
*/
class Hero {
public String[] weapons={"木剑","铁剑","魔剑"};
public int [] powers = {20,50,100};
public void attack(int weaponType,Monster monster){
boolean crit = new Random().nextBoolean();
int power = powers[weaponType];
if(crit)power = powers[weaponType]*2;
monster.blood -=power;
if (monster.blood > 0){
System.out.println((crit ? "暴击!" : "") + "玩家使用" + weapons[weaponType]
+ "对怪物造成了" + power + "点伤害!");
attack(new Random().nextInt(3),monster);
}else System.out.println("怪物被击毙");
}
}
class Monster {
public int blood;
}
}
public class Test3 { public static void main(String[] args){
new Test3().run();
}
public void run(){
Hero hero = new Hero(1);
Monster monster = new Monster(1000);
while(!monster.die()){
hero.attack(monster);
}
System.out.print("怪物被击毙!");
}
}
class Hero{
int weapon = 0;
int heat = 0 ;
double r = 0;
String[] weapons = {"木剑","铁剑","魔剑"};
int[] heats = { 20 , 50 , 100 };
public Hero(int weapon){
this.weapon = weapon;
} public void attack(Monster m){
r = Math.random();
heat = r<0.5 ? heats[weapon] : (heats[weapon]*2) ;
m.blood = m.blood - heat;
if(r>0.5){
System.out.print("暴击!");
}
System.out.println("玩家使用"+weapons[weapon]+"对怪物造成了"+heat+"点伤害!"); }
}
class Monster{
int blood = 0;
public Monster(int blood){
this.blood = blood;
}
public boolean die(){
if(blood<=0){
return true;
}
return false;
}
}
public static void main(String[] args){
System.out.println("暴击!!怪物用魔剑对楼主造成了200点伤害!楼主挂了!!");
}
}后台输出:暴击!!怪物用魔剑对楼主造成了200点伤害!楼主挂了!!
public class Test {
public static void main(String[] args){
System.out.println("怪物出场!!");
System.out.println("楼主出场!!");
System.out.println("必杀,怪物被楼主吓死,怪物挂了!!");
}
}
import java.util.Random;
public class Game2 {
public enum Weapons {
Wood,Iron,Magic;
int Attack;//攻击
int Crit;//暴击
String weaponName;//武器名称
public static Weapons get(String WeaponName) {
if(WeaponName.endsWith("木剑")) {
Wood.Attack = 20;
Wood.weaponName = WeaponName;
return Wood;
}else if (WeaponName.endsWith("铁剑")) {
Iron.Attack = 50;
Iron.weaponName = WeaponName;
return Iron;
}else if (WeaponName.endsWith("魔剑")) {
Magic.Attack = 100;
Magic.Crit = 50;//暴击50%
Magic.weaponName = WeaponName;
return Magic;
}else{
return null;
}
}
}
public class Monster{
int Life;
/**
* 怪物被攻击
* @param AP (int) 受到的攻击数值
*/
public void getAttack(int AP){
this.Life-=AP;
System.out.println("怪物受到了"+AP+"点伤害,剩余"+this.Life+"点生命;");
if(this.Life<=0){
System.out.println("怪物被打倒了!\r\n");
}
}
}
public class Hero{
Weapons weapon;
/**
* 攻击怪物
* @param monster (Monster) 怪物
*/
public void attack(Monster monster){
int isCrit = this.weapon.Crit>new Random().nextInt(100)?1:0;//是否发生暴击
System.out.print((isCrit==1?"暴击!":"")+"英雄使用"+this.weapon.weaponName+"进行攻击,");
monster.getAttack(this.weapon.Attack*(1+isCrit));//怪物被攻击
}
}
/**
* 游戏开始
* @param selectWeapon (Weapons) 英雄装备的武器
*/
public void Start(Weapons selectWeapon){
Hero hero = new Hero();
hero.weapon = selectWeapon;//装备武器
Monster monster = new Monster();
monster.Life = new Random().nextInt(1000)+1000;//生成怪物的生命
System.out.println("游戏开始,英雄使用武器为"+hero.weapon.weaponName+",怪物生命值:"+monster.Life);
do{
hero.attack(monster);//攻击
}while(monster.Life>0);
}
public static void main(String args[]){
Game2 game = new Game2();
game.Start(Weapons.get("木剑"));
game.Start(Weapons.get("铁剑"));
game.Start(Weapons.get("魔剑"));
}
}输出结果:
游戏开始,英雄使用武器为木剑,怪物生命值:1638
英雄使用木剑进行攻击,怪物受到了20点伤害,剩余1618点生命;
.
.
.
英雄使用木剑进行攻击,怪物受到了20点伤害,剩余38点生命;
英雄使用木剑进行攻击,怪物受到了20点伤害,剩余18点生命;
英雄使用木剑进行攻击,怪物受到了20点伤害,剩余-2点生命;
怪物被打倒了!游戏开始,英雄使用武器为铁剑,怪物生命值:1525
英雄使用铁剑进行攻击,怪物受到了50点伤害,剩余1475点生命;
.
.
.
英雄使用铁剑进行攻击,怪物受到了50点伤害,剩余75点生命;
英雄使用铁剑进行攻击,怪物受到了50点伤害,剩余25点生命;
英雄使用铁剑进行攻击,怪物受到了50点伤害,剩余-25点生命;
怪物被打倒了!游戏开始,英雄使用武器为魔剑,怪物生命值:1852
英雄使用魔剑进行攻击,怪物受到了100点伤害,剩余1752点生命;
英雄使用魔剑进行攻击,怪物受到了100点伤害,剩余1652点生命;
英雄使用魔剑进行攻击,怪物受到了100点伤害,剩余1552点生命;
暴击!英雄使用魔剑进行攻击,怪物受到了200点伤害,剩余1352点生命;
英雄使用魔剑进行攻击,怪物受到了100点伤害,剩余1252点生命;
英雄使用魔剑进行攻击,怪物受到了100点伤害,剩余1152点生命;
暴击!英雄使用魔剑进行攻击,怪物受到了200点伤害,剩余952点生命;
暴击!英雄使用魔剑进行攻击,怪物受到了200点伤害,剩余752点生命;
暴击!英雄使用魔剑进行攻击,怪物受到了200点伤害,剩余552点生命;
英雄使用魔剑进行攻击,怪物受到了100点伤害,剩余452点生命;
英雄使用魔剑进行攻击,怪物受到了100点伤害,剩余352点生命;
暴击!英雄使用魔剑进行攻击,怪物受到了200点伤害,剩余152点生命;
暴击!英雄使用魔剑进行攻击,怪物受到了200点伤害,剩余-48点生命;
怪物被打倒了!
package com.wangxing.dfq;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;/*
* 实现水果游戏机的一个简单模拟
*/
public class LuckyMan { //有关本游戏的游戏规则
public static void info(){
System.out.println("*******************************************************");
System.out.println("*幸运苹果机的游戏规则如下: *");
System.out.println("****选择水果种类,每一次只能选一种; * ");
System.out.println("****对应所选的水果押注; * ");
System.out.println("****不同水果的奖励不同; *");
System.out.println("****每一个初始的玩家赠送10个金币; *");
System.out.println("****运转后,若停在你选的水果上面, *");
System.out.println("****则获得奖励;否则损失你押的金币。 *");
System.out.println("******************************************************");
}
//根据ID判断各种水果对应的奖励
public static int unitJudge(char ID){
int unit;
switch(ID){
case 'A': unit = 2;break;
case 'B':unit = 5;break;
case 'C':unit = 10;break;
case 'D':unit = 20;break;
case 'E':unit = 50;break;
case 'F':unit = 100;break;
default:unit = 1;
}
return unit;
}
public static void main(String[] args) {
int coins , i , j , bets , read , rewards , unit;
char choiceID , luckyID;
boolean flag = true;
coins = 10;
bets = 0;
unit = 1;
choiceID = 'N';
luckyID = 'N';
BufferedReader br , bw;
//有关本游戏的相关的信息
info();
while(flag == true){
//统计一下的金币数,用以判断是否有足够的金币来继续游戏
System.out.println(" 你当前的金币总计:¥" + coins);
if(coins <= 0){
System.out.println("你的金币已经用完,游戏结束!");
flag = false;
return;
}
System.out.println("欢迎进入幸运苹果机,新的一轮游戏马上开始!!");
System.out.println("猜中的物品和对应的奖励如下:");
System.out.println("A.苹果—— 2金币");
System.out.println("B.木瓜—— 5金币");
System.out.println("C.西瓜—— 10金币");
System.out.println("D.香蕉—— 20金币");
System.out.println("E.橙子—— 50金币");
System.out.println("F.葡萄—— 100金币");
System.out.println("结束游戏,请按Q");
System.out.println("请输入你选中的水果(输入大写字母A~F):");
try {
br = new BufferedReader(new InputStreamReader(System.in));
choiceID = (char) br.read();
if(choiceID =='Q'){
flag = false;
return;
}
do{
System.out.print("输入你要押的金币数(最多");
System.out.println(coins + "金币):");
bw = new BufferedReader(new InputStreamReader(System.in));
bets = Integer.parseInt(bw.readLine());
if(bets > coins){
System.out.println("你的金币不足!");
flag = false;
}else{
flag = true;
}
}while(flag == false);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("幸运苹果机开始运转.......Good luck !");
for(i = 0 ; i < 8 ; i++){
//空的循环(要一个延迟效果)
for(j = 0 ; j < 55555555 ; j++){}
//获取一个随机数
read = (int)(Math.random()*10);
//每圈指向的是哪一种水果
switch(read){
case 1: System.out.println("~~~苹果~~~");luckyID = 'A';break;
case 2 :System.out.println("~~~木瓜~~~");luckyID = 'B';break;
case 3: System.out.println("~~~西瓜~~~");luckyID = 'C';break;
case 4:System.out.println("~~~香蕉~~~");luckyID = 'D';break;
case 5:System.out.println("~~~橙子~~~");luckyID = 'E';break;
case 6:System.out.println("~~~葡萄~~~");luckyID = 'F';break;
default:System.out.println("~~~水果盘~~~");luckyID = 'N';
}
}
//猜对了
if(choiceID == luckyID){
//调用上面定义好的函数(判断单注的奖励金币数)
unit = unitJudge(luckyID);
//计算实际押注的奖励金币数
rewards = bets*unit;
//支付金币
coins +=rewards;
System.out.print("恭喜你才对啦!幸运之神给你的奖励是");
System.out.println(rewards + "金币!");
}else{
coins -= bets;
System.out.print("很遗憾,你没有猜对........你的损失是");
System.out.println(bets + "金币");
}
System.out.println("本轮游戏结束!!!!-----------------------------------");
}
}
}
和前面那个简单游戏类似
package com.wangxing.dfq;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;/*
* 实现水果机的一个简单模拟
*/
public class LuckyMan { //有关本游戏的游戏规则
public static void info(){
System.out.println("*******************************************************");
System.out.println("*幸运苹果机的游戏规则如下: *");
System.out.println("****选择水果种类,每一次只能选一种; * ");
System.out.println("****对应所选的水果押注; * ");
System.out.println("****不同水果的奖励不同; *");
System.out.println("****每一个初始的玩家赠送10个金币; *");
System.out.println("****运转后,若停在你选的水果上面, *");
System.out.println("****则获得奖励;否则损失你押的金币。 *");
System.out.println("******************************************************");
}
//根据ID判断各种水果对应的奖励
public static int unitJudge(char ID){
int unit;
switch(ID){
case 'A': unit = 2;break;
case 'B':unit = 5;break;
case 'C':unit = 10;break;
case 'D':unit = 20;break;
case 'E':unit = 50;break;
case 'F':unit = 100;break;
default:unit = 1;
}
return unit;
}
public static void main(String[] args) {
int coins , i , j , bets , read , rewards , unit;
char choiceID , luckyID;
boolean flag = true;
coins = 10;
bets = 0;
unit = 1;
choiceID = 'N';
luckyID = 'N';
BufferedReader br , bw;
//有关本游戏的相关的信息
info();
while(flag == true){
//统计一下的金币数,用以判断是否有足够的金币来继续游戏
System.out.println(" 你当前的金币总计:¥" + coins);
if(coins <= 0){
System.out.println("你的金币已经用完,游戏结束!");
flag = false;
return;
}
System.out.println("欢迎进入幸运苹果机,新的一轮游戏马上开始!!");
System.out.println("猜中的物品和对应的奖励如下:");
System.out.println("A.苹果—— 2金币");
System.out.println("B.木瓜—— 5金币");
System.out.println("C.西瓜—— 10金币");
System.out.println("D.香蕉—— 20金币");
System.out.println("E.橙子—— 50金币");
System.out.println("F.葡萄—— 100金币");
System.out.println("结束游戏,请按Q");
System.out.println("请输入你选中的水果(输入大写字母A~F):");
try {
br = new BufferedReader(new InputStreamReader(System.in));
choiceID = (char) br.read();
if(choiceID =='Q'){
flag = false;
return;
}
do{
System.out.print("输入你要押的金币数(最多");
System.out.println(coins + "金币):");
bw = new BufferedReader(new InputStreamReader(System.in));
bets = Integer.parseInt(bw.readLine());
if(bets > coins){
System.out.println("你的金币不足!");
flag = false;
}else{
flag = true;
}
}while(flag == false);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("幸运苹果机开始运转.......Good luck !");
for(i = 0 ; i < 8 ; i++){
//空的循环(要一个延迟效果)
for(j = 0 ; j < 55555555 ; j++){}
//获取一个随机数
read = (int)(Math.random()*10);
//每圈指向的是哪一种水果
switch(read){
case 1: System.out.println("~~~苹果~~~");luckyID = 'A';break;
case 2 :System.out.println("~~~木瓜~~~");luckyID = 'B';break;
case 3: System.out.println("~~~西瓜~~~");luckyID = 'C';break;
case 4:System.out.println("~~~香蕉~~~");luckyID = 'D';break;
case 5:System.out.println("~~~橙子~~~");luckyID = 'E';break;
case 6:System.out.println("~~~葡萄~~~");luckyID = 'F';break;
default:System.out.println("~~~水果盘~~~");luckyID = 'N';
}
}
//猜对了
if(choiceID == luckyID){
//调用上面定义好的函数(判断单注的奖励金币数)
unit = unitJudge(luckyID);
//计算实际押注的奖励金币数
rewards = bets*unit;
//支付金币
coins +=rewards;
System.out.print("恭喜你才对啦!幸运之神给你的奖励是");
System.out.println(rewards + "金币!");
}else{
coins -= bets;
System.out.print("很遗憾,你没有猜对........你的损失是");
System.out.println(bets + "金币");
}
System.out.println("本轮游戏结束!!!!-----------------------------------");
}
}
}
package com.hm;import java.util.Random;
import java.util.Scanner;public class Main { public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请创建角色:");
String name=scanner.next();
System.out.println("请选择武器:\n1:木剑\t2:铁剑\t3:魔剑\t");
Weapon weapon=Weapon.getWeapon(scanner.nextInt());
Hero hero=new Hero(name,weapon);
System.out.println("选择攻击对象:\n1:企鹅\t2:饿狼\t3:黑熊\t");
Monster monster=Monster.getMonstor(scanner.nextInt());
System.out.println("攻击对象:"+monster.monsterName+","+monster.blood+"点血量");
attack(hero,monster);
} static void attack(Hero hero,Monster monster){
while(monster.blood>0){
boolean isCrit = new Random().nextBoolean();
int hurt= isCrit? hero.weapon.power*2 :hero.weapon.power;
monster.blood-=hurt;
System.out.println((isCrit ? "暴击!" : "")+hero.heroName+"使用"+hero.weapon.weaponName+"对"+monster.monsterName
+"造成了"+hurt+"伤害!");
System.out.println("\t"+ (monster.blood>0 ? (monster.monsterName+"还剩下"+monster.blood+"点血量") : monster.monsterName+"被击毙!" ));
}
}
static class Hero {
private String heroName;
private Weapon weapon; public Hero(String heroName, Weapon weapon) {
this.heroName = heroName;
this.weapon = weapon;
}
} static class Weapon {
private String weaponName;
private int power; public Weapon(String wname, int power) {
this.weaponName = wname;
this.power = power;
} public static Weapon getWeapon(int weaponType) {
Weapon weapon = null;
switch (weaponType) {
case 1: weapon=new Weapon("木剑", 20); break;
case 2: weapon=new Weapon("铁剑", 50); break;
case 3: weapon=new Weapon("魔剑", 100); break;
default:break;}
return weapon;
}
} static class Monster{
private String monsterName;
private int blood;
public Monster(String monsterName,int blood){
this.monsterName=monsterName;
this.blood=blood;
}
public static Monster getMonstor(int monstorType){
Monster monster=null;
Random random=new Random();
switch (monstorType) {
case 1: monster=new Monster("企鹅",random.nextInt(200)); break;
case 2: monster=new Monster("饿狼",random.nextInt(500)); break;
case 3: monster=new Monster("黑熊",random.nextInt(800)); break;
default: break; }
return monster;
}
}
}
结构明确,思路清晰,化繁为简,自认为无需注释了,如需要解释请PM我,对此贴不再关注。请创建角色:
a_mean
请选择武器:
1:木剑 2:铁剑 3:魔剑
2
选择攻击对象:
1:企鹅 2:饿狼 3:黑熊
3
攻击对象:黑熊,363点血量
a_mean使用铁剑对黑熊造成了50伤害!
黑熊还剩下313点血量
暴击!a_mean使用铁剑对黑熊造成了100伤害!
黑熊还剩下213点血量
a_mean使用铁剑对黑熊造成了50伤害!
黑熊还剩下163点血量
暴击!a_mean使用铁剑对黑熊造成了100伤害!
黑熊还剩下63点血量
暴击!a_mean使用铁剑对黑熊造成了100伤害!
黑熊被击毙!
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;
}
}其他的武器都实现了Weapon接口,大致与WoodSword相同,只是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;
}
}
}
}
楼主刚培训完C++,刚开始学JAVA
想小现一下
public static void main(String[] args) {
play();
}
static void play(){
Monster monster = new Monster(1500);
Player player = new Player();
int round = 1;
System.out.println("----进入战斗----");
while(monster.getHp() > 0){
System.out.println("第 " + round++ + " 回合");
player.setWeapon((int)(Math.random() * 3 + 1));//用于换武器测试
monster.setHp(monster.getHp() - player.getAttack());
System.out.println();
}
System.out.println("怪物被打倒,游戏结束!");
}
}class Monster{
private int mHp;
public Monster(int hp) {
mHp = hp;
}
public int getHp() {
return mHp;
}
public void setHp(int hp){
mHp = hp;
System.out.print("怪物剩余血量 " + (mHp > 0 ? mHp : 0) + " ");
}
}class Player{
private Weapon mWeapon;
public Player() {
setWeapon(0);
System.out.println();
} public Weapon getmWeapon() {
return mWeapon;
} public void setWeapon(int id) {
mWeapon = Weapon.getWeapon(id);
System.out.print("你装备了 " + mWeapon.getName() + " ");
}
public int getAttack(){
int normalAttack = mWeapon.getAttack();
int extraAttack = (int)(Math.random() * 100) < mWeapon.getProbability() ? 200 : 0;
int attack = normalAttack + extraAttack;
System.out.print("攻击力为 " + attack + (extraAttack > 0 ? " 暴击!!! " : " "));
return attack;
}
}class Weapon{
private String mName;
private int mAttack;
private double mProbability;
private static Weapon quanTou = new Weapon("拳头",1,0.0);
private static Weapon muJian = new Weapon("木剑",20,0.0);
private static Weapon tieJian = new Weapon("铁剑",50,0.0);
private static Weapon moJian = new Weapon("魔剑",100,50.00);
public String getName() {
return mName;
} public int getAttack() {
return mAttack;
} public double getProbability() {
return mProbability;
}
public static Weapon getWeapon(int id) {
switch (id) {
case 1: return muJian;
case 2: return tieJian;
case 3: return moJian;
}
return quanTou;
}
private Weapon(String name,int attack,double probability){
mName = name;
mAttack = attack;
mProbability = probability;
}
}
public static void main(String args[]){
System.out.println("楼主出场");
System.out.println("怪物悲剧");
System.out.println("此据结束,龙套观众继续");
}
* @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));
}}
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));
}}
这个答案不是要你实际,而是看你的设计模式.
需要用策略模式.
// 攻击者
private Role attacker;
// 被攻击者
private Role defender; public GameLogic(String attackType, int attackHp, Arms arms,
String defenderType, int defenderHp) {
attacker = new Role(attackType, attackHp);
attacker.equip(arms);
defender = new Role(defenderType, defenderHp);
} public void start() {
attack(attacker, defender);
} private void attack(Role attack, Role defender) {
Random random = new Random();
while (defender.getHp() > 0) {
// 计算伤害
int hurt = attack.getHurt() * (random.nextBoolean() ? 1 : 2);
if (hurt > defender.getHp())
System.out.println(attack.getType() + "攻击" + defender.getType()
+ " 伤害 :" + defender.getHp());
else
System.out.println(attack.getType() + "攻击" + defender.getType()
+ " 伤害 :" + hurt);
defender.setHp(defender.getHp() - hurt);
}
System.out.println(defender.getType() + "死亡");
} public static void main(String[] args) {
// .....用户 提示选项 确定初始参数 GameLogic logic = new GameLogic("a", 10, new Arms(100, "魔剑"), "d", 1001);
logic.start(); }
}class Role {
private int hp; private String type; private Arms arms; public int getHp() {
return hp;
} public synchronized void setHp(int hp) {
this.hp = hp;
} public String getType() {
return type;
} public Arms getArms() {
return arms;
} public Role(String type, int hp) {
this.type = type;
this.hp = hp;
} public synchronized void equip(Arms arms) {
this.arms = arms;
} public int getHurt() {
// 默认无武器装备时,默认伤害为1
return null == arms ? 1 : arms.getHurt();
}
}class Arms {
private int hurt; private String name; public Arms(int hurt, String name) {
this.hurt = hurt;
this.name = name;
} public String getName() {
return name;
} public int getHurt() {
return hurt;
}
}
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;
}
}
小小代码,请指教。
使用时候倒是3个构造方法Hero hero = new Hero(); //英雄
hero.addWeapon(new Shadowbane());//装备武器
hero.target(new Enemy());//敌人
hero.attack();//攻击public class Hero {
private IWeapon weapon;
private Enemy enemy;
public void addWeapon(IWeapon weapon){
this.weapon = weapon;
if(weapon != null){
System.out.println("你装备了武器:" + weapon.getName());
}
}
public void target(Enemy enemy){
this.enemy = enemy;
if(enemy != null){
System.out.println("你观察着你的敌人");
}
}
private void retarget(){
this.enemy = null;
}
public void attack(){
System.out.println("开始进攻");
if(enemy == null){
System.out.println("无攻击目标,进攻动作结束");
}else if(weapon == null){
System.out.println("未装备武器,救命啊啊啊");
}else{
if(enemy.isDie()){
System.out.println("目标已经死亡,深藏功与名");
}else{
int dam;
do{
dam = weapon.damage();
System.out.println("你发动了进攻,造成" + dam + "点伤害");
enemy.getDamage(dam);
}while(!enemy.isDie());
System.out.println("目标已消灭");
retarget();
}
}
}
}public class Enemy {
private int life = 1000;
public boolean isDie(){
return life <= 0;
}
public void getDamage(int life){
this.life = this.life - life;
this.life = Math.max(0,this.life);
}
}
public interface IWeapon {
public String getName();
public int damage();
}
public class IronSwords implements IWeapon { @Override
public int damage() {
return 50;
} @Override
public String getName() {
return "铁剑";
}}
public class Shadowbane implements IWeapon {
private Random r;
public Shadowbane(){
r = new Random(new Date().getTime());
}
@Override
public int damage() {
boolean doubleDama = r.nextInt(2) == 0;
return doubleDama?100:200;
} @Override
public String getName() {
return "魔剑";
}}
public class WoodenSword implements IWeapon { @Override
public int damage() {
return 20;
} @Override
public String getName() {
return "木剑";
}}
#include <stdlib.h>
#include <time.h>typedef struct _hero{
int weapon;
} hero;typedef struct _monster{
int hp;
} monster;static char* weapons[3] = {"Wood","Iron","Magic"};
static int power[3] = {20,50,100};
static float critical[2] = {1.0f,2.0f};
int main(void)
{
srand(time(NULL));
hero h1;
h1.weapon = rand()%3;
monster m1;
m1.hp = power[h1.weapon] * 6 + rand()%100;
int damage = 0;
while(m1.hp >= 0){
m1.hp -= damage = power[h1.weapon] * critical[rand()%2];
printf("hero uses %s sword to attack the monster cause %d damage.monster left %d.\n",
weapons[h1.weapon],damage,m1.hp);
}
printf("win\n");
return 0;
}
看c多简洁干净
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);
}
}
}
代码60行左右,大家给点意见
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);
}
}对刚才的代码做了下修改··将可选武器设置为final以供运行时调用