program tris;
uses crt;
var j,k,i,a,b,cont,numero,posizione:integer;
tasto:char;
log:array[1..9] of integer;
x,y,cancella,nome,s:string;
finito,vinx,viny,fine,finegioco,sovrascrizione:boolean;
function num_log(u,r:integer):integer;
begin
if (u=4) and (r=2) then num_log:=1;
if (u=12) and (r=2) then num_log:=2;
if (u=20) and (r=2) then num_log:=3;
if (u=4) and (r=7) then num_log:=4;
if (u=12) and (r=7) then num_log:=5;
if (u=20) and (r=7) then num_log:=6;
if (u=4) and (r=12) then num_log:=7;
if (u=12) and (r=12) then num_log:=8;
if (u=20) and (r=12) then num_log:=9;
end;
procedure intestazione;
begin
writeln('BENVENUTO NEL GIOCO DEL TRIS');
writeln();
write('come ti chiami 1 giocatore? ');
readln(x);
write('come ti chiami 2 giocatore? ');
readln(y);
clrscr;
end;
procedure no_soprascritto;
begin
i:=num_log(a,b);
if log[i]=2
then sovrascrizione:=true;
end;
procedure caricare;
begin
i:=0;
for i:=1 to 9 do
begin
log[i]:=2;
end;
i:=0;
cancella:=' ';
cont:=0;
vinx:=false;
viny:=false;
fine:=false;
end;
procedure scritte;
begin
gotoxy(2,15);
writeln(cancella);
gotoxy(2,15);
writeln('tocca al giocatore ',numero,': ',nome,' premi "a" per confermare');
if (vinx=true) or (viny=true)
then
begin
gotoxy(2,15);
writeln(cancella);
gotoxy(2,15);
writeln('IL GIOCATORE ',numero,': ',nome,', HA VINTO!');
writeln();
end
else gotoxy(a,b);
if (fine) and (vinx=false) and (viny=false)
then
begin
gotoxy(2,15);
writeln(cancella);
gotoxy(2,15);
writeln('PARTITA PARI');
writeln();
end;
if (sovrascrizione=false) and (cont<>1)
then
begin
gotoxy(2,15);
writeln(cancella);
gotoxy(2,15);
writeln('te volevi fare il furbo, ma mi no ghe casco mia');
gotoxy(a,b);
end;
end;
procedure check;
begin
vinx:=false;
if (log[1]=1) and (log[2]=1) and (log[3]=1)
then vinx:=true;
if (log[4]=1) and (log[5]=1) and (log[6]=1)
then vinx:=true;
if (log[7]=1) and (log[8]=1) and (log[9]=1)
then vinx:=true;
if (log[1]=1) and (log[5]=1) and (log[9]=1)
then vinx:=true;
if (log[3]=1) and (log[5]=1) and (log[7]=1)
then vinx:=true;
if (log[2]=1) and (log[5]=1) and (log[8]=1)
then vinx:=true;
if (log[1]=1) and (log[4]=1) and (log[7]=1)
then vinx:=true;
if (log[3]=1) and (log[6]=1) and (log[9]=1)
then vinx:=true;
viny:=false;
if (log[1]=0) and (log[2]=0) and (log[3]=0)
then viny:=true;
if (log[4]=0) and (log[5]=0) and (log[6]=0)
then viny:=true;
if (log[7]=0) and (log[8]=0) and (log[9]=0)
then viny:=true;
if (log[1]=0) and (log[5]=0) and (log[9]=0)
then viny:=true;
if (log[3]=0) and (log[5]=0) and (log[7]=0)
then viny:=true;
if (log[2]=0) and (log[5]=0) and (log[8]=0)
then viny:=true;
if (log[1]=0) and (log[4]=0) and (log[7]=0)
then viny:=true;
if (log[3]=0) and (log[6]=0) and (log[9]=0)
then viny:=true;
end;
procedure struttura;
begin
for i:=1 to 2 do
begin
for j:=1 to 4 do
begin
writeln(' | | ');
end;
writeln('-----------------------');
end;
for k:=1 to 4 do
begin
writeln(' | | ');
end;
gotoxy(4,2);
end;
procedure croce_o_cerchio;
begin
if cont mod 2 =1
then
begin
gotoxy(a,b);
writeln('\/');
gotoxy(a,(b+1));
writeln('/\');
gotoxy(a,b);
end
else
begin
gotoxy(a,b);
writeln('OO');
gotoxy(a,(b+1));
writeln('OO');
gotoxy(a,b);
end;
end;
procedure controllosefinito;
begin
if (log[1]<>2) and (log[2]<>2) and (log[3]<>2) and (log[4]<>2) and (log[5]<>2) and (log[6]<>2) and (log[7]<>2) and (log[8]<>2) and (log[9]<>2)
then fine:=true;
end;
procedure movimento;
begin
if cont=1
then
begin
b:=2;
a:=4;
gotoxy(4,2);
end;
finito:=false;
repeat
tasto:=readkey;
if tasto=char(0)
then tasto:=readkey;
if tasto=char(77)
then
begin
a:=a+8;
if a=28
then a:=20
else gotoxy(a,b);
end;
if tasto=char(75)
then
begin
a:=a-8;
if a=-4
then a:=4
else gotoxy(a,b);
end;
if tasto=char(72)
then
begin
b:=b-5;
if b=-3
then b:=2
else gotoxy(a,b);
end;
if tasto=char(80)
then
begin
b:=b+5;
if b=17
then b:=12
else gotoxy(a,b);
end;
if tasto='a'
then finito:=true;
until finito;
end;
begin
finegioco:=false;
repeat
clrscr;
intestazione;
caricare;
struttura;
repeat
cont:=cont+1;
if cont mod 2=1
then
begin
nome:=x;
numero:=1;
end
else
begin
nome:=y;
numero:=2;
end;
repeat
scritte;
movimento;
sovrascrizione:=false;
no_soprascritto;
until sovrascrizione=true;
croce_o_cerchio;
posizione:= num_log(a,b);
log[posizione]:=cont mod 2;
check;
controllosefinito;
scritte;
until (vinx=true) or (viny=true) or (fine);
gotoxy(2,17);
writeln(cancella);
gotoxy(2,17);
writeln('premere "n" per uscire oppure un tasto qualsiasi per continuare');
readln(s);
if s='n'
then finegioco:=true;
until finegioco=true;
end.