{$A+,B-,D+,E+,F+,G-,I+,L+,N-,O-,P-,Q+,R+,S+,T-,V+,X+,Y+} {$M 65520,0,655360} program gkernel; uses crt,dos; label 1; type Digits = set of 0..9; const maxx=6; maxy=6; maxn=3; man:array[0..1] of byte=($8f,$9f); lubatud:array[0..1] of digits=([1,2,3,4,5],[2,3,4,5,6]); {Veateated:(*1 ja *(-1) va"ljendab seda, mitmendalt ma"ngijalt viga tuli..) 1-seis ebakorrektne } type lauatyyp = array[0..maxx+1,0..maxy+1] of integer; algseisutyyp = array[1..maxx] of shortint; ajatyyp = record tund , min , sek , saj: word; end; kaigutyyp = string[4]; turniirityyp = record xpunktid , ypunktid :integer; xaeg , yaeg : ajatyyp; end; var storelaud,laud:lauatyyp; algseis1,algseis2:algseisutyyp; step1,step2:kaigutyyp; mangija:array[1..maxn] of record startup:procedure(pool:integer;var algseis:algseisutyyp;var usrid:string); step:procedure(pool:integer;seis:lauatyyp;var kaik:kaigutyyp;var usrid:string); end; usrid:array[1..2] of string; mangija1,mangija2:byte; aeg:array[0..2] of longint; bb:ajatyyp; tick:byte; tabel:array[1..maxn,1..maxn] of turniirityyp; viga:integer; mainx,mainy:integer; kellaintvec:procedure; oldexitproc:pointer; li:longint; Procedure ekrmode(n:byte); {vahetab ekraanimoodi (n:0=16;1=8+vilkuvad taustavarvid)} var regs:registers; Begin fillchar(regs,sizeof(regs),#0); Case n Of 0:Begin regs.bl:=0; regs.ah:=$10; regs.al:=3; End; 1:Begin regs.bl:=1; regs.ah:=$10; regs.al:=3; End; End; Intr($10,Regs); End; function sgn(i:integer):byte; begin if i<0 then sgn:=1 else sgn:=0 end; function punkte(pool:integer;laud:lauatyyp):integer; {see protseduur arvutab kysitava ma"ngija punktiseisu} var a,b,p:integer; begin p:=0; for a:=1 to maxx do for b:=1 to maxy do if (laud[a,b]<>0) and (sgn(laud[a,b])=sgn(pool)) then begin if b in lubatud[sgn(pool)] then p:=p+abs(laud[a,b]) else p:=p+3*abs(laud[a,b]) end; punkte:=p; end; (************************************************) procedure kaik1(pool:integer;seis:lauatyyp;var kaik:kaigutyyp;var usrid:string); var a,b,r, i,j:integer; begin usrid:=''; repeat r:=0; for a:=1 to maxx do for b:=1 to maxy do if (sgn(pool)=sgn(seis[a,b])) and (seis[a,b]<>0) then begin inc(r); end; if r=0 then begin kaik:='0000'; exit end; r:=random(r)+1; for a:=1 to maxx do begin for b:=1 to maxy do if (sgn(pool)=sgn(seis[a,b])) and (seis[a,b]<>0) then begin dec(r); if r=0 then break end; if r=0 then break end; until (b in lubatud[sgn(pool)]) and (seis[a,b]<>0) and (sgn(seis[a,b])=sgn(pool)); i:=a; j:=b; r:=random(abs(seis[a,b]))+1; if abs(seis[i,j])=1 then begin if (seis[a-1,b+pool]<>0) and (sgn(seis[a-1,b+pool])<>sgn(seis[i,j])) then begin a:=a-1; b:=b+pool; end else if (seis[a+1,b+pool]<>0) and (sgn(seis[a+1,b+pool])<>sgn(seis[i,j])) then begin a:=a+1; b:=b+pool; end else if seis[a,b+pool]=0 then b:=b+pool else begin kaik1(pool,seis,kaik,usrid); exit; end; end else begin while (r>0) and (b in lubatud[sgn(pool)]) do begin b:=b+pool; dec(r); if seis[a,b]<>0 then begin r:=0; if sgn(seis[i,j])=sgn(seis[a,b]) then b:=b-pool; end; end; if j=b then begin kaik1(pool,seis,kaik,usrid); exit; end; end; delay(500); kaik:=chr(64+i)+chr(48+j)+chr(64+a)+chr(48+b); end; procedure nupupaigutus1(pool:integer;var algseis:algseisutyyp;var usrid:string); begin usrid:=''; algseis[1]:=pool*1; algseis[2]:=pool*2; algseis[3]:=pool*1; algseis[4]:=pool*3; algseis[5]:=pool*1; algseis[6]:=pool*4; end; (************************************************) procedure nupupaigutus2(pool:integer; var rida:algseisutyyp; var usrid:string); var i:integer; begin usrid:='TRIVI'; for i:=1 to 6 do rida[i]:=pool; rida[random(6)+1]:=pool*4; repeat i:=random(6)+1; until rida[i]=pool; rida[i]:=pool*3; repeat i:=random(6)+1; until rida[i]=pool; rida[i]:=pool*2; end; procedure kaik2(pool:integer; seis:lauatyyp; var teen:kaigutyyp; var usrid:string); type vali=string[2]; var i,x:char; j,y:integer; maxv:integer; maxm,maxs:vali; function ci2v(i:char; j:integer):vali; begin ci2v:=i+chr(j+48); end; procedure kasl(x:char; y,pool:integer); var rida:integer; begin if ((pool=1) and (y=1)) or ((pool=-1) and (y=6)) then exit; if (x<>'a') and (seis[ord(pred(x))-96,y-pool]*pool=1) then begin maxs:=ci2v(x,y); maxm:=ci2v(pred(x),y-pool); maxv:=seis[ord(x)-96,y]*pool; exit; end; if (x<>'f') and (seis[ord(succ(x))-96,y-pool]*pool=1) then begin maxs:=ci2v(x,y); maxm:=ci2v(succ(x),y-pool); maxv:=seis[ord(x)-96,y]*pool; exit; end; rida:=y-pool; while (seis[ord(x)-96,rida]=0) and (rida in [1..6]) do rida:=rida-pool; if not (rida in [1..6]) then exit; if seis[ord(x)-96,rida]*pool=1 then exit; if (seis[ord(x)-96,rida]*pooly do begin maxs:=''; kasl(x,j,-pool); if maxs='' then begin teen:=ci2v(x,y)+ci2v(x,j); proovik:=true; exit; end; j:=j-pool; end; end; begin usrid:='TRIVI'; delay(500); maxv:=0; for i:='a' to 'f' do for j:=1 to 6 do if pool*seis[ord(i)-96,j]0 then begin teen:=maxm+maxs; exit; end; for x:='a' to 'f' do for y:=1 to 6 do if (seis[ord(x)-96,y]*pool=4) and proovik(x,y,4) then exit; for x:='a' to 'f' do for y:=1 to 6 do if (seis[ord(x)-96,y]*pool=3) and proovik(x,y,3) then exit; for x:='a' to 'f' do for y:=1 to 6 do if (seis[ord(x)-96,y]*pool=2) and proovik(x,y,2) then exit; for x:='a' to 'f' do for y:=1 to 6 do if (seis[ord(x)-96,y]*pool=1) and proovik(x,y,1) then exit; for x:='a' to 'f' do for y:=1 to 6 do if (seis[ord(x)-96,y]*pool=1) and saab(x,y) then exit; for x:='a' to 'f' do for y:=1 to 6 do if (seis[ord(x)-96,y]*pool=2) and saab(x,y) then exit; for x:='a' to 'f' do for y:=1 to 6 do if (seis[ord(x)-96,y]*pool=3) and saab(x,y) then exit; for x:='a' to 'f' do for y:=1 to 6 do if (seis[ord(x)-96,y]*pool=4) and saab(x,y) then exit; end; (************************************************) procedure nupupaigutus3(pool:integer;var algseis:algseisutyyp;var usrid:string); var m:string; i:integer; begin gotoxy(49,8); if pool=1 then textattr:=man[0] else textattr:=man[1]; write('Anna algseis '); readln(m); normvideo; gotoxy(49,8); write(' '); for i:=1 to maxx do algseis[i]:=pool*(ord(m[i])-48); usrid:='HUMAN'; end; procedure kaik3(pool:integer;seis:lauatyyp;var kaik:kaigutyyp;var usrid:string); var k:string; begin gotoxy(15,20); if pool=1 then textattr:=man[0] else textattr:=man[1]; write('Sinu käik? '); readln(kaik); normvideo; gotoxy(15,20); write(' '); usrid:='HUMAN'; end; (************************************************) procedure initsym; begin end; procedure oot; begin gotoxy(20,15); textattr:=$C; write('Vajuta klahvile'); normvideo; readkey; gotoxy(20,15); write(' '); end; procedure ootl; begin gotoxy(50,10); write(' '); gotoxy(20,17); textattr:=$A; write('Mäng läbi!'); normvideo; oot; gotoxy(20,17); write(' '); end; procedure joonestalaud(laud:lauatyyp); var i,j:integer; begin gotoxy(1,1); normvideo; writeln('┌───┬───┬───┬───┬───┬───┐'); for i:=1 to 6 do begin writeln('│ │ │ │ │ │ │',7-i); if i<>6 then writeln('├───┼───┼───┼───┼───┼───┤'); end; writeln('└───┴───┴───┴───┴───┴───┘'); writeln(' a b c d e f'); for i:=1 to 6 do for j:=1 to 6 do if laud[i,j]<>0 then begin if laud[i,j]>0 then textattr:=man[0] else textattr:=man[1]; gotoxy(i*4-2,14-j*2); write(' ',abs(laud[i,j]),' '); end; normvideo; gotoxy(60,1); write('Seis on ',punkte(1,laud):2,':',punkte(-1,laud):2); end; function chekiseisu(alg:algseisutyyp; pool:integer):boolean; const vaja:array[1..4] of integer=(3,1,1,1); var on:array[1..4] of integer; i:integer; begin chekiseisu:=false; for i:=1 to 4 do on[i]:=0; for i:=1 to maxx do begin if (pool*alg[i]<0) or (abs(alg[i])>4) then begin chekiseisu:=true; exit; end; inc(on[abs(alg[i])]); end; for i:=1 to 4 do if on[i]<>vaja[i] then begin chekiseisu:=true; exit; end; end; function voimalik(pool:integer; laud:lauatyyp):boolean; var i:integer; j:integer; viim:integer; begin voimalik:=false; if pool=1 then viim:=6 else viim:=1; for i:=1 to 6 do for j:=1 to 6 do if (j<>viim) and (laud[i,j]*pool>0) then begin if ((laud[i,j]*pool=1) and ((laud[i,j+pool]=0) or ((i<>1) and (laud[i-1,j+pool]*pool<0)) or ((i<>maxx) and (laud[i+1,j+pool]*pool<0)))) or ((laud[i,j]*pool>1) and (laud[i,j+pool]*pool<1)) then begin voimalik:=true; exit; end; end; end; {function voimalik(pool:integer;laud:lauatyyp):boolean; var r,a,b:integer; begin voimalik:=true; r:=0; for a:=1 to maxx do for b:=1 to maxy do begin if (laud[a,b]<>0) and (sgn(laud[a,b])=sgn(pool)) and (b in lubatud[sgn(pool)]) then begin if abs(laud[a,b])=1 then begin if (laud[a-1,b+pool]<>0) and (sgn(laud[a-1,b+pool])<>sgn(pool)) then inc(r) else if (laud[a+1,b+pool]<>0) and (sgn(laud[a-1,b+pool])<>sgn(pool)) then inc(r) else if (laud[a,b+pool]=0) then inc(r) end else if laud[a,b+pool]=0 then inc(r) else if (laud[a,b+pool]<>0) and (sgn(laud[a,b])<>sgn(laud[a,b+pool])) then inc(r) end end; voimalik:=(r>0); end; } function chekikaiku(pool:integer;step1:kaigutyyp;laud:lauatyyp):boolean; var x1,y1,x2,y2:integer; i:integer; nupp:integer; begin chekikaiku:=false; x1:=ord(upcase(step1[1]))-64; y1:=ord(step1[2])-48; x2:=ord(upcase(step1[3]))-64; y2:=ord(step1[4])-48; if not ((x1 in [1..maxx]) and (x2 in [1..maxx]) and (y1 in [1..maxy]) and (y2 in [1..maxy])) then begin chekikaiku:=true; exit; end; if (laud[x1,y1]*pool<=0) or ((y2-y1)*pool<=0) then begin chekikaiku:=true; exit; end; nupp:=laud[x1,y1]*pool; if x1<>x2 then begin if not ((nupp=1) and (abs(x1-x2)=1) and ((y2-y1)*pool=1) and (laud[x2,y2]*pool<0)) then chekikaiku:=true; exit; end; i:=y1+pool; while true do begin if abs(i-y1)>nupp then begin chekikaiku:=true; exit; end; if ((nupp=1) or (i<>y2)) and (laud[x1,i]<>0) then begin chekikaiku:=true; exit; end; if (i=y2) and (laud[x1,i]*pool>0) then begin chekikaiku:=true; exit; end; if i=y2 then break else i:=i+pool; end; end; function Zero(w : Word) : kaigutyyp; var s : kaigutyyp; begin Str(w:0,s); if Length(s) = 1 then s := '0' + s; Zero := s; end; procedure teekaik(pool:integer;kaik:kaigutyyp;var laud:lauatyyp); var x1,y1,x2,y2:integer; ss:string; begin x1:=ord(upcase(kaik[1]))-64; y1:=ord(kaik[2])-48; x2:=ord(upcase(kaik[3]))-64; y2:=ord(kaik[4])-48; if laud[x2,y2]=0 then ss:='-' else ss:=':'; ss:=concat(' ',chr(abs(laud[x1,y1])+48),' > ',copy(kaik,1,2),ss,copy(kaik,3,2),' '); if laud[x2,y2]<>0 then ss:=concat(ss,'X',chr(abs(laud[x2,y2])+48),'X '); if not (y2 in lubatud[sgn(pool)]) then ss:=concat(ss,#1); gotoxy(50,10); write(' '); gotoxy(50,10); if pool=1 then textattr:=man[0] else textattr:=man[1]; writeln('Tehti käik ',ss); normvideo; laud[x2,y2]:=laud[x1,y1]; laud[x1,y1]:=0 end; procedure start(x,y:integer;var laud:lauatyyp); var a:integer; begin fillchar(laud,sizeof(laud),#0); fillchar(aeg,sizeof(aeg),#0); mangija[x].startup(1,algseis1,usrid[1]); mangija[y].startup(-1,algseis2,usrid[2]); gotoxy(1,22); for a:=1 to 2 do begin textattr:=man[a-1]; writeln(a,'. mängija: ',usrid[a]); end; normvideo; if chekiseisu(algseis1,1) then begin viga:=1; exit; end; if chekiseisu(algseis2,-1) then begin viga:=-1; exit; end; for a:=1 to maxx do begin laud[a,1]:=algseis1[a]; laud[a,maxy]:=algseis2[a]; end; end; procedure koorik(x,y:integer); begin viga:=0; start(x,y,storelaud); if viga<>0 then exit; joonestalaud(storelaud); oot; with bb do begin gettime(tund,min,sek,saj); aeg[0]:=saj+longint(sek)*100+longint(min)*10000+longint(tund)*1000000; end; tick:=0; repeat if voimalik(1,storelaud) then begin laud:=storelaud; tick:=1; mangija[x].step(1,laud,step1,usrid[1]); tick:=0; if chekikaiku(1,step1,storelaud) then begin viga:=2; gotoxy(15,18); write('Esimene mängija tegi lubamatu käigu ',step1); oot; gotoxy(15,18); write(' '); exit; end; teekaik(1,step1,storelaud); end else break; joonestalaud(storelaud); if voimalik(-1,storelaud) then begin laud:=storelaud; tick:=2; mangija[y].step(-1,laud,step2,usrid[2]); tick:=0; if chekikaiku(-1,step2,storelaud) then begin viga:=2; write('Teine mängija tegi lubamatu käigu ',step2); oot; gotoxy(15,18); write(' '); exit; end; teekaik(-1,step2,storelaud); end else break; joonestalaud(storelaud); until false; end; procedure proge; label 1; begin if abs(viga)=100 then goto 1; for mainx:=1 to maxn do for mainy:=1 to maxn do if mainx<>mainy then begin koorik(mainx,mainy); joonestalaud(storelaud); ootl; 1: if abs(viga)=100 then begin ootl; viga:=0; end; {turniiriarvestus....} end; halt; end; procedure valjumine; begin if exitcode=3 then proge else begin exitproc:=oldexitproc; Writeln('No oli ju lōbus!?!'); halt; end; end; begin GetIntVec($1c,@KellaIntVec); oldexitproc:=exitproc; exitproc:=@valjumine; (************************************************) (* System initialization *) (************************************************) @mangija[1].startup:=addr(nupupaigutus1); @mangija[1].step:=addr(kaik1); @mangija[2].startup:=addr(nupupaigutus2); @mangija[2].step:=addr(kaik2); @mangija[3].startup:=addr(nupupaigutus3); @mangija[3].step:=addr(kaik3); randomize; initsym; (************************************************) (* System initialized *) (************************************************) randomize; ekrmode(0); clrscr; proge; ekrmode(1); end.