我正在制作一个基本的石头剪刀游戏。到目前为止,这个程序看起来很顺利,它可以检测到播放器和电脑之间的选择,但得分并没有正确上升。如果玩家连续多次获胜,我将无法获得高于1的分数,如果有平局,则分数变为0。
计算机选择由随机数决定,并在下面调用该函数时输入playerSelection。
如果玩家选择摇滚并且计算机选择纸张,则第一次,控制台会说""玩家获胜且分数为1&#34 ;.
如果第二轮玩家选择剪刀并且计算机选择纸张,则控制台会说"玩家获胜并且分数 1 "当我期望它说""玩家获胜并且分数 2 "
function computerPlay() {
const random = Math.ceil(Math.random() * 3);
if (random === 1) {
return "rock";
}
else if (random === 2) {
return "paper";
}
else {
return "scissors";
}
}
let score = 0;
function playRound(playerSelection, computerSelection) {
// Computer wins
if ((playerSelection === 'paper' && computerSelection === 'scissors') ||
(playerSelection === 'scissors' && computerSelection === 'rock') ||
(playerSelection === 'rock' && computerSelection === 'paper')) {
score--;
//Stop negative scores
if (score < 0) {
score = 0;
}
return "Computer wins and the score is " +score;
}
// Player wins
else if ((playerSelection === 'paper' && computerSelection === 'rock')||
(playerSelection === 'rock' && computerSelection === 'scissors') ||
playerSelection === 'scissors' && computerSelection === 'paper') {
score++;
return "Player wins and the score is " +score;
}
// Same selection
else {
return "Tie and the score is " +score;
}
}
console.log(playRound('paper', computerPlay()));
答案 0 :(得分:2)
好的,所以你的问题是你没有任何逻辑来保持游戏“会话”的进行。意思是,(根据我现在在你的帖子中看到的内容)每个游戏回合都是独家的,结果不是每一轮都是复合的。要解决这个问题,你需要实现一个游戏循环来控制游戏流程,并允许每个会话多轮。
话虽如此,这就是我想出来并进行了一些测试来帮助你:
function computerPlay () {
const options = [ 'rock', 'paper', 'scissors' ];
return options[Math.floor(Math.random()*options.length)];
}
function computerWins ( playerSelection, computerSelection ) {
return ( playerSelection === 'paper' && computerSelection === 'scissors' ) ||
( playerSelection === 'scissors' && computerSelection === 'rock' ) ||
( playerSelection === 'rock' && computerSelection === 'paper' );
}
function playerWins ( playerSelection, computerSelection ) {
return ( playerSelection === 'paper' && computerSelection === 'rock' ) ||
( playerSelection === 'rock' && computerSelection === 'scissors' ) ||
playerSelection === 'scissors' && computerSelection === 'paper';
}
function playRound ( score, playerSelection, computerSelection ) {
let result = {};
// Computer wins
if ( computerWins( playerSelection, computerSelection ) ) {
score--;
//Stop negative scores
if ( score < 0 ) {
score = 0;
}
result = { message : 'Computer wins, and the score is: ', score };
}
// Player wins
else if ( playerWins( playerSelection, computerSelection ) ) {
score++;
result = { message : 'Player wins and the score is: ', score };
}
// Same selection
else {
result = { message : 'Tie game and the score is: ', score };
}
return result;
}
function annouceWinner ( score, message ) {
console.log( `${message} ${score}` );
}
function main () {
let score = 0;
while ( score < 5 ) {
let roundResult = playRound( score, 'paper', computerPlay() );
score = roundResult.score;
annouceWinner( score, roundResult.message );
}
}
main();
您会注意到我创建了一些实用程序方法,并将其整体清理干净。
computerWins
方法来保存确定计算机何时获胜的逻辑。这很好,因为如果出于某种原因这个逻辑需要重构,那只需要在这一个方法中完成!playerWins
方法来保存确定玩家何时获胜的逻辑。announceWinner
方法。这不一定是必需的,但是这允许您轻松地从主循环功能中删除消息传递部分。main
方法。这是心脏,它控制程序的流程,并允许您每次会话有多个“轮次”。示例输出:
Computer wins, and the score is: 1
Tie game and the score is: 1
Player wins and the score is: 2
Player wins and the score is: 3
Computer wins, and the score is: 2
Tie game and the score is: 2
Player wins and the score is: 3
Player wins and the score is: 4
Player wins and the score is: 5