简体   繁体   English

我在哪里可以使用 JS Switch case 语句来转换这些多个 If/Else 语句?

[英]Where can I use the JS Switch case statement to convert these multiple If/Else statements?

Hello and thank you in advance for any assistance or insight.您好,提前感谢您的任何帮助或见解。

I have a Space Battle game I'm working on and I want to know if there is a way to use a JavaScript SWITCH case statement to replace the many If/Else statements I have in my code.我有一个正在开发的 Space Battle 游戏,我想知道是否有办法使用 JavaScript SWITCH 案例语句来替换我在代码中的许多 If/Else 语句。
I don't know what line I would begin at for the switch (expression) .我不知道switch (expression)从哪一行开始。
Would a switch (expression) be appropriate for this program? switch (expression)是否适合该程序?

// Battle Function = Set up a function that "Holds" the battle

let shipsBattle = (ship1, ship2) => {

  let ships = [ship1, ship2]; // put the ships into an array
  let attack = false;
  let attacking = 0;
  let beingAttacked = 1;
  let temp;
  console.log("%c Attack Begins =================", "font-size: 30px");
  while (ships[beingAttacked].hull > 0) { //While the hull is greater than 0...Keep attacking


    // Attacking Sequence

    if (ships[beingAttacked].hull > 0) {

      console.log("\n"); // Console log the attack information
      console.log(
        `%c ${ships[attacking].name} attacked ${ships[beingAttacked].name}`,
        "color: purple; border: 1px solid grey; font-size: 18px;"
      );

      attack = ships[attacking].attack(); // Generate the attack on the enemy ship
      if (attack === true) {
        ships[beingAttacked].hull -= ships[attacking].firePower; //Increase Fire power
        console.log(
          `%c Attack Successful! ${ships[beingAttacked].name} Hull: ${ships[beingAttacked].hull}`,
          "color: green; font-weight: bold; font-size: 16px;"
        );
      } else {
        console.log(
          `%c Attack Unsuccessful! ${ships[beingAttacked].name} Hull: ${ships[beingAttacked].hull}`,
          "color: red; font-size: 16px;"
        );
      }

      if (ships[beingAttacked].hull <= 0) { // Check if the ship being attacked is still alive
        console.log(
          `%c ${ships[beingAttacked].name} has been destroyed`,
          "color: red; border: 1px solid grey; font-size: 16px;"
        );
        if (ships[beingAttacked] === ussSchwartz) {

          alert("Game Over!!!"); //If the USS Ship is being attacked alert player Game is Over
        } else if (
          ships[beingAttacked].name === alienShips[alienShips.length - 1].name
        ) {
          alert(
            `%c ${ships[beingAttacked].name} destroyed!\nAlien fleet has been destroyed!\nyou have been victorious`,
            "color: green;"
          );
        } //If USS destroys alien fleet, then alert player of victory
        else {
          game.userResponse = prompt(
            `${alienShips[game.targetShip].name} destroyed!!\n${
              ussSchwartz.name
            } Hull: ${
              ussSchwartz.hull
            }\nWould you like to ATTACK the next ship or RETREAT from battle?`,
            ""
          );
          game.targetShip += 1; //PROMPT PLAYER IF THEY WANT TO CONTINUE OR RETREAT
          checkUserPrompt();
          return;
        }
      } else {

        temp = attacking; // Change the attacking/attacked ships
        attacking = beingAttacked;
        beingAttacked = temp;
      }
    }
  }
};

It looks to me like you are checking ships[beingAttacked].hull > 0 and then doing some calculations of true and false.在我看来,您正在检查ships[beingAttacked].hull > 0 ,然后进行一些真假计算。 After that you are updating ships[beingAttacked] and doing comparisons on ships[beingAttacked].hull < 0之后,您将更新ships[beingAttacked]并在ships[beingAttacked].hull < 0上进行比较

As such switch doesn't look appropriate as it is usually used when we have something defined and we are not doing different comparisons in each else if.因为这样的开关看起来不合适,因为它通常在我们定义了一些东西并且我们没有在每个 else if 中进行不同的比较时使用。

I guess, what you are trying to do is doing a code cleanup.我想,您要做的是进行代码清理。 I would suggest you:我建议你:

  • Create flow chart of your use cases and try to identify the common code.创建用例的流程图并尝试识别通用代码。 or a logic that can be reused.或者可以重用的逻辑。
  • move that code in separate functions, this will increase the readability and re utilization of code.将该代码移动到单独的函数中,这将增加代码的可读性和再利用。

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

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