uses dos,crt; var board : array [0..9,0..9] of byte; board1 : array [0..9,0..9] of byte; board2 : array [0..9,0..9] of byte; boardt : array [0..9,0..9] of byte; u,v,w,x,y,z : integer; inp : integer; depth : byte; score : integer; optmove : array [1..2] of byte; optscore: integer; bestscore : integer; ins : set of 1..8; moves : integer; runs : integer; free : byte; p1,p2 : byte; procedure displayboard; begin; for y:=1 to 8 do for x:=1 to 8 do begin; if board[x,y]=0 then write('0'); if board[x,y]=1 then write('*'); if board[x,y]=9 then write('.'); if x=8 then writeln(' '); end; end; procedure countfree; var f1,f2 : byte; begin; free:=0; for f1:=1 to 8 do for f2:=1 to 8 do if board[f1,f2]=9 then free:=free+1; end; procedure inputboard; var c : char; begin; clrscr; for y:=1 to 4 do for x:=1 to 4 do begin; c:=readkey; if c='0' then board[x+2,y+2]:=0; if c='*' then board[x+2,y+2]:=1; if c='.' then board[x+2,y+2]:=9; write(c); if x=4 then writeln(' '); end; end; procedure getinputs; var s2 : string; code:integer; begin; readln(s2); val(s2[1],p1,code); val(s2[3],p2,code); p2:=9-p2; end; procedure getval; var code: integer; s1 : string; begin; readln(s1); val(s1,inp,code); end; procedure makemove(movx,movy : integer); var p : integer; col : integer; temp: integer; m1,m2 : integer; begin; if odd(moves+depth+1) then col:=1 else col:=0; p:=1; repeat temp:=boardt[movx,movy+p]; if temp=1-col then boardt[movx,movy+p]:=temp+3; p:=p+1; until ((movy+p) in ins) or (temp=col) or (temp=9); for m1:=1 to 8 do for m2:=1 to 8 do if (boardt[m1,m2]>=3) and (boardt[m1,m2]<=4) then if (temp<>col) then dec(boardt[m1,m2],3) else boardt[m1,m2]:=col; p:=1; repeat temp:=boardt[movx,movy-p]; if temp=1-col then boardt[movx,movy-p]:=temp+3; p:=p+1; until ((movy-p) in ins) or (temp=col) or (temp=9); for m1:=1 to 8 do for m2:=1 to 8 do if (boardt[m1,m2]>=3) and (boardt[m1,m2]<=4) then if (temp<>col) then dec(boardt[m1,m2],3) else boardt[m1,m2]:=col; p:=1; repeat temp:=boardt[movx+p,movy]; if temp=1-col then boardt[movx+p,movy]:=temp+3; p:=p+1; until ((movx+p) in ins) or (temp=col) or (temp=9); for m1:=1 to 8 do for m2:=1 to 8 do if (boardt[m1,m2]>=3) and (boardt[m1,m2]<=4) then if (temp<>col) then dec(boardt[m1,m2],3) else boardt[m1,m2]:=col; p:=1; repeat temp:=boardt[movx-p,movy]; if temp=1-col then boardt[movx-p,movy]:=temp+3; p:=p+1; until ((movx-p) in ins) or (temp=col) or (temp=9); for m1:=1 to 8 do for m2:=1 to 8 do if (boardt[m1,m2]>=3) and (boardt[m1,m2]<=4) then if (temp<>col) then dec(boardt[m1,m2],3) else boardt[m1,m2]:=col; p:=1; repeat temp:=boardt[movx+p,movy+p]; if temp=1-col then boardt[movx+p,movy+p]:=temp+3; p:=p+1; until ((movx+p) in ins) or (temp=col) or ((movy+p) in ins) or (temp=9); for m1:=1 to 8 do for m2:=1 to 8 do if (boardt[m1,m2]>=3) and (boardt[m1,m2]<=4) then if (temp<>col) then dec(boardt[m1,m2],3) else boardt[m1,m2]:=col; p:=1; repeat temp:=boardt[movx-p,movy-p]; if temp=1-col then boardt[movx-p,movy-p]:=temp+3; p:=p+1; until ((movx-p) in ins) or (temp=col) or ((movy-p) in ins) or (temp=9); for m1:=1 to 8 do for m2:=1 to 8 do if (boardt[m1,m2]>=3) and (boardt[m1,m2]<=4) then if (temp<>col) then dec(boardt[m1,m2],3) else boardt[m1,m2]:=col; p:=1; repeat temp:=boardt[movx-p,movy+p]; if temp=1-col then boardt[movx-p,movy+p]:=temp+3; p:=p+1; until ((movx-p) in ins) or (temp=col) or ((movy+p) in ins) or (temp=9); for m1:=1 to 8 do for m2:=1 to 8 do if (boardt[m1,m2]>=3) and (boardt[m1,m2]<=4) then if (temp<>col) then dec(boardt[m1,m2],3) else boardt[m1,m2]:=col; p:=1; repeat temp:=boardt[movx+p,movy-p]; if temp=1-col then boardt[movx+p,movy-p]:=temp+3; p:=p+1; until ((movx+p) in ins) or (temp=col) or ((movy-p) in ins) or (temp=9); for m1:=1 to 8 do for m2:=1 to 8 do if (boardt[m1,m2]>=3) and (boardt[m1,m2]<=4) then if (temp<>col) then dec(boardt[m1,m2],3) else boardt[m1,m2]:=col; boardt[movx,movy]:=col; end; procedure try(a,b : byte); var q,p : integer; col : integer; s,t : integer; begin; for p:=0 to 9 do for q:=0 to 9 do begin; if depth=1 then boardt[p,q]:=board[p,q]; if depth=2 then boardt[p,q]:=board1[p,q]; end; if odd(moves+depth+1) then col:=1 else col:=0; makemove(a,b); for p:=0 to 9 do for q:=0 to 9 do if depth=1 then board1[p,q]:=boardt[p,q]; if depth=2 then begin; col:=1-col; score:=0; for t:=1 to 8 do for s:=1 to 8 do if boardt[s,t]=col then score:=score+1; if score9 then z:=1; for u:=-1 to 1 do if board1[s+u,t+1]<>9 then z:=1; if board1[s+1,t]<>9 then z:=1; if board1[s-1,t]<>9 then z:=1; if (board1[s,t]=9) and (z=1) then try(s,t); end; depth:=1; end; begin; for x:=0 to 9 do for y:=0 to 9 do board[x,y]:=9; inputboard; writeln(' '); writeln('Strategy 2'); displayboard; writeln(' '); ins:=[0,9]; moves:=0; repeat; repeat getval until (inp=0) or (inp=-1) or (inp>=1); if inp>=1 then begin; for runs:=1 to inp do begin; optscore:=-10000; for y:=1 to 8 do for x:=1 to 8 do begin; depth:=1; bestscore:=10000; z:=0; for u:=-1 to 1 do if board[x+u,y-1]<>9 then z:=1; for u:=-1 to 1 do if board[x+u,y+1]<>9 then z:=1; if board[x+1,y]<>9 then z:=1; if board[x-1,y]<>9 then z:=1; if (board[x,y]=9) and (z=1) then try(x,y); if (bestscore>=optscore) and (bestscore<>10000) then begin; optscore:=bestscore; optmove[1]:=x; optmove[2]:=y; end; end; depth:=1; for x:=0 to 9 do for y:=0 to 9 do boardt[x,y]:=board[x,y]; makemove(optmove[1],optmove[2]); for x:=0 to 9 do for y:=0 to 9 do board[x,y]:=boardt[x,y]; inc(moves); countfree; if free=1 then begin; for y:=1 to 8 do for x:=1 to 8 do if board[x,y]=9 then begin; moves:=moves+0; makemove(x,y); for u:=0 to 9 do for v:=0 to 9 do board[u,v]:=boardt[u,v]; runs:=inp; free:=0; end; end; end; end; if inp=0 then begin; getinputs; depth:=1; for x:=0 to 9 do for y:=0 to 9 do boardt[x,y]:=board[x,y]; makemove(p1,p2); for x:=0 to 9 do for y:=0 to 9 do board[x,y]:=boardt[x,y]; depth:=0; inc(moves); countfree; if free=1 then begin; for y:=1 to 8 do for x:=1 to 8 do if board[x,y]=9 then begin; moves:=moves+0; makemove(x,y); for u:=0 to 9 do for v:=0 to 9 do board[u,v]:=boardt[u,v]; runs:=inp; free:=0; end; end; end; displayboard; if free=0 then begin for x:=1 to 8 do for y:=1 to 8 do begin; if board[x,y]=0 then p1:=p1+1; if board[x,y]=1 then p2:=p2+1; end; if p1>p2 then writeln('White wins by ',p1-p2); if p2>p1 then writeln('Black wins by ',p2-p1); if p1=p2 then writeln('Black and White Draw'); free:=100; inp:=-1; end; until inp=-1; end.