javascript - Win check 的 Java 脚本刽子手问题
问题描述
我一直在尝试使用 javascript 制作一个刽子手游戏,但我正在努力获得两人和一人模式的胜利检查
alert('welcome to hang man here you will have ten guesses you may type one letter at '+
'a time or try to guess the whole word but be careful each letter in the word '+
'will count as a guess ');
game_mode_choice = prompt('what game mode would you like to play, A 1 player mode or B '+
'2 player mode');
dictinary_array = ["dog", 'cat', 'elephant', 'fox', 'horse'];
character_limit = [];
guess = "";
if (game_mode_choice == "B") {
usersGuesses = [];
solved = false;
alert('The first player will input a word for the other player to then guess got it okay');
word_choice = prompt('Which word would u like to chose');
for (spaces = 0; spaces < word_choice.length; spaces++) {
// check if the charAt word_choice index of spaces is in a
// list of the users guesses, if it is than you need to
// push the letter instead of a blank space;
character_limit.push(' _ ');
}
alert(character_limit);
while (!solved) {
guess = prompt("Please choose a letter");
if (word_choice.includes(guess)) {
for (i = 0; i < word_choice.length; i++) {
if (word_choice[i] == guess) {
character_limit[i] = guess;
solved = true;
}
}
}
usersGuesses.push(guess);
alert(character_limit + "\n\nLetters used so far: " + usersGuesses);
}
}
解决方案
尝试像这样创建函数:
function isWin(riddle, guess) {
for (let i = 0; i < riddle.length; i += 1) {
if (riddle[i] !== guess[i]) {
return false;
}
}
return true;
}
用法:
const win = isWin(word_choice, character_limit)
推荐阅读
- javascript - 为什么 window.close() 突然不起作用?
- javascript - 用奇怪的“â”字符解析格式错误的 JS 字符串
- python - 解析字符串列表
- arrays - 在堆中声明数组时的无限空间
- laravel - 文件在 50 个文件上获取内容太慢
- c# - 如何在 ReactiveUI 中通过单击按钮设置 TextBox 值
- printing - 斑马标签 zpl 没有完全显示在物理但是虚拟打印中
- discord.py - 在 cogs 中找不到命令
- php - 为什么只匹配记录中的第一个单词?
- python - Transform all list arguments of all functions?