I have some assignment to make a game n i choose the game puzzle like sudoku (more like Elemental at Android, if you had play this game). well, with think so long, i think i need to make arrays of number for each row which each indexs of arrays cannot be same to make 1 column and 1 row consist of 1 full array (without same number at one arrangement of array).
so i have made one and near of success because sometime the result as i want, sometime not. the problem is sometime there was arrays with "undefined" and the time took longer than the right results.
please, i really need your help to complete my codes. or maybe you can give me another codes that same with i need. i had search for it, but i can't find that i need.
thanks before. i hope you can help meCode:<head> <style type="text/css"> td { border:1px solid #ccc; text-align:center; padding:0.3em } </style> <script type="text/javascript"> function showArray(){ var firstArray = ['1','2','3','4','5']; var sizeArray = firstArray.length; var compArray = new Array(); var t = document.getElementById('tes'); //make array for row var row=0; while(row<sizeArray){ var shufArray = arrayShuffle(firstArray); var tr = t.appendChild(document.createElement('tr')); if(compArray.length>0) var shufArray = makeShuffle(shufArray, compArray); //write the array for one row for (var i=0,col=0;col<sizeArray;col++) tr.appendChild(document.createElement('td')).innerHTML = shufArray[i++]; compArray.push(shufArray); //<-- add shufArray to compArray => become reference for next row row++; } } //function to cek shufArray, ensure to not same with previous arrays per indexs function makeShuffle(oldShufArray, shufArrayAll){ var shufArray2 = new Array(); var temp = oldShufArray; for(j=0;j<=(oldShufArray.length-1);j++){ var compArray2 = new Array(); //array from shufArray, where index=j for(x=0;x<=(shufArrayAll.length-1);x++) compArray2.push(shufArrayAll[x][j]); //if temp[j] not in compArray2 and shufArray2 if(compArray2.indexOf(temp[j])<0 && shufArray2.indexOf(temp[j])<0){ shufArray2.push(temp[j]); //<-- pust into shufArray2 }else{ sliceArray = temp.slice(j); shufSliceArray = arrayShuffle(sliceArray); //<-- slice temp from j that problematic temp = shufArray2.concat(shufSliceArray); //<-- merge with previous index that didnt have problem j-=1; //<-- set j-1 so the next j become now j } } return shufArray2; } //function to shuffle array, i get from searching in google function arrayShuffle(oldArray) { var newArray = oldArray.slice(); var len = newArray.length; var i = len; while(i--){ //for(i=len-1;i>=0;i--){ var p = parseInt(Math.random()*len); var t = newArray[i]; newArray[i] = newArray[p]; newArray[p] = t; } return newArray; }; </script> </head> <body onLoad="showArray();"> <table><tbody id="tes"></tbody></table> </body>![]()


Reply With Quote
Bookmarks