【问题标题】:Two Players Fighting With Multiple Attack Options Java使用多种攻击选项 Java 战斗的两名玩家
【发布时间】:2014-06-05 00:00:16
【问题描述】:

我正在尝试创建一个有两个战士的游戏。战士对角斗士。 两个战士都有1000的生命值。战斗开始时,玩家 1 可以在三个选项中进行选择:1 - 刺击 2 - 切片 3 - 喝药水......

假设玩家 1 选择推力并攻击 55(使用随机),然后我的代码打印玩家 2 的新生命值,现在是 945。然后轮到玩家 2,假设他做了完全相同的事情并从玩家身上拿走一些生命值1.

现在它会回到玩家 1 的回合。这就是复杂的地方。现在玩家 1 进行第二次攻击,假设他造成 80 点伤害。它将打印出玩家二有 920 生命值。它没有减去之前的攻击。我希望他们战斗,直到其中一个生命值降至 0 并死亡。

那么我该如何修复我的代码,以便在每次攻击后健康状况不会在 1000 处重新启动,并且它会从之前的攻击中已经降低的新健康状况中减去。像这样:1000 hp - 55 = 945 hp - 80 = 865 hp 等等。我是编码新手。一月开始。任何帮助都会很棒!

import java.util.Random;

public class Duel 
{
Random hit = new Random();
Random hit1 = new Random();

int newHealth, newHealth1;
int outcome, outcome1, outcome2, outcome3;
int attack, attack1;
int defense, defense1;
int health, health1;

void calculateWinner()
{
  do
  { 
     outcome = hit1.nextInt(100) - hit1.nextInt(15);
     newHealth1 = health1 - outcome;

     System.out.println("Your attack does " + outcome + " damage!");
     System.out.println("");
     System.out.print("Warrior Health: " + newHealth1 + "\n");
     break;
  }     

  while(newHealth1 == 0);
  //System.out.println("Gladiator is the winner!");

}

void calculateWinner1()
{
  do
  { 
     outcome1 = hit.nextInt(100) - hit.nextInt(15);
     newHealth = health - outcome1;

     System.out.println("Your attack does " + outcome1 + " damage!");
     System.out.println("");
     System.out.print("Gladiator Health: " + newHealth + "\n\n");
     break;
  }     

  while(newHealth == 0);
  //System.out.println("Warrior is the winner!");
}

void calculateHealth()
{
  do
  { 
     newHealth = health + hit.nextInt(35);
     System.out.println("You drink the potion.");
     System.out.println("Your health is now at " + newHealth + "!");
     break;
  }     

  while(newHealth1 > 0 && newHealth > 0);
}

 void calculateHealth1()
{
  do
  { 
     newHealth1 = health1 + hit.nextInt(35);
     System.out.println("You drink the potion.");
     System.out.println("Your health is now at " + newHealth1 + "!");
     break;
  }     

  while(newHealth1 > 0 && newHealth > 0);
}
}


import java.util.Random;
import java.util.Scanner;

public class DuelMain 
{
public static void main(String[] args) 
{
  // Random + Scanner
  Random hit = new Random();
  Random hit1 = new Random();
  Scanner input = new Scanner(System.in);

  // String + Int
  String player1name = "";
  String player2name = "";
  int restart;
  int player1option;
  int player2option;
  int fight = 0;

  Duel warrior = new Duel();
  Duel gladiator = new Duel();

  gladiator.attack = hit.nextInt(100);
  gladiator.defense = hit.nextInt(15);
  gladiator.health = 1000;

  warrior.attack1 = hit1.nextInt(100);
  warrior.defense1 = hit1.nextInt(15);;
  warrior.health1 = 1000;

  // Printing Names
  System.out.print("Gladiator Enter Name: ");
  player1name = input.nextLine();

  System.out.print("Warrior Enter Name: ");
  player2name = input.nextLine();

  while(fight == 0)
  {
     // Choose Moves (Player1)
     System.out.printf("%n%s, Choose Your Move! \n", player1name);
     System.out.println("1: Thrust 2: Slice 3: Drink Potion");
     player1option = input.nextInt();

     if(player1option == 1)
     {
        System.out.printf("You Thrust Your Sword At %s! \n", player2name);

        warrior.attack1 = hit1.nextInt(100);
        warrior.defense1 = hit1.nextInt(15);
        warrior.health1 = 1000;
        warrior.calculateWinner();
     }

     if(player1option == 2)
     {
        System.out.printf("You Slice Your Sword At %s! \n", player2name);

        warrior.attack1 = hit1.nextInt(100);
        warrior.defense1 = hit1.nextInt(15);
        warrior.health1 = 1000;
        warrior.calculateWinner();
     }

     if(player1option == 3)
     {
        warrior.calculateHealth();
     }

     // Choose Moves (Player2)
     System.out.printf("%n%s, Choose Your Move! \n", player2name);
     System.out.println("1: Thrust 2: Slice 3: Drink Potion");
     player2option = input.nextInt();

     if(player2option == 1)
     {
        System.out.printf("You Thrust Your Sword At %s! \n", player1name);

        gladiator.attack = hit.nextInt(100);
        gladiator.defense = hit.nextInt(15);
        gladiator.health = 1000;
        gladiator.calculateWinner1();
     }

     if(player2option == 2)
     {
        System.out.printf("You Slice Your Sword At %s! \n", player1name);

        gladiator.attack = hit.nextInt(100);
        gladiator.defense = hit.nextInt(15);
        gladiator.health = 1000;
        gladiator.calculateWinner1();;
     }

     if(player2option == 3)
     {
        gladiator.calculateHealth1();
     }
  }        
}
}     

【问题讨论】:

  • 删除security标签考虑到这个问题绝对没有任何关系。

标签: java


【解决方案1】:

if(playerXoption == N) 之后的块内,您将使用warrior.health1 = 1000gladiator.health = 1000 将角色生命值重置为1000。删除这些行应该允许健康继续被进一步的攻击降低而不被重置。

【讨论】:

    【解决方案2】:

    很难为您指明任何方向,但我强烈建议您再次阅读 Java 和面向目标的编程教程。无论如何,我会尽量缩短。

    首先,你需要为你的战士设置一个职业。每个战士都将是这个类的一个实例。他们每个人都有属性:生命值、伤害和防御(假设伤害将保持不变)。

    public class Warrior {
    public Warrior(final int health, final int damage, final int defense) {
        this.health = health;
        this.damage = damage;
        this.defense = defense;
    }
    
    public int getHealth() {
        return health;
    }
    
    public void setHealth(int health) {
        this.health = health;
    }
    
    public int getDamage() {
        return damage;
    }
    
    public void setDamage(int damage) {
        this.damage = damage;
    }
    
    public int getDefense() {
        return defense;
    }
    
    public void setDefense(int defense) {
        this.defense = defense;
    }
    
    private int health;
    private int damage;
    private int defense;
    

    }

    然后我们需要一个类来处理决斗。为了简单起见,它将是相当硬编码的。基本上它只会让两个战士互相殴打直到一个倒下。

    public class Duel {
    private Warrior warrior;
    private Warrior gladiator;
    
    public Duel() {
        warrior = new Warrior(1000, 100, 20);
        gladiator = new Warrior(1000, 80, 30);
    }
    
    public int doDuel() {
        while (true) {
            if (attack(warrior, gladiator)) {
                return 1;
            } else if (attack(gladiator, warrior)) {
                return 2;
            }
        }
    }        
    
    public boolean attack(final Warrior attacker, final Warrior defender) {
        defender.setHealth(defender.getHealth() - (attacker.getDamage() - defender.getDefense()));
        if (defender.getHealth() <= 0) {
            return true;
        }
        return false;
    }
    

    }

    在主调用中:

    final Duel duel = new Duel();
    if(duel.doDuel() == 1) {
        // warrior won
    } else {
       //  gladiator won
    }
    

    请记住,这是非常幼稚和简单的实现。有数百万种方法可以实现这样的事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-24
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 2020-05-18
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      相关资源
      最近更新 更多