Repl.it中的语句崩溃选项卡(P5.js)

问题描述 投票:0回答:4

我有一个项目在27日到期,我遇到了一个问题。每当我启动程序时,Repl.it都会崩溃。如果你查看我的代码,请在第42行:

//getting a number that isnt the players door or the prize door
  while ((randomDoor2 == prizeDoor) || (randomDoor2 == randomDoor)) {
    setTimeout(
      function(){ 
        randomDoor2 = Math.round(random(1,3));
      }, 
      10000);
  }

有一个循环。对此进行评论会使代码完美无缺。

我不知道我该尝试做什么。

它可能是我不同的东西,所以这是我的整个script.js:

var chance; // swap | dont swap
var prizeDoor;
var randomDoor;
var randomDoor2;
var randomDoor3;
var decide;

function setup() {
  chance = 50;
  createCanvas(1000,1000);
}

function draw() {
 //setting up round

  prizeDoor = Math.round(random(1,3));

  //choosing first door

  console.log("[1] [2] [3]");
  randomDoor = Math.round(random(1,3));

  //showing user the door AI picks
var chance = 50; // swap | dont swap
var prizeDoor;
var randomDoor;
var randomDoor2;
var randomDoor3;
var decide;

function setup() {
  chance = 50;
  createCanvas(1000,1000);
}

function draw() {
 //setting up round

  prizeDoor = Math.round(random(1,3));

  //choosing first door

  console.log("[1] [2] [3]");
  randomDoor = Math.round(random(1,3));

  //showing user the door AI picks

  if (randomDoor == 1) {
    console.log(" ^");
    console.log(" |");
  } else if (randomDoor == 2) {
    console.log("     ^");
    console.log("     |");
  } else {
    console.log("         ^");
    console.log("         |");
  }

  console.log("AI chooses door #" + randomDoor + ".");

  //revealing a door

  //getting a number that isnt the players door or the prize door
  while ((randomDoor2 == prizeDoor) || (randomDoor2 == randomDoor)) {
    setTimeout(
      function(){ 
        randomDoor2 = Math.round(random(1,3));
      }, 
      10000);
  }

  //showing this to the user
  console.log("");
  console.log("Door #" + randomDoor2 + " does not have the prize.");

  //having the computer make a desicion
  if (random(0,100) < chance) {
    decide = "swap doors.";
    while ((randomDoor3 !== randomDoor2) || (randomDoor3 !==  randomDoor)) {
      randomDoor3 = Math.round(random(1,3));
    }
  } else {
    decide = "keep the current door.";
  }

  //letting the user know of the computer's desicion
  console.log("");
  console.log("The AI chose to " + decide);

  // figuring out if the AI won
  if (randomDoor3 == prizeDoor || randomDoor == prizeDoor) {

    console.log("AI won!");

    if (decide == "swap doors.") {
      chance -= 5;
    } else {
      chance += 5;
    }
  } else {

    console.log("AI lost.");

    if (decide == "swap doors.") {
      chance += 5;
    } else {
      chance -= 5;
    }
  }
}

我希望while语句能够找到不是所选门的门,也不是带奖品的门,而是它会崩溃。

javascript do-while repl.it
4个回答
0
投票

你正在连续创建两个随机数的门牌和随机门牌,所以使用Math.random在两次连续命中时生成的相同数字很难发生。

在你的while循环中,你期望你的新随机数与你之前的两个随机数相匹配,对于一次命中/编译,它们可以是相同的,或者如果它们不相同则不能使你的while循环永远不会终止,

说a = 1,b = 2

现在期待c == a && c == b将永远不会发生,因为!= b;

在while循环中改变c将没有任何区别。所以你的逻辑是完全错误的,让我们知道你想要实现的目标,然后我们可以帮助建立逻辑

let random = () => Math.random() * 3;
let prizeDoor = Math.round(random());
console.log('prizeDoor', prizeDoor)
let randomDoor = Math.round(random());
console.log('randomDoor', randomDoor)

0
投票

编辑我得到的代码工作确实改变了几位使其在我的结束像math.random一样工作,但不应该很难改变回新的stackoverflow所以花了我一段时间来弄清楚代码片段粘贴:p。

<script type="text/javascript">

var chance; // swap | dont swap
var prizeDoor;
var randomDoor;
var randomDoor2;
var randomDoor3;
var decide;
var chance = 50;
function setup() {
  createCanvas(1000,1000);
}

function draw() {
 //setting up round

  prizeDoor = Math.floor((Math.random() * 3) + 1);
  console.log(prizeDoor+" prizeDoor");

  //choosing first door
  console.log("[1] [2] [3]");
  randomDoor = Math.floor((Math.random() * 3) + 1);
  randomDoor2 = Math.floor((Math.random() * 3) + 1);
  randomDoor3 = Math.floor((Math.random() * 3) + 1);


  //showing user the door AI picks

  if (randomDoor == 1) {
    console.log(" ^");
    console.log(" |");
  } else if (randomDoor == 2) {
    console.log("     ^");
    console.log("     |");
  } else {
    console.log("         ^");
    console.log("         |");
  }



  console.log("AI chooses door #" + randomDoor + ".");


  //revealing a door

  //getting a number that isnt the players door or the prize door
  while ((randomDoor2 == prizeDoor) || (randomDoor2 == randomDoor)) {
   randomDoor2 = Math.floor((Math.random() * 3) + 1);
  }

  //showing this to the user
  console.log("");
  console.log("Door #" + randomDoor2 + " does not have the prize.");

  //having the computer make a desicion
  if (Math.floor((Math.random() * 100) + 1) < chance) {
    decide = "swap doors.";
    while ((randomDoor3 == randomDoor2) || (randomDoor3 == randomDoor)) {
      randomDoor3 = Math.floor((Math.random() * 3) + 1);
    }
  } else {
    randomDoor3 = randomDoor;
    decide = "keep the current door.";
  }

  //letting the user know of the computer's desicion
  console.log("");
  console.log("The AI chose to " + decide);

  // figuring out if the AI won
  if (randomDoor3 == prizeDoor) {

    console.log("AI won!");

    if (decide == "swap doors.") {
      chance -= 5;
    } else {
      chance += 5;
    }
  } else {

    console.log("AI lost.");

    if (decide == "swap doors.") {
      chance += 5;
    } else {
      chance -= 5;
    }
  }
}

draw();
</script>

0
投票

random()是Math对象的一种方法。它通过用Math.random()替换所有random()方法调用来为我工作。


0
投票

我想到了。这实际上非常简单,只需更改一些操作员即可。这是最后一段代码:

var chance = 50; // swap | dont swap
var prizeDoor;
var randomDoor;
var randomDoor2;
var randomDoor3;
var decide;

function setup() {
  chance = 50;
  createCanvas(1000,1000);
}

function draw() {
 //setting up round

  prizeDoor = Math.round(random(1,3));

  //choosing first door

  console.log("[1] [2] [3]");
  randomDoor = Math.round(random(1,3));

  //showing user the door AI picks

  if (randomDoor == 1) {
    console.log(" ^");
    console.log(" |");
  } else if (randomDoor == 2) {
    console.log("     ^");
    console.log("     |");
  } else {
    console.log("         ^");
    console.log("         |");
  }

  console.log("AI chooses door #" + randomDoor + ".");

  //revealing a door

  //getting a number that isnt the players door or the prize door
  while ((randomDoor2 == prizeDoor) || (randomDoor2 == randomDoor)) {
    setTimeout(
      function(){ 
        randomDoor2 = Math.round(random(1,3));
      }, 
      10000);
  }

  //showing this to the user
  console.log("");
  console.log("Door #" + randomDoor2 + " does not have the prize.");

  //having the computer make a desicion
  if (random(0,100) < chance) {
    decide = "swap doors.";
    while ((randomDoor3 == randomDoor2) || (randomDoor3 ==  randomDoor)) {
      randomDoor3 = Math.round(random(1,3));
    }
  } else {
    decide = "keep the current door.";
  }

  //letting the user know of the computer's desicion
  console.log("");
  console.log("The AI chose to " + decide);

  // figuring out if the AI won
  if (randomDoor3 == prizeDoor || randomDoor == prizeDoor) {

    console.log("AI won!");

    if (decide == "swap doors.") {
      chance -= 5;
    } else {
      chance += 5;
    }
  } else {

    console.log("AI lost.");

    if (decide == "swap doors.") {
      chance += 5;
    } else {
      chance -= 5;
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.