I am trying to make a simple version of the Guess Who guessing game. I have some global variables to store things, for example...
var allTheCharacters = [];
var allTheCharactersComp = [];
var theHuman = []; //Random Character stored here
var theComputer = []; //Random Character stored here
var singleChar;
[{
"id": 1,
"male": true,
"female": false,
"name": "Jimmy",
"bald": false,
"black hair": true,
"white hair": false,
"blonde hair": false,
"red hair": false,
"purple hair": false,
"blue hair": false,
"beard": false,
"moustache": false,
"glasses": false,
"earrings": false,
"hat": true,
"brown skin": true,
"pale skin": false,
"image": "img/face1.jpg"
}, {
"id": 2,
"male": false,
"female": true,
"name": "Vanessa",
"bald": false,
"black hair": false,
"white hair": false,
"blonde hair": true,
"red hair": false,
"purple hair": false,
"blue hair": false,
"beard": false,
"moustache": false,
"glasses": false,
"earrings": false,
"hat": false,
"brown skin": false,
"pale skin": true,
"image": "img/face2.jpg"
}, {
"id": 3,
"male": true,
"female": false,
"name": "Benjamin",
"bald": true,
"black hair": false,
"white hair": true,
"blonde hair": false,
"red hair": false,
"purple hair": false,
"blue hair": false,
"beard": false,
"moustache": true,
"glasses": false,
"earrings": false,
"hat": false,
"brown skin": false,
"pale skin": true,
"image": "img/face3.jpg"
}]
var newGame = function() {
$('.start-button').one('click', function(event) {
turn == 0;
humanTurn();
//checkForWinner();
turn == 1;
computerTurn();
//checkForWinner();
});
};
$(document).ready(function() {
newGame();
});
//Get <SELECT> values
function grabInputValue() {
// click event on select submit
$(".pickAFeatureBtn").on('click', function(e) {
e.preventDefault();
humanTurn();
console.log(turn);
});
}
grabInputValue();
function humanTurn() {
allTheCharacters;
//console.log(allTheCharactersComp);
for(i=allTheCharactersComp.length; i >= 0; i-- ){
//console.log(allTheCharactersComp[i]);
var singleChar = allTheCharactersComp[i];
//
$.each(singleChar, function(index, val) {
// console.log(index, val);
var hasFeature = ($('.featureList').val());
if(index == hasFeature && val == false){
console.log(index + " : " + val);
//Need to remove object from allThe Characters
allTheCharactersComp.splice(index, 1);
}
});
}
console.log(allTheCharactersComp);
turn = 1;
// console.log(turn);
};
function computerTurn() {
// To come...
// Will this just be similar to the humanTurn() code?
};
function checkForWinner() {
// To come...
// check for allTheCharacters.length = 1;
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<button type="submit" class="pickAFeatureBtn">Check for Attribute</button>
<select class="featureList">
<option class="charFeature" value="male">male</option>
<option class="charFeature" value="female">female</option>
<option class="charFeature" value="black hair">black hair</option>
<option class="charFeature" value="white hair">white hair</option>
<option class="charFeature" value="blonde hair">blonde hair</option>
<option class="charFeature" value="red hair">red hair</option>
<option class="charFeature" value="blue hair">blue hair</option>
<option class="charFeature" value="purple hair">purple hair</option>
<option class="charFeature" value="bald">bald</option>
<option class="charFeature" value="beard">beard</option>
<option class="charFeature" value="moustache">moustache</option>
<option class="charFeature" value="glasses">glasses</option>
<option class="charFeature" value="earrings">earrings</option>
<option class="charFeature" value="hat">hat</option>
<option class="charFeature" value="green eyes">green eyes</option>
<option class="charFeature" value="black eyes">black eyes</option>
<option class="charFeature" value="brown skin">dark skin</option>
<option class="charFeature" value="pale skin">light skin</option>
</select>
So - In the humanTurn() function what I am trying to do is check if the value of the dropdown is the same as the random Character held in theComputer[] and if so delete that character from allTheCharactersComp[] However, when I console.log the effects of the - allTheCharacters.splice(index, 1) - it only returns a single object in the array, rather that having removed all the characters that dont match the condition in the if statement, leaving those that do.
Thank you for reading this far, I know there is a load of stuff here but I'm at a loss how to get this working. Any help would be greatly appreciated.
If there is any other bits of code that you need to see just shout out. Thanks again.