当我在 2 播放器模式下运行我的代码时,函数 rounda 和 roundb 没有响应



当我的代码运行时,我进入 2 玩家模式并调用函数 rounda 和 roundb,但由于某种原因它们没有响应。其他一切正常,但我不知道出了什么问题。提前感谢您提供任何可能的建议。我的代码如下。我调用函数的地方在底部

var a = Math.floor(Math.random() * 7) + 1;
var da = null;
var ga = null;
if (a === 1) {
  da = "Monday";
} else if (a === 2) {
  da = "Tuesday";
} else if (a === 3) {
  da = "Wednesday";
} else if (a === 4) {
  da = "Thursday";
} else if (a === 5) {
  da = "Friday";
} else if (a === 6) {
  da = "Saturday";
} else if (a === 7) {
  da = "Sunday";
}
var play = prompt("Please select either 1 or 2 players! Have at least 5000 credits at the end to win!");
if (play === "1") {
  var wel = confirm("On a stormy " + da + " night, lightning lurks the skies. Will you risk your business for a chance to make millions?");
} else if (play === "2") {
  confirm("On a stormy " + da + " night, lightning lurks the skies. Two electricity distributors compete head to head for dominance over the electric market. Who will reign supreme?");
} else {
  ga = 1;
}
var ezR1 = Math.floor((Math.random() * 2560) + 500);
var ezR2 = Math.floor((Math.random() * 6650) + 500);
var ezR3 = Math.floor((Math.random() * 6670) + 600);
var ezR4 = Math.floor((Math.random() * 7780) + 600);
var ezR5 = Math.floor((Math.random() * 8120) + 700);
var ezR6 = Math.floor((Math.random() * 9540) + 700);
var ezR7 = Math.floor((Math.random() * 1230) + 800);
var ezR8 = Math.floor((Math.random() * 1134) + 800);
var ezR9 = Math.floor((Math.random() * 1223) + 900);
var ezR10 = Math.floor((Math.random() * 1510) + 1000);
var ezR1a = Math.floor((Math.random() * 2560) + 500);
var ezR2a = Math.floor((Math.random() * 6650) + 500);
var ezR3a = Math.floor((Math.random() * 6670) + 600);
var ezR4a = Math.floor((Math.random() * 7780) + 600);
var ezR5a = Math.floor((Math.random() * 8120) + 700);
var ezR6a = Math.floor((Math.random() * 9540) + 700);
var ezR7a = Math.floor((Math.random() * 1230) + 800);
var ezR8a = Math.floor((Math.random() * 1134) + 800);
var ezR9a = Math.floor((Math.random() * 1223) + 900);
var ezR10a = Math.floor((Math.random() * 1510) + 1000);
var ezR1b = Math.floor((Math.random() * 2560) + 500);
var ezR2b = Math.floor((Math.random() * 6650) + 500);
var ezR3b = Math.floor((Math.random() * 6670) + 600);
var ezR4b = Math.floor((Math.random() * 7780) + 600);
var ezR5b = Math.floor((Math.random() * 8120) + 700);
var ezR6b = Math.floor((Math.random() * 9540) + 700);
var ezR7b = Math.floor((Math.random() * 1230) + 800);
var ezR8b = Math.floor((Math.random() * 1134) + 800);
var ezR9b = Math.floor((Math.random() * 1223) + 900);
var ezR10b = Math.floor((Math.random() * 1510) + 1000);
var choozMany = Math.floor((Math.random() * 4) + 1);
var chooz = Math.random();
var amountOfPower = null;
var money = null;
var moneya = null;
var moneyb = null;
var cabels = 5;
var clients = 2;
var bacout = 1;
var one = 1;
var d = 0;
var da = 0;
var db = 0;
var vas = true;
var vasa = true;
var vasb = true;
var cabelsTake = 500 * cabels;
var bacoutTake = 2000 * bacout;
var valu = "a";
var valua = "a";
var valub = "a";
var gunka = true;
var gunkb = true;
var cabelsa = 5;
var clientsa = 2;
var bacouta = 1;
var cabelsb = 5;
var clientsb = 2;
var bacoutb = 1;

function Round(x) {
  alert("Round " + x + " has begun!");
}

function s() {
  console.log(" ");
}

function Tell(a, b, c, d, z, e) {
  s();
  console.log("***********************************");
  if(e === n1 || e === n2){
  console.log("ROUND " + z + "(" + e + ")");
  } else {

   console.log("ROUND " + z);
  }

  console.log("-----------------------------------");
  console.log("Credits: " + a);
  console.log("Power Cabels: " + b);
  console.log("Back Outputters: " + c);
  console.log("Clients: " + d);
  console.log("-----------------------------------");
}

function Strike(x) {
  console.log("⚡ KABOOM! ⚡");
  console.log("Lightning strike power: " + x + " Volts");
}

function blowCable() {
  console.log("Oh No! Some of your cables have blown up! You have " + cabels + " left!");
}

function blowBacout() {
  console.log("Oh No! Some of your backup outputters have blown up! You have " + bacout + " left!");
}
// Logic(cabelsTake, bacoutTake, clients, cabels, ezRX,);
function Logic(a, b, c, d, e) {
  var total = e - a;
  var out = total - b;
  var yon = a - e;
  var cab = 500 * d;
  if (yon > 0) {
    var moneyEarned = e / 2 * c + cab;
    var result = Math.round(moneyEarned);
    money += result;
    console.log("-----------------------------------");
    console.log("Credits Earned: " + result);
    console.log("Cables/Backup Outputters Overridden: None");
    console.log("***********************************");
    s();
  } else {
    console.log(" ");
    console.log("ALERT! ALERT! Too much power!");
    console.log(" ");
    console.log("Attempting to send power to Backup Outputter... ");
    console.log("⚠️BREACH! BREACH! OUTPUTTER INVALID! ATTEMPTING TO RESEND...⚠️");
    console.log("***********************************");
    s();
    if (out < 0) {
      var pen = 3000 + cab;
      money += pen;
      console.log("***********************************");
      console.log("Success! Power diverted.");
      console.log("Credits Earned: " + pen);
      console.log("***********************************");
      s();
    } else {
      console.log("WARNING! Input to large!");
      if (chooz < 0.75 && cabels > 0) {
        cabels -= choozMany;
        blowCable();
        console.log("Cables/Backup Outputters Overridden: " + choozMany);
        console.log("***********************************");
      } else if (chooz > 0.75 && bacout > 0) {
        bacout -= choozMany;
        blowBacout();
        console.log("Cables/Backup Outputters Overridden: " + choozMany);
        console.log("***********************************");
        s();
      }
    }
  }
}
//round('round number', 'ezRX' )
function round(x, y) {
  if (d < 1 && valu === "a") {} else if (valu === "a" && vas === true) {
    alert("Press 'ok' to move on to the next round");
  }
  if (valu === "a" && money > 3000) {
    var shop = prompt("Shop Round " + x + ": 1.Power cable-$3000  2.Backup Outputter-$5000 3. Extra client-$30,000            Credits: " + "$" + money);
    if (shop === "1" && money >= 3000) {
      //cabels += one; works, money -= 1000; works
      cabels += one;
      money -= 3000;
      cabelsTake = 500 * cabels;
      bacoutTake = 2000 * bacout;
      //Round x
      Round(x);
      Tell(money, cabels, bacout, clients, x);
      Strike(y);
      Logic(cabelsTake, bacoutTake, clients, cabels, y);
      d++;
    } else if (shop === "2" && money >= 5000) {
      money -= 5000;
      cabelsTake = 500 * cabels;
      bacoutTake = 2000 * bacout;
      bacout += one;
      //Round x
      Round(x);
      Tell(money, cabels, bacout, clients, x);
      Strike(y);
      Logic(cabelsTake, bacoutTake, clients, cabels, y);
      d++;
    } else if (shop === "3" && money >= 30000) {
      money -= 30000;
      cabelsTake = 500 * cabels;
      bacoutTake = 2000 * bacout;
      clients += one;
      //Round x
      Round(x);
      Tell(money, cabels, bacout, clients, x);
      Strike(y);
      Logic(cabelsTake, bacoutTake, clients, cabels, y);
      d++;
    } else {
      var g = money / 2;
      if (money < 3000) {
        valu = "b";
      } else {
        money -= g;
        console.log("You lost " + g + " credits for skipping a turn.");
        if (money < 3000) {
          console.log("------------------");
          console.log("GAME OVER! Thanks for playing.");
          valu = "b";
          vas = false;
        }
      }
    }
    if (bacout <= 0 || cabels <= 0) {
      valu = "b";
    }
  }
  if (valu === "b" && vas === true) {
    console.log("------------------");
    console.log("GAME OVER! Thanks for playing.");
    valu = "b";
    vas = false;
  }
  if (d === 10) {
    if (money > 5000 && valu === "a") {
      console.log("Congratulations! You have made it through the storm. Your business continues to make millions!");
    } else {
      console.log("Oh No! You don't have enough money! Your business crumbles into chaos!");
    }
    valu = "b";
    vas = false;
  }
}
// Logic(cabelsTake, bacoutTake, clients, cabels, ezRX,);
//-------------------------------------------
function Logica(a, b, c, d, e) {
  var total = e - a;
  var out = total - b;
  var yon = a - e;
  var cab = 500 * d;
  if (yon > 0) {
    var moneyEarned = e / 2 * c + cab;
    var result = Math.round(moneyEarned);
    moneya += result;
    console.log("-----------------------------------");
    console.log("Credits Earned: " + result);
    console.log("Cables/Backup Outputters Overridden: None");
    console.log("***********************************");
    s();
  } else {
    console.log(" ");
    console.log("ALERT! ALERT! Too much power!");
    console.log(" ");
    console.log("Attempting to send power to Backup Outputter... ");
    console.log("⚠️BREACH! BREACH! OUTPUTTER INVALID! ATTEMPTING TO RESEND...⚠️");
    console.log("***********************************");
    s();
    if (out < 0) {
      var pen = 3000 + cab;
      moneya += pen;
      console.log("***********************************");
      console.log("Success! Power diverted.");
      console.log("Credits Earned: " + pen);
      console.log("***********************************");
      s();
    } else {
      console.log("WARNING! Input to large!");
      if (chooz < 0.75 && cabels > 0) {
        cabels -= choozMany;
        blowCable();
        console.log("Cables/Backup Outputters Overridden: " + choozMany);
        console.log("***********************************");
        s();
      } else if (chooz > 0.75 && bacout > 0) {
        bacout -= choozMany;
        blowBacout();
        console.log("Cables/Backup Outputters Overridden: " + choozMany);
        console.log("***********************************");
        s();
      }
    }
  }
}

//round('round number', 'ezRX' )
function rounda(x, y) {
  if (da < 1 && valua === "a") {} else if (valua === "a" && vasa === true && valub === true) {
    alert("Press 'ok' to move on to the next round");
  }
  if (valua === "a" && moneya > 3000 && valub === "a") {
    nexd(n1);
    var shopa = prompt("Shop Round " + x + ": 1.Power cable-$3000  2.Backup Outputter-$5000 3. Extra client-$30,000            Credits: " + "$" + moneya);
    if (shopa === "1" && moneya >= 3000) {
      //cabels += one; works, money -= 1000; works
      cabelsa += one;
      moneya -= 3000;
      cabelsTake = 500 * cabelsa;
      bacoutTake = 2000 * bacouta;
      //Round x
      Round(x);
      Tell(moneya, cabelsa, bacouta, clientsa, x, n1);
      Strike(y);
      Logica(cabelsTake, bacoutTake, clientsa, cabelsa, y);
      da++;
    } else if (shopa === "2" && moneya >= 5000) {
      moneya -= 5000;
      cabelsTake = 500 * cabelsa;
      bacoutTake = 2000 * bacouta;
      bacouta += one;
      //Round x
      Round(x);
      Tell(moneya, cabelsa, bacouta, clientsa, x, n1);
      Strike(y);
      Logica(cabelsTake, bacoutTake, clientsa, cabelsa, y);
      da++;
    } else if (shopa === "3" && moneya >= 30000) {
      moneya -= 30000;
      cabelsTake = 500 * cabelsa;
      bacoutTake = 2000 * bacouta;
      clientsa += one;
      //Round x
      Round(x);
      Tell(moneya, cabelsa, bacouta, clientsa, x, n1);
      Strike(y);
      Logica(cabelsTake, bacoutTake, clientsa, cabelsa, y);
      da++;
    } else {
      var g = moneya / 2;
      if (g < 3000 && gunka === true && gunkb === true) {
        console.log(n1 + " Eliminated!");
        console.log(n2 + " wins!");
        gunka = false;
        gunkb = false;
        valua = "b";
        valub = "b";
      } else {
        moneya -= g;
        console.log("You lost " + g + " credits for skipping a turn!");
      }
    }
  } else {}
  //Prototype
  var tosta = moneya + cabelsTake + bacoutTake + clientsa * 100;
  var tostb = moneyb + cabelsTake + bacoutTake + clientsb * 100;
  if (bacouta <= 0 || cabelsa <= 0 && gunka === true) {
    console.log(n1 + " Eliminated!");
    console.log(n2 + " wins with a total of " + tostb + " Points!");
    gunka = false;
    gunkb = false;
    valua = "b";
    valub = "b";
  }
  if (bacoutb <= 0 || cabelsb <= 0 && gunkb === true) {
    console.log(n2 + " Eliminated!");
    console.log(n1 + " wins with a total of " + tosta + " Points!");
    gunka = false;
    gunkb = false;
    valua = "b";
    valub = "b";
  }

}

//round('round number', 'ezRX' )
function roundb(x, y) {
  if (db < 1 && valub === "a") {} else if (valua === "a" && vasb === true && valub === true) {
    alert("Press 'ok' to move on to the next round");
  }
  if (valua === "a" && moneyb > 3000 && valub === "a") {
    nexd(n2);
    var shopb = prompt("Shop Round " + x + ": 1.Power cable-$3000  2.Backup Outputter-$5000 3. Extra client-$30,000            Credits: " + "$" + moneya);
    if (shopb === "1" && moneyb >= 3000) {
      //cabels += one; works, money -= 1000; works
      cabelsb += one;
      moneyb -= 3000;
      cabelsTake = 500 * cabelsb;
      bacoutTake = 2000 * bacoutb;
      //Round x
      Round(x);
      Tell(moneyb, cabelb, bacoutb, clientsb, x, n2);
      Strike(y);
      Logica(cabelsTake, bacoutTake, clientsb, cabelsb, y);
      db++;
    } else if (shopb === "2" && moneyb >= 5000) {
      moneyb -= 5000;
      cabelsTake = 500 * cabelsb;
      bacoutTake = 2000 * bacoutb;
      bacoutb += one;
      //Round x
      Round(x);
      Tell(moneyb, cabelb, bacoutb, clientsb, x, n2);
      Strike(y);
      Logica(cabelsTake, bacoutTake, clientsb, cabelsb, y);
      db++;
    } else if (shopb === "3" && moneyb >= 30000) {
      moneyb -= 30000;
      cabelsTake = 500 * cabelsb;
      bacoutTake = 2000 * bacoutb;
      clientsb += one;
      //Round x
      Round(x);
      Tell(moneyb, cabelb, bacoutb, clientsb, x, n2);
      Strike(y);
      Logica(cabelsTake, bacoutTake, clientsb, cabelsb, y);
      db++;
    } else {
      var g = moneyb / 2;
      if (g < 3000 && gunka === true && gunkb === true) {
        console.log(n2 + " Eliminated!");
        console.log(n1 + " wins!");
        gunka = false;
        gunkb = false;
        valua = "b";
        valub = "b";
      } else {
        moneyab -= g;
        console.log("You lost " + g + " credits for skipping a turn!");
      }
    }
  } else {}
  //Prototype
  var tosta = moneya + cabelsTake + bacoutTake + clientsa * 100;
  var tostb = moneyb + cabelsTake + bacoutTake + clientsb * 100;
  if (bacoutb <= 0 || cabelsb <= 0 && gunkb === true) {
    console.log(n2 + " Eliminated!");
    console.log(n1 + " wins with a total of " + tosta + " Points!");
    gunka = false;
    gunkb = false;
    valua = "b";
    valub = "b";
  }
  if (bacouta <= 0 || cabelsa <= 0 && gunka === true) {
    console.log(n1 + " Eliminated!");
    console.log(n2 + " wins with a total of " + tostb + " Points!");
    gunka = false;
    gunkb = false;
    valua = "b";
    valub = "b";
  }
  if (db === 10) {
    if (valua === "a" && valub === "a") {

        compare(tosta, tostb, n1, n2);
    } else {}
  }
}



if (play === "1") {
  //Start rounds
  money = 15000;
  round(1, ezR1);
  round(2, ezR2);
  round(3, ezR3);
  round(4, ezR4);
  round(5, ezR5);
  round(6, ezR6);
  round(7, ezR7);
  round(8, ezR8);
  round(9, ezR9);
  round(10, ezR10);
} else if (play === "2") {

var check = alert("Welcome to 2 player mode!");

    var n1 = prompt("Input Player 1's username.");
    var n2 = prompt("Input Player 2's username.");

    rounda(1, ezR1a );
    roundb(1, ezR1b );

    rounda(2, ezR2a );
    roundb(2, ezR2b );

    rounda(3, ezR3a );
    roundb(3, ezR3b );

    rounda(4, ezR4a );
    roundb(4, ezR4b );

    rounda(5, ezR5a );
    roundb(5, ezR5b );

    rounda(6, ezR6a );
    roundb(6, ezR6b );

    rounda(7, ezR7a );
    roundb(7, ezR7b );

    rounda(8, ezR8a );
    roundb(8, ezR8b );

    rounda(9, ezR9a );
    roundb(9, ezR9b );

    rounda(10, ezR10a );
    roundb(10, ezR10b );



} else {
  console.log("You didn't select a valid amount of players.");
}

你有 2 个错误

首先,您没有为 moneya 和 moneyb 设置值,因此,如果您比较货币的第二个值总是返回 false。 然后是没有定义nexd()(第485,609行)的问题。

这段代码有太多的 if,变量的命名不是很好。 我清理了一下顶部,给你一些想法

https://jsfiddle.net/ujn6uq5r/

var dayOfWeek = Math.floor(Math.random() * 7) + 1,
    dateLookup = {
        1: "Monday",
        2: "Tuesday",
        3: "Wednesday",
        4: "Thursday",
        5: "Friday",
        6: "Saturday",
        7: "Sunday"
    },
    da = dateLookup[dayOfWeek],
    ga = null;

最新更新