[英]How to display a hint when you have 3 guesses (lives) left in a hangman game using JavaScript?
[英]trying to display the amount of guesses that is left in a Hangman game
我是一個新手,嘗試顯示每次執行錯誤字母(錯誤猜測)時,Hangman游戲中剩下的猜測數量(十分之十,下降到0)。 我想將用戶重定向到一個名為“ Looser Page.html”的新頁面。 目前正在這樣做,但是公眾/用戶看不到剩下的猜測。 我不知道該怎么做。 請幫忙。 提前致謝。
以下是我用來創建此頁面的全部JavaScript代碼。
var wordbank = ['browser', 'binary', 'cache', 'cookie', 'CSS', 'HTML', 'javascript', 'gigabyte', 'google', 'download']
var currentPlayingWord = "";
var underscores = "";
var guessCounter = 0;
$(document).ready(function() {
var randomWordIndex = randomNumber();
currentPlayingWord = wordbank[randomWordIndex];
underscores = wordloop(currentPlayingWord);
wordOutCome(underscores);
guessCounter = 10;
$('#all-the-buttons button').click(function () {
letterPress($(this));
});
});
var wordloop = function(word){
var wordcount = 0
var underscores = "";
while(wordcount < word.length) {
underscores = underscores + "_";
wordcount ++;
}
return underscores;
}
var randomNumber = function(){
var random = Math.floor((Math.random() * 9) + 0);
return random;
}
var wordOutCome = function(underscores){
var wordoutcome = document.getElementById('word-outcome');
wordoutcome.value = underscores;
}
function letterPress(button) {
var text = button.text();
if ("RESET" === text){
resetButton();
}
else {
var currentText = $('#word-outcome').val();
//var output = currentText + text;
var result = isLetterInWord(text, currentPlayingWord);
if(result == true) {
replaceDashesForLetter(text);
var hasDashes = noMoreDashes();
if(hasDashes == true) {
navigateToWinnerPage();
}
}
else {
decreaseGuessCount();
noMoreGuesses();
addIncorrectGuessToWrongGuesses(text);
}
$('#word-outcome').val(underscores);
}
}
function isLetterInWord(guess, word) {
var uppercaseGuess = guess.toUpperCase();
var uppercaseWord = word.toUpperCase();
for (var i = 0; i < uppercaseWord.length; i++){
console.log(i);
if (uppercaseWord[i] === uppercaseGuess){
return true;
}
//get letter from word
//is letter from word the same as guess
//if letter from word is the same as guess return true
//return false if guess is not in the word
}
return false;
}
function replaceDashesForLetter(letter) {
for (var i = 0; i < currentPlayingWord.length; i++){
console.log(currentPlayingWord);
var playingLetter = currentPlayingWord[i];
var upperCaseCurrentLetter = playingLetter.toUpperCase();
if (upperCaseCurrentLetter == letter){
underscores = setCharAt(underscores, i, letter);
}
}
//for each letter in current word being played
//does letter guessed match the letter in the current word
//if letter guessed matches the letter in the current word - then replace the dash at the index (count in loop) with the letter guessed
//for each of the letters in the word being played there is a dash
//if the letter is at the index of a dash then replace that dash with the letter (which is the users guess)
}
function setCharAt(str,index,chr) {
//get first part of word up to character we want to replace
var first = str.substr(0,index);
//get second part of word ONE letter AFTER character we want to replace
var second = str.substr(index+1);
//result is the first part plus the character to replace plus the second part
return first + chr + second;
}
var addIncorrectGuessToWrongGuesses = function (guess) {
var currentText = document.getElementById("wrong-guesses").value;
document.getElementById("wrong-guesses").value = currentText + guess;
//As the guess is wrong
//add the guess to the list of incorrect guesses displayed on the screen
}
var greyOutButton = function (button) {
//grey out the button
//make sure that the user cannot press the button anymore
}
function resetButton () {
location.href = "Hangman.html";
//Send user to the home page
}
var decreaseGuessCount = function () {
guessCounter = guessCounter - 1;
//guess count should be decreased by one
}
var noMoreGuesses = function() {
if (guessCounter === 0){
location.href = "Looser Page.html";
}
//do something when no more guesses (navigate to loser page)
}
var noMoreDashes = function() {
var i = underscores.indexOf("_");
if (i > -1){
return false;
}
return true;
//if index of '_' is not -1 then there are dashes
}
var navigateToWinnerPage = function() {
location.href = "Winner Page.html";
}
要顯示剩余的猜測數量,您將需要有一個帶ID的范圍,然后每當減少剩余的猜測時,就將該值放入范圍的text屬性中。
HTML:
<span id="guessesLeft"></span>
jQuery的:
$("#guessesLeft").text(guessesLeft);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.