Odin项目石头剪刀布游戏如何用循环实现无需刷新续玩且不干扰代码?
问题原因
- 你当前的单轮执行逻辑(获取玩家选择、生成电脑选择、调用
playRound)仅在页面加载时执行一次,所以每玩一轮就需要刷新才能继续 - 之前循环只输出日志的原因是没有把每轮都需要更新的玩家选择、电脑选择逻辑放进循环体里,只是循环了结果输出步骤
实现方案
你可以直接用for循环完成指定局数的游戏,也可以用while循环设置分数阈值作为终止条件,这里以Odin项目普遍要求的5局定胜负为例,调整逻辑如下:
- 把获取玩家输入的
prompt、生成电脑选择的computerPlay()调用、playRound()调用都放进循环体内部,保证每轮都重新执行 - 循环结束后输出最终胜负结果
调整后的完整代码
const options = ["Rock", "Paper", "Scissors"]; let playerPoints = 0; let computerPoints = 0; // 电脑随机出拳 function computerPlay(){ const compAnswer = options[Math.floor(Math.random() * options.length)]; console.log("电脑选择:" + compAnswer); return compAnswer; } // 单轮游戏逻辑 function playRound(playerSelection, computerSelection){ let rock = options[0]; let paper = options[1]; let scissors = options[2]; let youWin = "你赢了!"; let youLose = "你输了!"; let rockWin = "石头赢剪刀"; let scissorWin = "剪刀赢布"; let paperWin = "布赢石头"; let tie = "平局,不加分"; console.log("你选择:" + playerSelection); if (playerSelection === computerSelection){ console.log(`当前比分:你${playerPoints} - 电脑${computerPoints}`); return tie; } else if(playerSelection === rock && computerSelection === scissors){ playerPoints++; console.log(`当前比分:你${playerPoints} - 电脑${computerPoints}`); return youWin + rockWin; } else if(playerSelection === rock && computerSelection == paper){ computerPoints++; console.log(`当前比分:你${playerPoints} - 电脑${computerPoints}`); return youLose + paperWin; } else if(playerSelection === scissors && computerSelection === rock){ computerPoints++; console.log(`当前比分:你${playerPoints} - 电脑${computerPoints}`) return youLose + rockWin; } else if(playerSelection === scissors && computerSelection === paper){ playerPoints++; console.log(`当前比分:你${playerPoints} - 电脑${computerPoints}`) return youWin + scissorWin; } else if(playerSelection === paper && computerSelection === rock){ playerPoints++; console.log(`当前比分:你${playerPoints} - 电脑${computerPoints}`); return youWin + paperWin; } else if(playerSelection === paper && computerSelection === scissors){ computerPoints++; console.log(`当前比分:你${playerPoints} - 电脑${computerPoints}`); return youLose + scissorWin; } else{ console.log(`当前比分:你${playerPoints} - 电脑${computerPoints}`); return "输入无效,请重新输入" } }; // 游戏主逻辑 玩5局 function game() { for (let i = 0; i < 5; i++) { const computerSelection = computerPlay(); // 每轮都弹出prompt获取玩家选择 const playerSelection = prompt("选择你的武器:Rock/Paper/Scissors"); // 校验输入是否合法,不合法的话重新获取输入,不占用轮次 if (!options.includes(playerSelection)) { alert("输入错误,请输入Rock、Paper、Scissors中的一个"); i--; continue; } console.log(playRound(playerSelection, computerSelection)); } // 5局结束输出最终结果 console.log("===== 游戏结束 ====="); if (playerPoints > computerPoints) { console.log(`最终比分:你${playerPoints} - 电脑${computerPoints},你获得了最终胜利!`); } else if (playerPoints < computerPoints) { console.log(`最终比分:你${playerPoints} - 电脑${computerPoints},你输了。`); } else { console.log(`最终比分:你${playerPoints} - 电脑${computerPoints},平局。`); } } // 启动游戏 game();
额外说明
如果不想固定玩5局,可以把for循环改成while循环,终止条件设为playerPoints < 5 && computerPoints <5,先得到5分的一方获胜即可。
内容的提问来源于stack exchange,提问作者becmarley22
相关产品推荐
相关产品推荐

