我是一个新的有抱负的开发者,我正试图弄清楚如何使用vanilla js构建一个刽子手游戏。我已经把一个关键事件列表器放在一起,并让它控制台记录输入。我还得到了打印出来的字母,这些字母已经猜到了#34;阵列。
document.addEventListener("keypress", letterPressed);
function letterPressed(event) {
var letter = String.fromCharCode(event.keyCode);
guessedLetters.push(letter);
document.getElementById("lettersGuessed").innerHTML = guessedLetters;
console.log(guessedLetters)
}
我还有一系列单词选择
var wordList = ["Ravens", "Cardinals", "Falcons", "Bills",
"Panthers", "Bears", "Bengals", "Browns", "Cowboys",
"Broncos", "Lions", "Packers", "Texans", "Colts",
"Jaguars", "Cheifs", "Chargers", "Rams",
"Dolphins", "Vikings", "Patriots", "Saints",
"Giants", "Jets", "Raiders", "Eagles", "Steelers",
"Forty Niners", "Seahawks", "Buccaneers", "Titans",
"Redskins"];
和for循环从这个数组中选取随机字,将其转换为" _"单词长度中的字符串,并将其打印到div标识为"空格"的html文档中。
var wordBlanks = [];
var guessedLetters = [];
var randomWord = wordList[Math.floor(Math.random() * wordList.length)];
for (var i = 0; i < randomWord.length; i++) {
wordBlanks[i] = "_";
console.log(wordBlanks,randomWord);
document.getElementById("spaces").innerHTML = wordBlanks.join(" ");
};
我甚至想从哪里离开?我想检查键盘输入(或字母猜测数组,我不确定哪个是最好的)对所选的单词和#34; _&#34;正确猜测时,字符串会显示正确的猜测。
我的问题更多的是指向我正确的方向。所以,我可以正确地教自己。有任何建议吗?
谢谢!
答案 0 :(得分:1)
你必须首先考虑大写/小写问题,但在此之后,这样的事情会起作用:
const randomWord = "BEARS";
const guessedLetters = ["S", "O", "E"];
const wordBlanks = randomWord.split('')
.map(letter => guessedLetters.indexOf(letter) >= 0 ? letter : "_")
.join(' ');
console.log(wordBlanks);
答案 1 :(得分:0)
不要放入using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Animation;
namespace WeRecognition.Services
{
public static class NavigationService
{
private static Frame _frame;
public static Frame Frame
{
get
{
if (_frame == null)
{
_frame = Window.Current.Content as Frame;
}
return _frame;
}
set
{
_frame = value;
}
}
public static bool CanGoBack => Frame.CanGoBack;
public static bool CanGoForward => Frame.CanGoForward;
public static void GoBack() => Frame.GoBack();
public static void GoForward() => Frame.GoForward();
public static bool Navigate(Type pageType, object parameter = null, NavigationTransitionInfo infoOverride = null)
{
// Don't open the same page multiple times
if (Frame.Content?.GetType() != pageType)
{
return Frame.Navigate(pageType, parameter, infoOverride);
}
else
{
return false;
}
}
public static bool Navigate<T>(object parameter = null, NavigationTransitionInfo infoOverride = null) where T : Page => Navigate(typeof(T), parameter, infoOverride);
}
}
数组,而是尝试查看是否可以在guessedLetters
中找到印记信。
如果有,请找到该字母的位置,并将其替换为randomWord
数组中相应的_
空格。
类似的东西:
wordBlank
请注意,一个字母可能有多个出现。