问题描述:

I'm doing "fifteen puzzle" game. I'm only a beginner, so I chose this project to implement. My problem is shuffle algorithm :

function shuffle() {

$('td').empty();

var p = 0;

var f = 0;

do {

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

var rand = arr[Math.floor(Math.random() * arr.length)];

if ($('#' + rand).is(':empty')) {

p = p + 1;

document.getElementById(rand).textContent = p

var f = $('td').not(":empty").length;

} else {}

} while (f < 15)

That works cool, but I've heard that almost 50% of all random shuffle like mine is unsolvable. So I found math formula at wikipedia.org for this game, explaining how you can avoid that.

Here's modified algorithm that doesn't work either. The way I know it is alert stuff: it launches only 2 times instead of 31.

array = [];

function algorithm (){

// alert('works')

for (var c=16; c<17; c++){

document.getElementById(c).textContent = '100';

}

for (var i=1; i<16; i++){

var curId = document.getElementById(i).id;

var curIdNum = Math.floor(curId);

alert('works')

var curIn = document.getElementById(i).textContent;

var curInNum = Math.floor(curIn);

array.push(i);

array[i] = new Array();

for (var j=1; j<15; j++){

var nextId = curIdNum + j; //curIdNum NOT cerIdNum

var nextIn = document.getElementById(nextId).textContent;

//alert('works')

if (nextId < 16){

var nextInNum = Math.floor(nextIn);

if (curInNum > nextInNum){

array[i].push(j)

}

}

}

var sum = 0;

for (var a=0; a<15; a++){

var add = array[a].length;

sum = sum + add;

}

var end = sum + 4;

if (end % 2 == 0){

document.getElementById('16').textContent = "";

}

else {

shuffle();

}

}

}

The question is the same:

What's wrong? Two-dimensional array doesn't work.If you've got any questions - ask.

Just to make it clear: 2 for loops with i and j should make a 2-dimensional array like this [ this is " var i" -->[1,3,4,5,7], this is "var i" too-->[5,7,9,14,15]]. Inside each i there's j. The for loop with var a should count the number of js inside each i. if the number of js is even, the code is finished and shuffle's accomplished, otherwise shuffle should be made once again.

网友答案:
  var nextId = cerIdNum + j;

in that fiddle, I don't see this cerIdNum declared & defined neither as local nor as global variable, I suppose that is curIdNum

网友答案:

Please use the below definition of algorithm and let us know if this works. Basically, the alert messages would come only twice, since there were usages of undefined variables. For the purpose of illustration, I have placed comments at where the problem points occured. Due to these problems, your script would stop executing abruptly thereby resulting in the behavior you described.

Oh and by the way - I did not have time to go through the Wiki link provided - hence you will have to verify your logic is correct. However, I have definitely resolved the errors causing the behavior you observed.

As an aside - consider using jQuery, your code will be a lot cleaner...

function algorithm (){
       // alert('works')
        for (var c=16; c<17; c++){
            document.getElementById(c).textContent = '100';
        }     

        for (var i=1; i<16; i++){
            var curId = document.getElementById(i).id;
            var curIdNum = Math.floor(curId);
            alert('works')
            var curIn = document.getElementById(i).textContent;
            var curInNum = Math.floor(curIn);
            array.push(i);
            for (var j=1; j<15; j++){
                var nextId = curIdNum + j; //curIdNum NOT cerIdNum
                var nextIn = document.getElementById(nextId).textContent;
            //alert('works')
            if (nextId < 16){
                var nextInNum = Math.floor(nextIn);
                if (curInNum > nextInNum){
                    array.push(j) //array[i].push does not make sense
                }
            }
        }            

        var sum = 0; 
        for (var a=0; a<15; a++){
            var add = array.length; //array[1].length does not make sense
            sum = sum + add;
        }            
        var end = sum + 4;
        if (end % 2 == 0){
            document.getElementById('16').textContent = "";
        }            
        else {       
            shuffle();
        }            
    }                
}
网友答案:

I found the solution by totally rewriting the code. Thank everyone for help!

Here's what do work:

function shuffle (){
press = 1;
$('td').empty().removeClass();
p=0;
var f;

do {
    var arr=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
    var rand=arr[Math.floor(Math.random()*arr.length)];
        if ($('#'+ rand).is(':empty')){
            p = p + 1;
            document.getElementById(rand).textContent = p
            var f = $('td').not(":empty").length;
        }
        else{}   
}while(f < 15){
    winChance();
}

}
function winChance (){
array = [];
for (i=1;i<16;i++){
    array[i]= new Array();
    var currentId = $('#' + i).attr('id');
    var currentIn = $('#' + i).html()

    var currentIdNum = parseInt(currentId, 10);
    var currentInNum = parseInt(currentIn, 10);
    for (j=1;j<16;j++){
        var nextId = currentIdNum + j;
        if (nextId < 16){
            var nextIn = $('#' + nextId).html();
            var nextInNum = parseInt(nextIn, 10);
            if (currentInNum > nextInNum){
                array[i].push(j);
            }
        }
    }
}
checkSum();
}

function checkSum(){
var sum = 0; 
for (var a=1; a<16; a++){
    var add = array[a].length;
    sum = sum + add;
}            
var end = sum + 4;
if (end % 2 == 0){}            
else {       
    shuffle();
}            
}
相关阅读:
Top