dimanche 28 février 2021

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. I believe this is due to my switch statement but am unsure how to get the same result minus these essentially empty rounds. 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. 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();
          }
      }
  }

Aucun commentaire:

Enregistrer un commentaire