[英]trying to display the amount of guesses that is left in a Hangman game
I am a newbie trying to display the amount of guesses that is left in a Hangman game out of 10, which goes down to 0, when every time you hit the wrong letter (wrong guess). 我是一个新手,尝试显示每次执行错误字母(错误猜测)时,Hangman游戏中剩下的猜测数量(十分之十,下降到0)。 And I want to redirect the user to a new page called "Looser Page.html".
我想将用户重定向到一个名为“ Looser Page.html”的新页面。 At the moment it does that but the guesses left is not visible to the public/user.
目前正在这样做,但是公众/用户看不到剩下的猜测。 I have no clue to how to do that.
我不知道该怎么做。 Please Help.
请帮忙。 Thanks in advance.
提前致谢。
Below is my whole JavaScript code that I am using to to create this page. 以下是我用来创建此页面的全部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";
}
To display the amount of guesses left, you will need to have a span with an id, then whenever you decrement the guesses left, you put that value into the text attribute of the span. 要显示剩余的猜测数量,您将需要有一个带ID的范围,然后每当减少剩余的猜测时,就将该值放入范围的text属性中。
HTML: HTML:
<span id="guessesLeft"></span>
jQuery: jQuery的:
$("#guessesLeft").text(guessesLeft);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.