对我来说,编码两个角色之间的实际攻击/防御的最佳方式是什么,以及我如何存储健康值,以便可以存储重新攻击,直到玩家健康或敌人健康达到0,然后宣布胜利者。这是我第一次尝试任何类型的编程后,从不同的来源自学,也请给我反馈任何我可以做的改进,我相信会有很多。
提前谢谢你。
:-)
package test;
public class BattleClass {
public static void main(String[] args) {
PlayerStats ps = new PlayerStats();
EnemyStats es = new EnemyStats();
int eh = es.getEnemyHealth();
int ph = ps.getPlayerHealth();
ps.PlayerAttackDefend();
es.AttackDefend();
System.out.println("You chose to " + ps.getpInput() + " and rolled "
+ ps.getPlayerRoll());
System.out.println("The enemy chose to " + es.getEaod()
+ " and rolled " + es.getEnemyRoll() + ".");
if (ps.getpInput().equals("Attack")) {
if (es.getEaod().equals("Attack")) {
System.out
.println("YOUR SWORDS BOUNCE OFF EACHOUTHERS... TRY AGAIN!");
System.exit(0);
}
if (es.getEaod().equals("Defend")) {
if (ps.getPlayerRoll() > es.getEnemyRoll())
eh -= ps.getPlayerRoll() - es.getEnemyRoll();
System.out.println("Enemy Health is " + eh);
}
}
if (ps.getpInput().equals("Defend")) {
if (es.getEaod().equals("Defend")) {
System.out
.println("YOUR SHIELDS BOUNCE OFF EACHOTHERS... TRY AGAIN!");
System.exit(0);
}
}
if (es.getEaod().equals("Attack")) {
if (es.getEnemyRoll() > ps.getPlayerRoll())
ph -= es.getEnemyRoll() - ps.getPlayerRoll();
System.out.println("Your Health is " + ph);
}
}
}
package test;
import java.util.Random;
import java.util.Scanner;
public class PlayerStats {
static Scanner paod = new Scanner(System.in);
//Players initial health value.
private int playerHealth = 10;
//RNG for attack value / defence value using dice as object.
private int playerRoll = new Random().nextInt(6) + 1;
private String pInput;
//Method for selecting Attack or Defence.
public void PlayerAttackDefend() {
System.out.println("Do you want to Attack or Defend?");
System.out.println("a = Attack / d = Defend");
//Player selects attack or defend.
String userInput = paod.nextLine();
if (userInput.equals("a")) {
pInput = "Attack";
}
if (userInput.equals("d")) {
pInput = "Defend";
}
}
public static Scanner getPaod() {
return paod;
}
public int getPlayerHealth() {
return playerHealth;
}
public int getPlayerRoll() {
return playerRoll;
}
public String getpInput() {
return pInput;
}
public static void setPaod(Scanner paod) {
PlayerStats.paod = paod;
}
public void setPlayerHealth(int playerHealth) {
this.playerHealth = playerHealth;
}
public void setPlayerRoll(int playerRoll) {
this.playerRoll = playerRoll;
}
public void setpInput(String pInput) {
this.pInput = pInput;
}
}
package test;
import java.util.Random;
public class EnemyStats {
//Enemy initial health value.
private int enemyHealth = 10;
//RNG for attack value / defence value using dice as object.
private static int enemyRoll = new Random().nextInt(6) + 1;
//RNG for enemy decision to Attack or Defend.
private static int eAttackDefend = new Random().nextInt(2) + 1;
//Used for returning attack or defend string.
private static String eaod;
//Attack or Defend method.
public void AttackDefend() {
if (eAttackDefend == 1) {
eaod = "Attack";
} else {
eaod = "Defend";
}
}
public int getEnemyHealth() {
return enemyHealth;
}
public int getEnemyRoll() {
return enemyRoll;
}
public int geteAttackDefend() {
return eAttackDefend;
}
public String getEaod() {
return eaod;
}
public void setEnemyHealth(int enemyHealth) {
this.enemyHealth = enemyHealth;
}
public void setEnemyRoll(int enemyRoll) {
EnemyStats.enemyRoll = enemyRoll;
}
public void seteAttackDefend(int eAttackDefend) {
EnemyStats.eAttackDefend = eAttackDefend;
}
public void setEaod(String eaod) {
EnemyStats.eaod = eaod;
}
}
发布于 2014-02-23 03:25:09
一个简单的方法是设置maxHp和actualHp的值,如果你想要“修复”的话。如果你只是减少,直到一个人死了,你就可以减少你已经拥有的实际健康变量。
你可能想从总体上看一下继承,因为你有很多重复的代码。
一般来说,只要做一个循环就可以了
while(ps.getHealth() > 0 && es.getHealth() > 0) {
// your battle code
}
您可能希望删除System.exit(0)
调用,因为它们会终止程序。
向玩家/敌人添加一个dealDamage(int damage)
方法,以便能够实际降低他们的生命值。生命值应该在对象中,而您不应该将它们存储在BattleClass中。
发布于 2014-02-23 03:30:22
我可以给你简短的答案,但我猜你会从详细的解释中得到更多:-)
你想运行你的代码“直到玩家健康或者敌人健康达到0”,所以你需要一个循环。
在java中,你有3种循环:
循环
for(int i=1;i<=3;i++) System.out.println("Hello Musketeer Nr. "+i);
最复杂的循环是for循环,它由三个部分组成:初始化、条件和后处理。虽然可以以不同的方式使用for循环,但它主要是以这里所示的方式使用的,即,您有一个计数器变量,您需要以某种方式使用它的值。
如果不需要计数器变量值,可以对集合和数组使用简写形式:
for(Person p: persons) System.out.println("Hello, "+person.getName()+"!");
while循环是第二个最常用的(至少是被我使用的)循环,它有一个初始条件并进行迭代,只要它是真的。
while(ph>0&&eh>0)
{
...
}
如您所见,它非常适合您的问题。为了完整起见,我将描述第三个循环,即
do-while循环
do
{
...
}
while(ph>0&&eh>0)
您可以像while循环一样使用此循环,但如果您希望至少有一次遍历。
其他备注
为什么战斗系统中有两个类PlayerStats和EnemyStats (它们似乎都有相同的动作和值)?你可以这样做:
Stats playerStats=new Stats();
Stats enemyStats=new Stats();
https://stackoverflow.com/questions/21959098
复制相似问题