It has been two years, since I know JS1k for first time. I have sent three times, until this year, 2017. This time, I'll discuss about what really happen in my demoscene and how it works.
This is the code:
for(_='er~";Z-1]Xp||W=[]V+1UA[S){QinP0<O14-p]Nstyle "+( Si] 1>h? ") 6<i%8? && ); else C(2 ":" =0; .push( function( 1>h Sp document.createElement("t = able for( if( >"+ +"</ === WOh >i;i++) . .margP= >p)S8*h+7]+=S .Pn~HTML= .appendChild( 1> j++;6<j 3, b T b Y T Y "autoZvar AV,RV,p=s=h=n=i=j=k=l=m (C= a,cQ a) 20 aQi j="ZkV; m="Z16 l="<td =\'paddPg:9px;bord~:1px solid #000;background: i p? "cyan red #ff0 +(7>i O ?Zcursor:poPt~;\'onclick=\'C( iU)+ \' ;\' +( "rowspan=2><b b> td> ,8>i?7>i?j+=l:m=l:k l R[0] k.rev~se().joP(" +m;R[1] j} 21 a)Os?(n=1, =(Oh 6 W 14 p?p+2:pU)%16]++,s--, 0),setTimeout( Q 1)},500)):(n=0,1< ] 7>p%8?C(pU): 2)),Y 0>h?"Reload for new game In hand: "+s+"<br/> "Your AI\'s +" turnZ 22 aQ 1 ]Q 7> 7<p 15 N,SN 8<p 14> Op 6 pX+ U], X= U]=0} 0 7 15 W(h= 1:0 j=i 7 1 i=8; j 15 2 Oh) ;;) OSi=8+Date.now()%7]QC(iU break}} 23 a?(h=-1,al~t("You 1<c?"wP lose )):1>n (s= =aX, ]=0, 1) { i 16 A 0:7 i 2 R T r ) 0)}})( ';g=/[^ -MRTY[-}]/.exec(_);)with(_.split(g))_=join(shift());eval(_)
Maybe you're confused with the code above. What is that?Opening code pack
The code above is the code which has been packaged into small package that will be opened and run automatically. If you see the source code, actually, that is the code when you open the package.
Start to analyse code
// JS1K ENTRY
// Title: Dakon (Javanese Congkak)
// by Muhammad Rifqi Priyo Susanto
First, we make table for container and for information then assign them to some variables while being argument to a function.
b.appendChild(T=document.createElement("table"));
b.appendChild(Y=document.createElement("table"));
Set margin to be auto so that it is at the center of document. Only works for element with CSS display value is table, including table element.
T.style.margin=Y.style.margin="auto";
Variable initialisation, we can use global variable to store it.
var N=[], R=[],
p=s=h=n=i=j=k=l=m=0;
Entering main function
Z is a main function to do a lot of things in this demo. Z function can do many things, which will explained one by one.(Z=function(task,param){
- Preparing the game.
Prepare value for each house. Also, prepare tr element for container described above.if(!task){ for(i=0;i<16;i++)N.push(i%8>6?0:7); for(i=0;i<2;i++)R.push(T.appendChild(document.createElement("tr"))); Z(20);
- Tell user about now's status, from this on, called "drawing".
By using created tr element, fill with td elements which already filled with amount of seeds for each houses. If that house is being passed by player, choose another color.}else if(task===20){ for(i=0,j="",k=[],m="";i<16;i++){ // draw house l="<td style='padding:9px;border:1px solid #000;background:"+ (i===p?(h<1?"cyan":"red"):"#ff0")+ (i<7&&N[i]>0 ?";cursor:pointer;'onclick='Z("+(i+1)+")'" :";'" )+ (i%8>6?"rowspan=2><b>"+N[i]+"</b>":">"+N[i]+"</td>"); i<8?i<7?j+=l:m=l:k.push(l); } R[0].innerHTML=k.reverse().join("")+m; R[1].innerHTML=j;
- Moves each step.
}else if(task===21){ // if there is a seed on hand, go if(s>0){ // declare step activity n=1; // add 1 to next house // move pointer N[p=( h>0&&p===6 || h<1&&p===14 ? p+2: p+1 )%16]++; // subtract seed amount on hand s--; // draw game Z(20); // set next step setTimeout(function(){Z(21)},500) // if there is no seed on hand }else{ // stop step activity n=0; // if seed in pointed house // is more than one and // not a storehouse N[p]>1&&p%8<7? // next step Z(p+1): // else // run analyser Z(22) } // tell player // if game's over, add notification // to refresh for new game // if not, tell amount of seed on hand and whose turn is it Y.innerHTML=h<0? "Reload for new game": "In hand: "+s+"<br/>"+(h<1?"Your":"AI's")+" turn" ;
- Analysing game's condition and computer movement.
Also will check for special condition, including another rules in game.}else if(task===22){ if(N[p]===1){ // shooting ("nembak") // if seed amount in pointed house is 1 and the house across is // one of enemy's house then add seed from was house shot to // storehouse and set seed amount in house shot to zero if(h<1&&p<7 || h>0&&p>7&&p<15){ N[h*8+7]+=N[14-p], N[14-p]=0 } // bring ("mikul") // if amount of seed in pointed house is 1 and that house is enemy's // and there is seed in left and right houses (not storehouse) then // add seed from those houses to storehouse and set amount of seed // in those houses to zero if(h<1&&p>8&&p<14 || h>0&&p>0&&p<6){ N[h*8+7]+=N[p-1]+N[p+1], N[p-1]=N[p+1]=0 } } // draw game Z(20); // if pointer is not at storehouse, // change player !(h<1&&p===7 || h>0&&p===15) && (h=h<1?1:0); // check player's houses (except storehouse) for(i=0,j=0;i<7;i++)N[i]<1&&j++; // if all player's houses don't have seed, // computer wins! j>6&&Z(23,1); // check computer's houses (except storehouse) for(i=8,j=0;i<15;i++)N[i]<1&&j++; // if all computer's houses don't have seed, // player wins! j>6&&Z(23,2); // if this is computer's turn now if(h>0) // run // Artificial Intelligence // by using loop and random select for(;;){ // if sample house is more than zero if(N[i=8+Date.now()%7]>0){ // choose that house Z(i+1); break; } }
- When game's over.
}else if(task===23){ // not anyone's turn h=-1; // announce who's the winner alert("You "+(param>1?"win":"lose"));
- Choose house to move next.
}else{ // if activity step is NOT running, if(n<1) // set: // - seed amount on hand // - pointer // - seed amount on hand in pointed house to zero s=N[p=task-1], N[p]=0, // run step activity Z(21); }
Run this function automatically so that immediately prepare the game.
})();
Tidak ada komentar:
Posting Komentar