石头,剪刀,布 – 记分困难(浏览器控制台)

huangapple go评论57阅读模式
英文:

Rock, Paper, Scissor - Trouble keeping score (browser console)

问题

I'm doing an Odin Project (JavaScript Basic): Rock, Paper, Scissors only in the browser console and I'm having trouble keeping score. I'm still a beginner, so excuse my messy code. I've tried adding another function to keep the score, but it didn't work either.

choices = ['rock', 'paper', 'scissors']
let playerScore = 0
let computerScore = 0

function getComputerChoice() {
    return choices[Math.floor(Math.random() * choices.length)]
}

function getPlayerChoice() {
    let input = prompt('Pick Rock, Paper, or Scissor?')
    input = input.toLowerCase()
    return input
}

function playRound(playerSelection, computerSelection) {
    if (playerSelection === 'paper' && computerSelection === 'scissors' || 
        playerSelection === 'scissors' && computerSelection === 'rock' || 
        playerSelection === 'rock' && computerSelection === 'paper') {
        computerScore++
        return `You lose! ${computerSelection} beats ${playerSelection}`
        
    } else if (playerSelection === 'rock' && computerSelection === 'scissors' || 
               playerSelection === 'paper' && computerSelection === 'rock' || 
               playerSelection === 'scissors' && computerSelection === 'paper') {
        playerScore++
        return `You win! ${playerSelection} beats ${computerSelection}`
    } else
        return `It's a tie!`
    
}

function game() {
    for (let i = 1; i <= 5; i++) {
        const playerSelection = getPlayerChoice();
        const computerSelection = getComputerChoice();
        console.log(playRound(playerSelection, computerSelection));
    }

    if (playerScore > computerScore) {
        return 'You beat the computer! You are a genius'
    } else if (playerScore < computerScore) {
        return 'You got beat by the computer. Practice your throws!'
    } else {
        return 'You tied with the computer!'
    }  
}

game();

(Note: I've translated the code portion as per your request.)

英文:

I'm doing an Odin Porject (Javascript Basic): Rock, Paper, Scissor only in the browser console and I'm having trouble keeping score. I'm still a beginner so excuse my messy code. I've tried adding another function to keep the score but it didn't work either.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

choices = [&#39;rock&#39;, &#39;paper&#39;, &#39;scissors&#39;]
let playerScore = 0
let computerScore = 0
function getComputerChoice() {
return choices[Math.floor(Math.random() * choices.length)]
}
function getPlayerChoice() {
let input = prompt(&#39;Pick Rock, Paper, or Scissor?&#39;)
input = input.toLowerCase()
return input
}
function playRound(playerSelection, computerSelection) {
if (playerSelection === &#39;paper&#39; &amp;&amp; computerSelection === &#39;scissors&#39; || 
playerSelection === &#39;scissors&#39; &amp;&amp; computerSelection === &#39;rock&#39; || 
playerSelection === &#39;rock&#39; &amp;&amp; computerSelection === &#39;paper&#39;) {
computerScore++
return `You lose! ${computerSelection} beats ${playerSelection}`
} else if (playerSelection === &#39;rock&#39; &amp;&amp; computerSelection === &#39;scissors&#39; || 
playerSelection === &#39;paper&#39; &amp;&amp; computerSelection === &#39;rock&#39; || 
playerSelection === &#39;scissors&#39; &amp;&amp; computerSelection === &#39;paper&#39;) {
playerScore++
return `You win! ${playerSelection} beats ${computerSelection}`
} else
return `It&#39;s a tie!`
}
function game() {
for (let i = 1; i &lt;= 5; i++) {
const playerSelection = getPlayerChoice();
const computerSelection = getComputerChoice();
console.log(playRound(playerSelection, computerSelection));
}
if (playerScore &gt; computerScore) {
return &#39;You beat the computer! You are a genius&#39;
} else if (playerScore &lt; computerScore) {
return `You got beat by the computer. Practice your throws!`
} else {
return `You tied with the computer!`
}  
}
game();

<!-- end snippet -->

答案1

得分: 1

问题在于你在条件语句中使用了单个&amp;。这不是在JavaScript中表示“逻辑与”的方式。单个&amp;是位与运算,与逻辑与(也称为“逻辑连接”)非常不同。

你应该在条件语句中使用两个&amp;字符。示例:

function playRound(playerSelection, computerSelection) {
    if (playerSelection === 'paper' && computerSelection === 'scissors' || 
            playerSelection === 'scissors' && computerSelection === 'rock' || 
            playerSelection === 'rock' && computerSelection === 'paper') {
        computerScore++
        return `You lose! ${computerSelection} beats ${playerSelection}`
        
    } else if (playerSelection === 'rock' && computerSelection === 'scissors' || 
            playerSelection === 'paper' && computerSelection === 'rock' || 
            playerSelection === 'scissors' && computerSelection === 'paper') {
        playerScore++
        return `You win! ${playerSelection} beats ${computerSelection}`
    } else if (playerSelection === computerSelection) {
       return "Tie!"
    } else {
        return `Error: Invalid input\n${playerSelection}\n${computerSelection}`
    }

}

还值得注意的是,你的if/else结构中存在逻辑错误。按照你的方式,输入不在'rock''paper''scissors'范围内的内容会导致返回“You win...”。

我在上面的示例代码中已经修复了这个错误。现在,如果任何一方输入无效内容,都不会增加玩家得分,并且游戏会打印错误消息。

英文:

The issue is you are using a single &amp; as part of your conditional statements. This is not how you say "logical this AND that" in JavaScript. A single &amp; is a bitwise AND which is very different from a logical AND (also known as a 'logical conjunction').

You should use two &amp; characters within your conditional statements. Example:

function playRound(playerSelection, computerSelection) {
    if (playerSelection === &#39;paper&#39; &amp;&amp; computerSelection === &#39;scissors&#39; || 
            playerSelection === &#39;scissors&#39; &amp;&amp; computerSelection === &#39;rock&#39; || 
            playerSelection === &#39;rock&#39; &amp;&amp; computerSelection === &#39;paper&#39;) {
        computerScore++
        return `You lose! ${computerSelection} beats ${playerSelection}`
        
    } else if (playerSelection === &#39;rock&#39; &amp;&amp; computerSelection === &#39;scissors&#39; || 
            playerSelection === &#39;paper&#39; &amp;&amp; computerSelection === &#39;rock&#39; || 
            playerSelection === &#39;scissors&#39; &amp;&amp; computerSelection === &#39;paper&#39;) {
        playerScore++
        return `You win! ${playerSelection} beats ${computerSelection}`
    } else if (playerSelection === computerSelection) {
       return &quot;Tie!&quot;
    } else {
        return `Error: Invalid input\n${playerSelection}\n${computerSelection}`
    }

}

It's also worth noting that you had a logical error in your if/else structure. They way you have it, entering something that isn't within &#39;rock&#39;, &#39;paper&#39;, &#39;scissors&#39; would result in returning "You win..."

I've addressed that error in my example code above. Now if either user enters something invalid, neither players score is incremented and the game will print an error message.

答案2

得分: 1

请注意,你可以大大简化测试来查看谁赢。存在一个循环顺序:石头 < 布 < 剪刀 < 石头...

因此,如果计算机在选择数组中的索引恰好比你在选择数组中的索引少一,你就赢了。为了考虑到必须循环,以便石头能打败剪刀,你可以使用模 % 运算符将减法的结果-2变成+1。

const choices = ['石头', '布', '剪刀']
let playerScore = 0, computerScore = 0

function getComputerChoice() {
  return choices[Math.floor(Math.random() * choices.length)]
}

function getPlayerChoice() {
  let choice;
  while(true) {
    choice = prompt('选择石头、布或剪刀?').toLowerCase();
    if(choices.includes(choice)) return choice;
    else console.log('无效的选择,请重试');
  }
}

function playRound(playerSelection, computerSelection) {
  if(playerSelection===computerSelection) {
    return '平局';
  }
  else if((((choices.indexOf(playerSelection) - choices.indexOf(computerSelection)) + 3) % 3)===1) {
    playerScore++
    return `你赢了!${playerSelection} 战胜了 ${computerSelection}`
  }
  else {
    computerScore++
    return `你输了!${computerSelection} 战胜了 ${playerSelection}`

  }
}

function game() {
  for (let i = 1; i <= 5; i++) {
    const playerSelection = getPlayerChoice()
    const computerSelection = getComputerChoice()
    console.log(playRound(playerSelection, computerSelection))
  }

  if (playerScore > computerScore) {
    return '你战胜了电脑!你是一个天才';
  } else if (playerScore < computerScore) {
    return '电脑战胜了你。练习你的出招吧!';
  } else {
    return '你和电脑打成了平局!';
  }
}

console.log(game())

注意:上面的代码已将游戏的选择从英语翻译为中文。

英文:

Note that you can greatly simplify the test to see who wins. There is a circular order: Rock < Paper < Scissors < Rock...

Therefore, you win if the index of the computer's choice in the choices array is exactly one less than the index of your choice in the choices array. To account for the fact that it must loop around, so that Rock beats Scissors, you can turn a -2 result of the subtraction into +1 by using the mod % operator.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const choices = [&#39;rock&#39;, &#39;paper&#39;, &#39;scissors&#39;]
let playerScore = 0, computerScore = 0
function getComputerChoice() {
return choices[Math.floor(Math.random() * choices.length)]
}
function getPlayerChoice() {
let choice;
while(true) {
choice = prompt(&#39;Pick Rock, Paper, or Scissors?&#39;).toLowerCase();
if(choices.includes(choice)) return choice;
else console.log(&#39;Invalid choice, please try again&#39;);
}
}
function playRound(playerSelection, computerSelection) {
if(playerSelection===computerSelection) {
return &#39;Draw&#39;
}
else if((((choices.indexOf(playerSelection) - choices.indexOf(computerSelection)) + 3) % 3)===1) {
playerScore++
return `You win! ${playerSelection} beats ${computerSelection}`
}
else {
computerScore++
return `You lose! ${computerSelection} beats ${playerSelection}`
}
}
function game() {
for (let i = 1; i &lt;= 5; i++) {
const playerSelection = getPlayerChoice()
const computerSelection = getComputerChoice()
console.log(playRound(playerSelection, computerSelection))
}
if (playerScore &gt; computerScore) {
return &#39;You beat the computer! You are a genius&#39;
} else if (playerScore &lt; computerScore) {
return `You got beat by the computer. Practice your throws!`
} else {
return `You tied with the computer!`
}
}
console.log(game())

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月18日 01:50:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定