首页 > web前端 > js教程 > 正文

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

聖光之護
发布: 2025-08-20 18:52:01
原创
753人浏览过

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 数组的索引,并结合模运算 % 来实现。

美图设计室
美图设计室

5分钟在线高效完成平面设计,AI帮你做设计

美图设计室 29
查看详情 美图设计室

核心思想:

  • Rock < Paper < Scissors < 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 <= 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中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号