gpt4 book ai didi

Javascript - Math.random 函数在 for 循环中调用时返回相同的结果

转载 作者:行者123 更新时间:2023-11-30 20:52:31 31 4
gpt4 key购买 nike

我有一个 function{computerPlay()} 每次调用它时都会生成一个新的随机数。它从顶部声明的数组中返回一个项目。当我在 console 中测试它时,它按照我希望的方式工作。但是,为了让我的游戏正常运行,我将其置于 for 循环中,并且它会一直返回相同的值,除非我刷新页面。我无法在任何地方找到答案,这是我的第一个问题。我确定代码很草率,所以请忽略它。这是代码。

  let playOptionsArray = ["Rock", "Paper", "Scissors"];
let totalTies = 0;
let playerPoints = 0;
let computerPoints = 0;
let round = 1;

//formats a player's entry so it will match playOptionsArray
function firstLetterUppercase(string){
let firstLetter = string.charAt(0);
let firstLetterCapitalized = firstLetter.toUpperCase();
let restOfResponse = string.slice(1);
let formattedResponse = firstLetterCapitalized + restOfResponse;
return formattedResponse;
}

//compares player entry to ensure it matches an item on the playOptions array
function checkPlayerResponse(response){
if (response === "Rock" || response === "Paper" || response === "Scissors") {
return response;
} else {
return alert("You must choose either 'Rock', 'Paper', or 'Scissor' as your play. Please try again.");
}
}

//generates a random option for the computer's play

function computerPlay() {
let computerSelection = playOptionsArray[Math.floor(Math.random()*(playOptionsArray.length))];
return computerSelection;
}

let computerSelection = computerPlay();

function getPlayerSelection() {
let playerEntry = window.prompt("You are playing the computer in a game of Rock, Paper, Scissors. First player to five points wins. Choose either Rock, Paper, or Scissors as your play", "Rock");
if (playerEntry != null) {
let playerEntryLowercase = playerEntry.toLowerCase();
playerSelection = firstLetterUppercase(playerEntryLowercase);
} else {
getPlayerSelection();
}
checkPlayerResponse(playerSelection);
return playerSelection;
}

let playerSelection = getPlayerSelection;

//emulates a single round of rock paper scissor
function singleRound(){
if (playerSelection === "Rock" && computerSelection === "Scissors"){
alert('You have won this round. The computer chose ' + computerSelection + '.');
playerPoints++;
round++;
} else if (playerSelection === "Scissors" && computerSelection === "Paper"){
alert('You have won this round. The computer chose ' + computerSelection + '.');
playerPoints++;
round++
} else if (playerSelection === "Paper" && computerSelection === "Rock"){
alert('You have won this round. The computer chose ' + computerSelection + '.');
playerPoints++;
round++
} else if (playerSelection === computerSelection){
alert('You have tied this round. The computer chose ' + computerSelection + '.');
totalTies++;
} else {
alert('Something went wrong. Refresh the page.')
}
}

function updateScore() {
document.getElementById("player-points").innerHTML = playerPoints;
document.getElementById("round").innerHTML = round;
document.getElementById("computer-points").innerHTML = computerPoints;
document.getElementById("total-ties").innerHTML = totalTies;
}

//keeps track of player points and ends when a player gets 5 points
function game() {
for (round = 0; computerPoints < 5 && playerPoints < 5; round) {
getPlayerSelection();
computerPlay();
singleRound();
updateScore();
}
}

game();

最佳答案

computerPlay 的返回值不会存储在循环中的任何位置。 (应该注意你的 playerSelection 也会发生这种情况)。尝试

for (round = 0; computerPoints < 5 && playerPoints < 5; round) {
playerSelection = getPlayerSelection();
computerSelection = computerPlay();
singleRound();
updateScore();
}

编辑:@kavakava 的回答提供了一些关于为什么会出现这种情况的见解。

关于Javascript - Math.random 函数在 for 循环中调用时返回相同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48014455/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com