Reputation: 912
I would like to generate a random value on an array. But each click, the array gain a new value, so I don't want to get a random value who have already a value on the array. In a nutshell, I need to random for the empty value of the array.
Here is a little part of the code. I have 9 values on my array, and at the beginning all of them are empty.
var gameCase = ['', '', '', '', '', '', '', '', ''];
var randomValue = gameCase[VALUE_EMPTY*][Math.round(Math.random()*gameCase.length)];
*Here is the part I don't really know how to do it.
Upvotes: 0
Views: 191
Reputation: 7466
There are few ways to approach this:
Method 1: Keep generating random index until it points to an empty value
var gameCase = ['', '', '', '', '', '', '', '', ''];
var randomIndex = Math.round(Math.random()*gameCase.length) % emptyCases.length;
var randomValue = gameCase[randomIndex];
// while we haven't found the empty value
while (randomValue !== '') {
// keep on looking
randomIndex = Math.round(Math.random()*gameCase.length % emptyCases.length);
randomValue = gameCase[randomIndex];
}
// when we exit the while loop:
// - randomValue would === ''
// - randomIndex would point to its position in gameCase[]
Method 2: Have a 2nd array that keeps track of which indices of gameCase
array have empty values
var gameCase = ['', '', '', '', '', '', '', '', ''];
var emptyCases = [0,1,2,3,4,5,6,7,8];
if (emptyCases.length > 0) {
// generate random Index from emptyCases[]
var randomIndex = emptyCase[Math.round(Math.random()*emptyCase.length) % emptyCases.length];
// get the corresponding value
var randomValue = gameCase[randomIndex];
// remove index from emptyCases[]
emptyCases.splice(randomIndex, 1);
}
Method #2 is more efficient in a sense because you don't have waste time to generate/guess a random index. For Method #1 you need a way to check if there are any empty values left in gameCase[]
or else you might be generating/guessing forever in an infinite loop.
More: When you set values for gameCase[]
you need to update emptyCases[]
accordingly to accurately reflect the state of gameCase[]
:
var gameCase = ['', '', '', '', '', '', '', '', ''];
var emptyCases = [0,1,2,3,4,5,6,7,8];
/* Update a value in gameCase[] at the specified index */
var setGameCaseValue = function(index, value) {
// set value for gameCase[]
gameCase[index] = value;
if (value !== '') { // we're setting a value
// remove that index from emptyCases[]
emptyCases.splice(emptyCases.indexOf(index), 1);
} else { // we're setting it back to empty string
// add that index into emptyCases[] that refers to the empty string in gameCase[]
emptyCases.push(index);
}
};
setGameCaseValue(2, 'null');
// gameCase now has ['','','null','','','','','','']
// emptyCases now has [0,1,3,4,5,6,7,8]
setGameCaseValue(0, 'null');
// gameCase now has ['null','','null','','','','','','']
// emptyCases now has [1,3,4,5,6,7,8]
setGameCaseValue(5, 'null');
// gameCase now has ['null','','null','','','null','','','']
// emptyCases now has [1,3,4,6,7,8]
setGameCaseValue(7, 'null');
// gameCase now has ['null','','null','','','null','','null','']
// emptyCases now has [1,3,4,6,8]
See fiddle: http://jsfiddle.net/rWvnW/1/
Upvotes: 3