
本文将引导你构建一个基于 JavaScript 的猜拳游戏,重点解决计分逻辑问题,并提供更简洁高效的实现方案。我们将深入探讨如何使用数组索引和模运算来简化胜负判断,同时优化用户输入验证,确保游戏的健壮性和用户体验。通过本文,你将掌握编写清晰、可维护的 JavaScript 代码的技巧,并提升解决实际编程问题的能力。
首先,我们需要定义游戏的核心元素:rock(石头)、paper(布)、scissors(剪刀)。 将它们存储在一个数组中,方便后续的逻辑处理。
const choices = ['rock', 'paper', 'scissors']; let playerScore = 0; let computerScore = 0;
getComputerChoice 函数负责随机生成电脑的选择。
function getComputerChoice() {
return choices[Math.floor(Math.random() * choices.length)];
}getPlayerChoice 函数负责获取玩家的输入,并进行有效性验证。 使用 prompt 接收用户输入,并使用循环确保输入是 choices 数组中的有效值。
立即学习“Java免费学习笔记(深入)”;
function getPlayerChoice() {
let choice;
while(true) {
choice = prompt('Pick Rock, Paper, or Scissors?').toLowerCase();
if(choices.includes(choice)) return choice;
else console.log('Invalid choice, please try again');
}
}playRound 函数是游戏的核心,负责判断胜负并更新分数。 关键在于如何简化胜负判断的逻辑。 我们可以利用 choices 数组的索引,并结合模运算 % 来实现。
核心思想:
function playRound(playerSelection, computerSelection) {
if(playerSelection===computerSelection) {
return 'Draw'
}
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}`
}
}代码解释:
game 函数负责控制游戏流程,进行 5 轮游戏,并最终输出结果。
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!`
}
}
console.log(game())const choices = ['rock', 'paper', 'scissors'];
let playerScore = 0;
let computerScore = 0;
function getComputerChoice() {
return choices[Math.floor(Math.random() * choices.length)];
}
function getPlayerChoice() {
let choice;
while(true) {
choice = prompt('Pick Rock, Paper, or Scissors?').toLowerCase();
if(choices.includes(choice)) return choice;
else console.log('Invalid choice, please try again');
}
}
function playRound(playerSelection, computerSelection) {
if(playerSelection===computerSelection) {
return 'Draw'
}
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 <= 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!`
}
}
console.log(game())通过本文,你学习了如何构建一个基于 JavaScript 的猜拳游戏,并掌握了优化计分逻辑和用户输入验证的方法。 记住,编写清晰、可维护的代码是成为一名优秀程序员的关键。 持续学习和实践,你将能够解决更复杂的编程问题。
以上就是JavaScript 猜拳游戏:完善你的计分系统与逻辑的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号