I am working on an implementation of the 15-pieces-sliding puzzle, and I am stuck at the point were I must make sure I only shuffle into "solvable permutations" - in my case with the empty tile in the down right corner: even permutations.
I have read many similar threads such as How can I ensure that when I shuffle my puzzle I still end up with an even permutation? and understand that I need to "count the parity of the number of inversions in the permutation".
I am writing in Javascript, and using Fischer-Yates-algorithm to randomize my numbers:
var allNrs = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14];
for (var i = allNrs.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp1 = allNrs[i];
var temp2 = allNrs[j];
allNrs[i] = temp2;
allNrs[j] = temp1;
}
How do I actually caculate this permutation or parity value that I have read about in so many posts?