简体   繁体   English

如何更改实质上创建空轮的 switch 语句?

[英]How to alter a switch statement that essentially creates empty rounds?

I am trying to make a four-person attack game that randomly selects the attacker and prints out the winner, but in my output, I keep getting empty rounds.我正在尝试制作一个四人攻击游戏,随机选择攻击者并打印出获胜者,但在我的 output 中,我不断得到空回合。 I believe this is due to my switch statement but am unsure how to get the same result minus these essentially empty rounds.我相信这是由于我的 switch 语句,但我不确定如何获得相同的结果减去这些基本上是空的回合。 I have created an array of my characters to put into my randomly generated switch statement but with the if statements inside the cases are not skipped how I envisioned them being skipped.我已经创建了一个字符数组来放入我随机生成的 switch 语句中,但是 case 中的 if 语句并没有像我想象的那样被跳过。 I've also tried removing the players from the array as they die, but that does not work and results in an error and exception message.我还尝试在玩家死亡时将其从阵列中移除,但这不起作用并导致错误和异常消息。

  import project3.characters.Plumber;
  import project3.characters.Vampire;
  import project3.characters.Werewolf;

  import java.util.ArrayList;
  import java.util.Random;

  public class Game {

  public static void main(String[] args) {
      GameCharacter player1 = new Plumber("Mario", 10, 20);
      GameCharacter player2 = new Fairy("Tinker Bell", 10, 20);
      GameCharacter player3 = new Vampire("Edward", 10, 20);
      GameCharacter player4 = new Werewolf("Derek", 10, 20);

      int round = 0;

      ArrayList<GameCharacter> player = new ArrayList<GameCharacter>. 
      ();

      player.add(player1);
      player.add(player2);
      player.add(player3);
      player.add(player4);

      while (player1.isAlive() || player2.isAlive() || 
          player3.isAlive() || player4.isAlive()) {
          System.out.println("Round " + (round + 1) + ": ");
          System.out.print(" " + player1 + " ");
          System.out.println(" " + player2);
          System.out.print(" " + player3 + " ");
          System.out.println(" " + player4 + " ");
          System.out.println();

          // Only two players can battle
          // if (round % 2 == 0) {
          // player1.hit(player2.attack());
          // } else
          // player2.hit(player1.attack())

          // All players randomly attack each other
          Random rand = new Random();
          int turn = rand.nextInt(12);
          switch (turn) {
          case 0:
              if (player1.isAlive() && player2.isAlive())
                  player1.hit(player2.attack());
              break;
          case 1:
              if (player1.isAlive() && player3.isAlive())
                  player1.hit(player3.attack());
              break;
          case 2:
              if (player1.isAlive() && player4.isAlive())
                  player1.hit(player4.attack());
              break;
          case 3:
              if (player1.isAlive() && player2.isAlive())
                  player2.hit(player1.attack());
              break;
          case 4:
              if (player3.isAlive() && player2.isAlive())
                  player2.hit(player3.attack());
              break;
          case 5:
              if (player4.isAlive() && player2.isAlive())
                  player2.hit(player4.attack());
              break;
          case 6:
              if (player1.isAlive() && player3.isAlive())
                  player3.hit(player1.attack());
              break;
          case 7:
              if (player3.isAlive() && player2.isAlive())
                  player3.hit(player2.attack());
              break;
          case 8:
              if (player3.isAlive() && player4.isAlive())
                  player3.hit(player4.attack());
              break;
          case 9:
              if (player1.isAlive() && player4.isAlive())
                  player4.hit(player1.attack());
              break;
          case 10:
              if (player4.isAlive() && player2.isAlive())
                  player4.hit(player2.attack());
              break;
          case 11:
              if (player3.isAlive() && player4.isAlive())
                  player4.hit(player3.attack());
              break;
          }

          if (!player1.isAlive()) {
              System.out.println(player1.getName() + " is dead!");
              // player.remove(0);
              // player1 = null
          }
          if (!player2.isAlive()) {
              System.out.println(player2.getName() + " is dead!");
              // player.remove(1);
              // player2 = null
          }
          if (!player3.isAlive()) {
              System.out.println(player3.getName() + " is dead!");
              // player.remove(2);
              // player3 = null
          }
          if (!player4.isAlive()) {
              System.out.println(player4.getName() + " is dead!");
              // player.remove(3);
              // player4 = null
          }

          if (!player4.isAlive() && !player2.isAlive() && 
              !player3.isAlive()) {
              System.out.println(player1.getName() + " is the 
           winner!");
              break;
          }
          if (!player4.isAlive() && !player1.isAlive() && 
             !player3.isAlive()) {
              System.out.println(player2.getName() + " is the 
           winner!");
              break;
          }
          if (!player4.isAlive() && !player2.isAlive() && 
          !player1.isAlive()) {
              System.out.println(player3.getName() + " is the 
         winner!");
              break;
          }
          if (!player1.isAlive() && !player2.isAlive() && 
          !player3.isAlive()) {
              System.out.println(player4.getName() + " is the 
        winner!");
              break;
          }
          
          
          round++;
          System.out.println();
          }
      }
  }

One way could be to maintain a flag if fight happens.如果发生战斗,一种方法可能是保持旗帜。 Like this:像这样:

    boolean didFightHappen = true;
    while (any player alive) {
        //anounce round only if didFightHappen = true;
        // round 1 etc
        
        Random rand = new Random();
        int turn = rand.nextInt(12);
        didFightHappen = false;  // make flag false
        switch (turn) {
        case 0:
            //if fight happens then set
            // didFightHappen = true;
            break;
        }
        // at end of the switch
        if(didFightHappen == false) {
            continue; // this continues the while loop to next iteration without executing rest of the loop code
        }
        
    }

Here is the tiny version of your game:p这是您游戏的小版本:p

List<Integer> l = new ArrayList<Integer>();
l.add(1); l.add(2); l.add(3); l.add(4);
        
Collections.shuffle(l);
while(l.size()>1) {
    int a = l.remove(0); 
    int b = l.get(0);
    System.out.println("Player " + b + " attacked player " + a );
    System.out.println(a + " died!");
}
System.out.println("Player " + l.get(0) + " is the winner!");

Output: Output:

Player 3 attacked player 4
4 died!
Player 1 attacked player 3
3 died!
Player 2 attacked player 1
1 died!
Playe 2 is the winner!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM