הכנסה למצב קוד
מתנצל מראש על זה שזה לא כ"כ קריא
יש דברים שאולי היה כדאי לשפר...
האלגוריתם כולל בדיקה של המערכת ע"י יצירת מספרים אקראים ושליחתם לאלגוריתם
function ge(thenum){
temp=[];
for (i=1000;i<10000;i++){
snum=i.toString();
if (snum[0]!=0&&snum[1]!=0&&snum[2]!=0&&snum[3]!=0&&snum[0]!=snum[1]&&snum[0]!=snum[2]&&snum[0]!=snum[3]&&snum[1]!=snum[2]&&snum[1]!=snum[3]&&snum[2]!=snum[3]){
temp.push(i);
}
}
function getans(gess,num){
gess=gess.toString();
num=num.toString();
numarr=[0,0,0,0,0,0,0,0,0,0];
pig=0;
bol=0;
for (i=0;i<4;i++)
if(gess[i]==num[i]){bol++;numarr[gess[i]]=-4;} else if(num.indexOf(gess[i])!=-1){numarr[gess[i]]++;}
for (i=0;i<10;i++)
if (numarr[i]>0) pig ++;
return bol+","+pig;
}
function getnewtemp(toza,gess){
var temp1=[];
for (j=0;j<temp.length;j++){
if (getans(gess,temp[j])==toza&&temp[j]!=gess) temp1.push(temp[j]);
}
return temp1;
}
cont=0;
good="0";
while (good!="4"){
gess=temp[0];
toza=getans(gess,thenum);
if (toza=="0,0") alert (cont);
cont++;
temp=getnewtemp(toza,gess);
good=toza.split(",")[0];
if (cont>10) return thenum;
}
return cont;
}
tozot=[];
for (l=0;l<10;l++){
tozot.push(ge(dig4()));
}
function dig4(){
numarr=[1,2,3,4,5,6,7,8,9];
function zimzom(){for (i=0;i<9;i++){if (numarr[i]==0){numarr[i]=numarr[i+1];numarr[i+1]=0;}}}
temp=Math.floor(Math.random()*9);
num=numarr[temp];
numarr[temp]=0;
zimzom();
temp=Math.floor(Math.random()*8);
num+=numarr[temp]*10;
numarr[temp]=0;
zimzom();
temp=Math.floor(Math.random()*7);
num+=numarr[temp]*100;
numarr[temp]=0;
zimzom();
num+=numarr[Math.floor(Math.random()*6)]*1000;
return num;
}
