Created
August 2, 2013 02:20
-
-
Save rondorkerin/6137044 to your computer and use it in GitHub Desktop.
Minimax algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
q-=q<e;l-=l<=e; /* adj. window: delay bonus */ | |
d=Y=0; /* start iter. from scratch */ | |
X=myrand()&~M; /* start at random field */ | |
W(d++<n||d<3|| /*** min depth = 2 iterative deepening loop */ | |
z==8&K==I&&(N<T&d<98|| /* root: deepen upto time */ | |
(K=X,L=Y&S-9,d=3))) /* time's up: go do best */ | |
{x=B=X; /* start scan at prev. best */ | |
h=Y&S; /* request try noncastl. 1st*/ | |
P=d>2&&l+I?minimax(24-k,-l,1-l,-e,S,S,d-3):I; /* search null move */ | |
m=-P<l|R>35?d-2?-I:e:-P; /*** prune if > beta unconsidered:static eval */ | |
++N; /* node count (for timing) */ | |
do{u=b[x]; /* scan board looking for */ | |
if(u&k) /* own piece (inefficient!)*/ | |
{r=p=u&7; /* p = piece type (set r>0) */ | |
j=o[p+16]; /* first step vector f.piece*/ | |
W(r=p>2&r<0?-r:-o[++j]) /* loop over directions o[] */ | |
{A: /* resume normal after best */ | |
y=x;F=G=S; /* (x,y)=move, (F,G)=castl.R*/ | |
do{ /* y traverses ray, or: */ | |
H=y=h?Y^h:y+r; /* sneak in prev. best move */ | |
if(y&M)break; /* board edge hit */ | |
m=E-S&b[E]&&y-E<2&E-y<2?I:m; /* bad castling */ | |
if(p<3&y==E)H^=16; /* shift capt.sqr. H if e.p.*/ | |
t=b[H];if(t&k|p<3&!(y-x&7)-!t)break; /* capt. own, bad pawn mode */ | |
i=37*w[t&7]+(t&192); /* value of capt. piece t */ | |
if(i<0)m=I,d=98; /* K capture */ | |
if(m>=l&d>1)goto C; /* abort on fail high */ | |
v=d-1?e:i-p; /*** MVV/LVA scoring if d=1**/ | |
if(d-!t>1) /*** all captures if d=2 ***/ | |
{v=p<6?b[x+8]-b[y+8]:0; /* center positional pts. */ | |
b[G]=b[H]=b[x]=0;b[y]=u|32; /* do move, set non-virgin */ | |
if(!(G&M))b[F]=k+6,v+=50; /* castling: put R & score */ | |
v-=p-4|R>30?0:20; /*** freeze K in mid-game ***/ | |
if(p<3) /* pawns: */ | |
{v-=9*((x-2&M||b[x-2]-u)+ /* structure, undefended */ | |
(x+2&M||b[x+2]-u)-1 /* squares plus bias */ | |
+(b[x^16]==k+36)) /*** cling to magnetic K ***/ | |
-(R>>2); /* end-game Pawn-push bonus */ | |
i+=V=y+r+1&S?647-p:2*(u&y+16&32); /* promotion / passer bonus */ | |
b[y]+=V; /* upgrade P or convert to Q*/ | |
} | |
v+=e+i;V=m>q?m:q; /*** new eval & alpha ****/ | |
C=d-1-(d>5&p>2&!t&!h); /* nw depth, reduce non-cpt.*/ | |
C=R>30|P-I|d<3||t&&p-4?C:d; /* extend 1 ply if in-check */ | |
do | |
s=C>2|v>V?-minimax(24-k,-l,-V,-v,/*** futility, recursive eval. of reply */ | |
F,y,C):v; | |
W(s>q&++C<d);v=s; /* no fail:re-srch unreduced*/ | |
if(z&8&&K-I) /* move pending: check legal*/ | |
{if(v+I&&x==K&y==L) /* if move found */ | |
{Q=-e-i;O=F; | |
R+=i>>7; /*** total captd material ***/ | |
//if((b[y]&7)!=p && PromPiece == 'n') UnderProm = y; | |
//if((b[y]&7)!=p) {printf("tellics kibitz promotion\n");fflush(stdout);}; | |
--Z;return l; /* & not in check, signal */ | |
} | |
v=m; /* (prevent fail-lows on */ | |
} /* K-capt. replies) */ | |
b[G]=k+6;b[F]=b[y]=0;b[x]=u;b[H]=t; /* undo move,G can be dummy */ | |
} /* if non-castling */ | |
if(v>m) /* new best, update max,best*/ | |
m=v,X=x,Y=y|S&F; /* mark non-double with S */ | |
if(h){h=0;goto A;} /* redo after doing old best*/ | |
if(x+r-y|u&32| /* not 1st step,moved before*/ | |
p>2&(p-4|j-7|| /* no P & no lateral K move,*/ | |
b[G=x+3^r>>1&7]-k-6 /* no virgin R in corner G, */ | |
||b[G^1]|b[G^2]) /* no 2 empty sq. next to R */ | |
)t+=p<5; /* fake capt. for nonsliding*/ | |
else F=y; /* enable e.p. */ | |
}W(!t); /* if not capt. continue ray*/ | |
}}}W((x=x+9&~M)-B); /* next sqr. of board, wrap */ | |
C:m=m+I|P==I?m:0; /*** check test thru NM best loses K: (stale)mate*/ | |
} /* encoded in X S,8 bits */ | |
if(z==S+1)K=X,L=Y&~M; | |
--Z;return m+=m<e; /* delayed-loss bonus */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment