gpt4 book ai didi

javascript - 具有获胜条件的循环基本 Javascript 游戏

转载 作者:行者123 更新时间:2023-11-28 03:41:31 24 4
gpt4 key购买 nike

如何循环石头剪刀布游戏并让它在获胜条件下保持得分?

我尝试创建一个“游戏”功能,循环 RPS 轮次,直到玩家或计算机达到 5 分。但是,我无法让分数保持不变,游戏也不会循环。

playerSelection = prompt( ' Enter Rock, Paper, or Scissors');
let winner = 0;
let humanScore = 0;
let computerScore = 0;

function computerPlay() {
let number = Math.floor(Math.random() * (3 + 1));
if(number == 1)
return 'Rock';
else if(number == 2)
return 'Paper';
else return 'Scissors';
}

let computerSelection = computerPlay();
playerSelection = playerSelection.toUpperCase();
computerSelection = computerSelection.toUpperCase();



function game() {
while( humanScore <= 5 || computerScore <= 5) {
playRound();

}
}


function playRound(playerSelection, computerSelection) {
if( playerSelection === 'ROCK' && computerSelection === 'SCISSORS') {
humanScore +=1;
return playerSelection + ' wins vs ' + computerSelection + '. Congratulations!';
} else if( playerSelection === 'ROCK' && computerSelection === 'PAPER') {
computerScore +=2;
return playerSelection + ' loses vs ' + computerSelection + '. Try again!';
} else if( playerSelection === 'ROCK' && computerSelection === 'ROCK') {
return playerSelection + ' ties with ' + computerSelection + '. Try again!';
} else if( playerSelection === 'PAPER' && computerSelection === 'ROCK') {
humanScore +=1;
return playerSelection + ' wins vs ' + computerSelection + '. Congratulations!';
} else if( playerSelection === 'PAPER' && computerSelection === 'PAPER') {
return playerSelection + ' ties with ' + computerSelection + '. Try again!';
} else if( playerSelection === 'PAPER' && computerSelection === 'SCISSORS') {
computerScore +=2;
return playerSelection + ' loses vs ' + computerSelection + '. Try again!';
} else if( playerSelection === 'SCISSORS' && computerSelection === 'SCISSORS') {
return playerSelection + ' ties with ' + computerSelection + '. Try again!';
} else if( playerSelection === 'SCISSORS' && computerSelection === 'PAPER') {
humanScore += 1;
return playerSelection + ' wins vs ' +computerSelection + '. Congratulations!';
} else if( playerSelection === 'SCISSORS' && computerSelection === 'ROCK') {
computerScore +=2;
return playerSelection + ' loses vs ' + computerSelection + '. Try again!';
} else return 'Enter a valid move! Refresh the damn page!';

win_condition();
}

function win_condition() {
if( humanScore === 5 ) {
return 'Player Wins!';
}
if( computerScore === 5 ) {
return 'Computer wins!';
}
}

该程序执行一次并有效,但不会像我期望的那样重复。

最佳答案

您的代码中存在一些问题,如评论部分中突出显示的那样:

  1. 函数 win_conditiongame从来没有被调用过。
  2. 函数 win_conditionplayGround返回处理后的值,但被接受。
  3. if( humanScore === 5 )if( computerScore === 5 )总是假的。处理条件为humanScore <= 5 || computerScore <= 5
  4. if(humanScore <= 5 || computerScore <= 5)即使玩家得分超过 5,此操作也会失败。

let winner = 0;
let humanScore = 0;
let computerScore = 0;

function initializeGame() {
let playerSelection = prompt(' Enter Rock, Paper, or Scissors');
let computerSelection = computerPlay();

let msg = playRound(playerSelection, computerSelection);
console.log(msg);
if (humanScore <= 5 && computerScore <= 5) {
console.log('Current score: Human: ', humanScore, ' Computer: ', computerScore)
setTimeout(initializeGame, 0);
} else {
console.log(win_condition());
}
}

function computerPlay() {
let number = Math.floor(Math.random() * (3 + 1));
if (number == 1)
return 'Rock';
else if (number == 2)
return 'Paper';
else return 'Scissors';
}

function playRound(playerSelection, computerSelection) {
playerSelection = playerSelection.toUpperCase();
computerSelection = computerSelection.toUpperCase();
if (playerSelection === 'ROCK' && computerSelection === 'SCISSORS') {
humanScore += 1;
return playerSelection + ' wins vs ' + computerSelection + '. Congratulations!';
} else if (playerSelection === 'ROCK' && computerSelection === 'PAPER') {
computerScore += 2;
return playerSelection + ' loses vs ' + computerSelection + '. Try again!';
} else if (playerSelection === 'ROCK' && computerSelection === 'ROCK') {
return playerSelection + ' ties with ' + computerSelection + '. Try again!';
} else if (playerSelection === 'PAPER' && computerSelection === 'ROCK') {
humanScore += 1;
return playerSelection + ' wins vs ' + computerSelection + '. Congratulations!';
} else if (playerSelection === 'PAPER' && computerSelection === 'PAPER') {
return playerSelection + ' ties with ' + computerSelection + '. Try again!';
} else if (playerSelection === 'PAPER' && computerSelection === 'SCISSORS') {
computerScore += 2;
return playerSelection + ' loses vs ' + computerSelection + '. Try again!';
} else if (playerSelection === 'SCISSORS' && computerSelection === 'SCISSORS') {
return playerSelection + ' ties with ' + computerSelection + '. Try again!';
} else if (playerSelection === 'SCISSORS' && computerSelection === 'PAPER') {
humanScore += 1;
return playerSelection + ' wins vs ' + computerSelection + '. Congratulations!';
} else if (playerSelection === 'SCISSORS' && computerSelection === 'ROCK') {
computerScore += 2;
return playerSelection + ' loses vs ' + computerSelection + '. Try again!';
} else return 'Enter a valid move! Refresh the damn page!';
}

function win_condition() {
if (humanScore > 5) {
return 'Player Wins!';
}
if (computerScore > 5) {
return 'Computer wins!';
}
}

initializeGame();

关于javascript - 具有获胜条件的循环基本 Javascript 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57267005/

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