My number of guesses wont stop at correct level

I am currently making a minefield game using JavaScript only. I have a problem at the end of the game. I have three difficulty levels so easy gives the user 45 guesses, intermediate 40 guesses and advanced is 35 guesses. However, for some reason when I have it set on intermediate/advanced it keeps going to 45 guesses instead of where it should stop.

After ive spent ages looking at it and changing different things such as the names of the variables and editing code. The guesses were working correctly so each guess actually counted as 1 guess now they are counting as 3. The code is now stopping at 35.

var number_of_guesses = 0;
var maximum_guesses = 45;
var intermediate_guesses = 40;
var advanced_guesses = 35;
var number_of_hits = 0;
var number_of_misses = 0;

var element = opener.document.getElementById("easy");
if (element.checked === true)
{
maximum_guesses = 45;
}

var intermediate = opener.document.getElementById("intermediate");
if (element.checked === true)
{
intermediate_guesses = 40;
}

var advanced = opener.document.getElementById("advanced");
if (element.checked === true)
{
advanced_guesses = 35;
}

number_of_guesses++;
if (number_of_guesses > maximum_guesses)
{
window.alert("Sorry, you have run out of guesses!");
return;
}

number_of_guesses++;
if (number_of_guesses > intermediate_guesses)
{
window.alert("Sorry, you have run out of guesses!");
return;
}

number_of_guesses++;
if (number_of_guesses > intermediate_guesses)
{
window.alert("Sorry, you have run out of guesses!");
return;
}

Can someone please tell me: a) How to make each guess count as 1 instead of 3? b) How to set each difficulty correctly?

Answers


Try having all three options set the maximum_guesses variable, rather than having three different variables for the different difficulties. Then just check that one variable.

var number_of_guesses = 0;
var maximum_guesses = 45;
var number_of_hits = 0;
var number_of_misses = 0;
// notice the missing intermediate_guesses and advanced_guesses vars

var element = opener.document.getElementById("easy");
if (element.checked)
{
maximum_guesses = 45;
}

var intermediate = opener.document.getElementById("intermediate");
if (intermediate.checked)
{
maximum_guesses = 40; // changed this to maximum_guesses
}

var advanced = opener.document.getElementById("advanced");
if (advanced.checked)
{
maximum_guesses = 35; // changed this to maximum_guesses
}

number_of_guesses++; // you only need this once
if (number_of_guesses > maximum_guesses)
{
window.alert("Sorry, you have run out of guesses!");
return;
}

EDIT: You also had a typo in that when you were checking wether or not the intermediate and advanced checkboxes were checked, you were still checking the easy element.

EDIT 2: Also, in your code, you were calling number_of_guesses++; three times, which was why each guess was counting 3 times. For them to only count as one, you only needed the first one. Each time you check, the number will stay the same, so you don't need to increment it three times.

EDIT 3: In my code, notice how I removed the === false parts in your if statements where you checked wether or not the checkboxes were checked.


Need Your Help

Arguments in a function prototype

c++ c arrays pointers

My question is: when i write a function prototype in C like this:

Rowwise weighted.mean in dplyr using weights from another data.frame

r dplyr

I have a data.frame with columns from different groups (here, a and b), and another data.frame containing the weights to perform a weighted mean: