JavaScript 猜拳游戏:完善你的计分系统与逻辑

javascript 猜拳游戏:完善你的计分系统与逻辑

本文将引导你构建一个基于 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 数组的索引,并结合模运算 % 来实现。

核心思想:

Rock 如果玩家的选择的索引比电脑的选择的索引大 1,则玩家获胜(考虑循环,使用模运算)。

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}`  }}

代码解释:

choices.indexOf(playerSelection) 获取玩家选择在 choices 数组中的索引。choices.indexOf(computerSelection) 获取电脑选择在 choices 数组中的索引。((choices.indexOf(playerSelection) – choices.indexOf(computerSelection)) + 3) % 3 计算索引差,并使用模运算处理循环情况(例如,Rock 胜 Scissors 的情况)。如果结果为 1,则玩家获胜。

游戏主循环

game 函数负责控制游戏流程,进行 5 轮游戏,并最终输出结果。

function game() {  for (let i = 1; i  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  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 猜拳游戏:完善你的计分系统与逻辑的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1516612.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月20日 10:10:44
下一篇 2025年12月20日 10:10:51

相关推荐

发表回复

登录后才能评论
关注微信