Skip to content

Instantly share code, notes, and snippets.

@Kaveh8
Last active August 31, 2023 00:26
Show Gist options
  • Save Kaveh8/648aa4dcad15ba1370370d286bfd339f to your computer and use it in GitHub Desktop.
Save Kaveh8/648aa4dcad15ba1370370d286bfd339f to your computer and use it in GitHub Desktop.
Graphical tic-tac-toe game (Computer vs Human) in Turbo Pascal
program tictactoe;{Kaveh Shahhosseini}
uses graph,crt,dos;
type
dooz=array[1..3,1..3]of integer;
const
directory='e:\tp\bgi';
var
driver,mode,size,i,j:integer; {For Graphic}
m:dooz; {Dooz Array}
n,y,w,q,p:byte;
t:string; {For Player Name}
sa,ma,ru,dw:word; {For Date}
procedure table; {Drawing Table}
begin
initgraph(driver,mode,directory);
setbkcolor(black); {For Backgroundcolor}
setfillstyle(11,4);
floodfill(200,225,4);
outtextxy(60,100,'PLAYER NAME:'); {For Showing Player Name}
outtextxy(160,100,t);
outtextxy(60,125,t);
outtextxy(125,125,'IS (X) AND COM IS (O)');
setcolor(white);
line(100,150,100,300); {Drawing Table}
line(150,150,150,300);
line(50,200,200,200);
line(50,250,200,250);
line(50,150,200,150);
line(50,150,50,300);
line(50,300,200,300);
line(200,150,200,300);
outtextxy(75,175,'1'); {For Showing Numbers In Table}
outtextxy(75,225,'4');
outtextxy(75,275,'7');
outtextxy(125,175,'2');
outtextxy(125,225,'5');
outtextxy(125,275,'8');
outtextxy(175,175,'3');
outtextxy(175,225,'6');
outtextxy(175,275,'9');
end;
procedure dayere(a,b:integer); {For Drawing Circle}
begin
for i:=1 to 20 do
circle(a,b,i);
end;
procedure draw1(n:byte); {For Knowing Where The Circle Must be Draw}
begin
if n=1 then
dayere(75,175)
else if n=2 then
dayere(125,175)
else if n=3 then
dayere(175,175)
else if n=4 then
dayere(75,225)
else if n=5 then
dayere(125,225)
else if n=6 then
dayere(175,225)
else if n=7 then
dayere(75,275)
else if n=8 then
dayere(125,275)
else if n=9 then
dayere(175,275);
end;
procedure zarbdar(a,b,c,d,e,f,g,h:integer); {For Drawing Cross}
begin
line(a,b,c,d);
line(e,f,g,h);
end;
procedure draw2(y:byte); {For Knowing Where The Cross Must Be Draw}
begin
if y=1 then
zarbdar(55,155,95,195,95,155,55,195)
else if y=2 then
zarbdar(105,155,145,195,145,155,105,195)
else if y=3 then
zarbdar(155,155,195,195,195,155,155,195)
else if y=4 then
zarbdar(55,205,95,245,95,205,55,245)
else if y=5 then
zarbdar(105,205,145,245,145,205,105,245)
else if y=6 then
zarbdar(155,205,195,245,195,205,155,245)
else if y=7 then
zarbdar(55,255,95,295,95,255,55,295)
else if y=8 then
zarbdar(105,255,145,295,145,255,105,295)
else if y=9 then
zarbdar(155,255,195,295,195,255,155,295)
end;
procedure attack; forward;
procedure turn(w:byte); forward;
procedure check; forward;
procedure error(v:byte);
begin
if v=1 then
outtextxy(100,370,'ERROR 1:PLEASE ENTER CORRECT NUMBER');
if v=2 then
outtextxy(100,400,'ERROR 2:INVALID NUMBER.PLEASE ENTER NUMBER BETWEEN 1-9');
turn(3);
end;
procedure winner(w:byte); {For Knowing Who Is Winner}
begin
if w=3 then
begin
settextstyle(8,horizdir,2);
outtextxy(225,150,'**COMPUTER IS WINNER**');
outtextxy(350,200,' LOSE');
outtextxy(270,200,t);
outtextxy(150,440,'PLEASE PRESS ENTER TO EXIT');
repeat until keypressed;
halt;
end
else if w=2 then
begin
settextstyle(8,horizdir,2);
outtextxy(250,150,t);
outtextxy(225,150,'** IS WINNER**');
outtextxy(225,200,' **CONGRATULATION** ');
outtextxy(160,440,'PLEASE PRESS ENTER TO EXIT');
repeat until keypressed;
halt;
end
else if w=4 then
begin
settextstyle(8,horizdir,2);
outtextxy(250,150,'**A DRAW GAME**');
outtextxy(160,440,'PLEASE PRESS ENTER TO EXIT');
repeat until keypressed;
halt;
end;
end;
procedure win(w:byte); {For Finding Winnig In Play}
begin
if m[1,1]=w then
if m[1,2]=w then
if m[1,3]=w then
winner(w);
if m[2,1]=w then
if m[2,2]=w then
if m[2,3]=w then
winner(w);
if m[3,1]=w then
if m[3,2]=w then
if m[3,3]=w then
winner(w);
if m[1,1]=w then
if m[2,1]=w then
if m[3,1]=w then
winner(w);
if m[1,2]=w then
if m[2,2]=w then
if m[3,2]=w then
winner(w);
if m[1,3]=w then
if m[2,3]=w then
if m[3,3]=w then
winner(w);
if m[1,1]=w then
if m[2,2]=w then
if m[3,3]=w then
winner(w);
if m[1,3]=w then
if m[2,2]=w then
if m[3,1]=w then
winner(w);
end;
procedure turn(w:byte); {For Knowing Who`s Turn Is It?}
var
r:integer;
begin
for i:=1 to 3 do
for j:=1 to 3 do
if m[i,j]=1 then begin
if w=3 then begin
readln(r);
if (1<=r) and (r<=9) then begin {For error 2}
if r=1 then
if m[1,1]=1 then {For error 1}
begin
m[1,1]:=2;
draw2(1);
win(2);
turn(2);
end
else error(1)
else if r=2 then
if m[1,2]=1 then
begin
m[1,2]:=2;
draw2(2);
win(2);
turn(2);
end
else error(1)
else if r=3 then
if m[1,3]=1 then
begin
m[1,3]:=2;
draw2(3);
win(2);
turn(2);
end
else error(1)
else if r=4 then
if m[2,1]=1 then
begin
m[2,1]:=2;
draw2(4);
win(2);
turn(2);
end
else error(1)
else if r=5 then
if m[2,2]=1 then
begin
m[2,2]:=2;
draw2(5);
win(2);
turn(2);
end
else error(1)
else if r=6 then
if m[2,3]=1 then
begin
m[2,3]:=2;
draw2(6);
win(2);
turn(2);
end
else error(1)
else if r=7 then
if m[3,1]=1 then
begin
m[3,1]:=2;
draw2(7);
win(2);
turn(2);
end
else error(1)
else if r=8 then
if m[3,2]=1 then
begin
m[3,2]:=2;
draw2(8);
win(2);
turn(2);
end
else error(1)
else if r=9 then
if m[3,3]=1 then
begin
m[3,3]:=2;
draw2(9);
win(2);
turn(2);
end
else error(1);
end
else error(2);
end
else if w=2 then
attack;
end;
if w=3 then begin
win(3);
winner(4); {For Realizing Draw}
end;
if w=2 then begin
win(2);
winner(4);
end;
end;
procedure check;
var
k,s:integer;
begin
k:=0;
s:=1;
for i:=1 to 3 do
s:=s*m[1,i];
if s=9 then
k:=k+1;
s:=1;
for i:=1 to 3 do
s:=s*m[2,i];
if s=9 then
k:=k+1;
s:=1;
for i:=1 to 3 do
s:=s*m[3,i];
if s=9 then
k:=k+1;
s:=1;
for i:=1 to 3 do
s:=s*m[i,1];
if s=9 then
k:=k+1;
s:=1;
for i:=1 to 3 do
s:=s*m[i,2];
if s=9 then
k:=k+1;
s:=1;
for i:=1 to 3 do
s:=s*m[i,3];
if s=9 then
k:=k+1;
s:=1;
for i:=1 to 3 do
s:=s*m[i,i];
if s=9 then
k:=k+1;
s:=1;
s:=m[1,3]*m[3,1]*m[2,2];
if s=9 then
k:=k+1;
if k=2 then Begin
settextstyle(8,horizdir,2);
outtextxy(250,50,'YOU WILL LOSE ');
outtextxy(435,50,t);
end;
end;
procedure attack;
var
i,j,s:integer;
begin
s:=1;
for i:=1 to 3 do
s:=m[i,i]*s;
if s=12 then
begin
if m[2,1]=1 then
begin
m[2,1]:=3;
check;
draw1(4);
win(3);
turn(3);
end
else if m[1,2]=1 then
begin
m[1,2]:=3;
check;
draw1(2);
win(3);
turn(3);
end
else if m[2,3]=1 then
begin
m[2,3]:=3;
check;
draw1(6);
win(3);
turn(3);
end
else if m[3,2]=1 then
begin
m[3,2]:=3;
check;
draw1(8);
win(3);
turn(3);
end;
end;
s:=m[1,3]*m[2,2]*m[3,1];
if s=12 then
begin
if m[2,1]=1 then
begin
m[2,1]:=3;
check;
draw1(4);
win(3);
turn(3);
end
else if m[1,2]=1 then
begin
m[1,2]:=3;
check;
draw1(2);
win(3);
turn(3);
end
else if m[2,3]=1 then
begin
m[2,3]:=3;
check;
draw1(6);
win(3);
turn(3);
end
else if m[3,2]=1 then
begin
m[3,2]:=3;
check;
draw1(8);
win(3);
turn(3);
end;
end;
begin
s:=0;
for i:=1 to 3 do
for j:=1 to 3 do
s:=m[i,j]+s;
if (s=10) and (m[2,2]=1) then
begin
m[2,2]:=3;
check;
draw1(5);
win(3);
turn(3);
end;
end;
s:=1;
for i:=1 to 3 do
s:=m[i,1]*s;
begin
if s=9 then
begin
for i:=1 to 3 do
begin
if m[i,1]=1 then
begin
m[i,1]:=3;
check;
if i=1 then
begin
draw1(1);
win(3);
turn(3);
end
else if i=2 then
begin
draw1(4);
win(3);
turn(3);
end
else begin
draw1(7);
win(3);
turn(3);
end;
end;
end;
end;
end;
s:=1;
for i:=1 to 3 do
s:=m[i,2]*s;
begin
if s=9 then
begin
for i:=1 to 3 do
begin
if m[i,2]=1 then
begin
m[i,2]:=3;
check;
if i=1 then
begin
draw1(2);
win(3);
turn(3);
end;
if i=2 then
begin
draw1(5);
win(3);
turn(3);
end;
if i=3 then
begin
draw1(8);
win(3);
turn(3);
end;
end;
end;
end;
end;
s:=1;
for i:=1 to 3 do
s:=m[i,3]*s;
begin
if s=9 then
begin
for i:=1 to 3 do
begin
if m[i,3]=1 then
begin
m[i,3]:=3;
check;
if i=1 then
begin
draw1(3);
win(3);
turn(3);
end;
if i=2 then
begin
draw1(6);
win(3);
turn(3);
end;
if i=3 then
begin
draw1(9);
win(3);
turn(3);
end;
end;
end;
end;
end;
s:=1;
for i:=1 to 3 do
s:=m[1,i]*s;
begin
if s=9 then
begin
for i:=1 to 3 do
begin
if m[1,i]=1 then
begin
m[1,i]:=3;
check;
draw1(i);
win(3);
turn(3);
end;
end;
end;
end;
s:=1;
for i:=1 to 3 do
s:=m[2,i]*s;
begin
if s=9 then
begin
for i:=1 to 3 do
begin
if m[2,i]=1 then
begin
m[2,i]:=3;
check;
draw1(i+3);
win(3);
turn(3);
end;
end;
end;
end;
s:=1;
for i:=1 to 3 do
s:=m[3,i]*s;
begin
if s=9 then
begin
for i:=1 to 3 do begin
if m[3,i]=1 then
begin
m[3,i]:=3;
check;
draw1(i+6);
win(3);
turn(3);
end;
end;
end;
end;
s:=1;
for i:=1 to 3 do
s:=m[i,i]*s;
begin
if s=9 then
begin
for i:=1 to 3 do
begin
if m[i,i]=1 then
begin
m[i,i]:=3;
check;
if i=1 then
begin
draw1(1);
win(3);
turn(3);
end;
if i=2 then
begin
draw1(5);
win(3);
turn(3);
end;
if i=3 then
begin
draw1(9);
win(3);
turn(3);
end;
end;
end;
end;
end;
s:=1;
begin
s:=m[1,3]*m[2,2]*m[3,1];
if s=9 then
begin
if m[1,3]=1 then
begin
m[1,3]:=3;
check;
draw1(3);
win(3);
turn(3);
end
else if m[2,2]=1 then
begin
m[2,2]:=3;
check;
draw1(5);
win(3);
turn(3);
end
else if m[3,1]=1 then
begin
m[3,1]:=3;
check;
draw1(7);
win(3);
turn(3);
end;
end;
end;
s:=1;
for i:=1 to 3 do
s:=m[i,1]*s;
begin
if s=4 then
begin
for i:=1 to 3 do
begin
if m[i,1]=1 then
begin
m[i,1]:=3;
check;
if i=1 then
begin
draw1(1);
win(3);
turn(3);
end;
if i=2 then
begin
draw1(4);
win(3);
turn(3);
end;
if i=3 then
begin
draw1(7);
win(3);
turn(3);
end;
end;
end;
end;
end;
s:=1;
for i:=1 to 3 do
s:=m[i,2]*s;
begin
if s=4 then
begin
for i:=1 to 3 do
begin
if m[i,2]=1 then
begin
m[i,2]:=3;
check;
if i=1 then
begin
draw1(2);
win(3);
turn(3);
end;
if i=2 then
begin
draw1(5);
win(3);
turn(3);
end;
if i=3 then
begin
draw1(8);
win(3);
turn(3);
end;
end;
end;
end;
end;
s:=1;
for i:=1 to 3 do
s:=m[i,3]*s;
begin
if s=4 then
begin
for i:=1 to 3 do
begin
if m[i,3]=1 then
begin
m[i,3]:=3;
check;
if i=1 then
begin
draw1(3);
win(3);
turn(3);
end;
if i=2 then
begin
draw1(6);
win(3);
turn(3);
end;
if i=3 then
begin
draw1(9);
win(3);
turn(3);
end;
end;
end;
end;
end;
s:=1;
for i:=1 to 3 do
s:=m[1,i]*s;
begin
if s=4 then
begin
for i:=1 to 3 do
begin
if m[1,i]=1 then
begin
m[1,i]:=3;
check;
draw1(i);
win(3);
turn(3);
end;
end;
end;
end;
s:=1;
for i:=1 to 3 do
s:=m[2,i]*s;
begin
if s=4 then
begin
for i:=1 to 3 do
begin
if m[2,i]=1 then
begin
m[2,i]:=3;
check;
draw1(i+3);
win(3);
turn(3);
end;
end;
end;
end;
s:=1;
for i:=1 to 3 do
s:=m[3,i]*s;
begin
if s=4 then
begin
for i:=1 to 3 do
begin
if m[3,i]=1 then
begin
m[3,i]:=3;
check;
draw1(i+6);
win(3);
turn(3);
end;
end;
end;
end;
s:=1;
for i:=1 to 3 do
s:=m[i,i]*s;
begin
if s=4 then
begin
for i:=1 to 3 do
begin
if m[i,i]=1 then
begin
m[i,i]:=3;
if i=1 then
begin
draw1(1);
win(3);
turn(3);
end;
if i=2 then
begin
draw1(5);
win(3);
turn(3);
end;
if i=3 then
begin
draw1(9);
win(3);
turn(3);
end;
end;
end;
end;
end;
begin
s:=1;
s:=m[1,3]*m[2,2]*m[3,1];
if s=4 then
begin
if m[1,3]=1 then
begin
m[1,3]:=3;
check;
draw1(3);
win(3);
turn(3);
end
else if m[2,2]=1 then
begin
m[2,2]:=3;
check;
draw1(5);
win(3);
turn(3);
end
else if m[3,1]=1 then
begin
m[3,1]:=3;
check;
draw1(7);
win(3);
turn(3);
end;
end;
end;
s:=1;
for i:=1 to 3 do
s:=s*m[i,i];
begin
if (s mod 2)<>0 then
begin
for i:=1 to 3 do
if m[i,i]=1 then
begin
m[i,i]:=3;
check;
if i=1 then
begin
draw1(1);
win(3);
turn(3);
end;
if i=2 then
begin
draw1(5);
win(3);
turn(3);
end;
if i=3 then
begin
draw1(9);
win(3);
turn(3);
end;
end;
end;
end;
begin
s:=1;
s:=m[1,3]*m[2,2]*m[3,1];
if (s mod 2)<>0 then
begin
if m[1,3]=1 then
begin
m[1,3]:=3;
check;
draw1(3);
win(3);
turn(3);
end
else if m[2,2]=1 then
begin
m[2,2]:=3;
check;
draw1(5);
win(3);
turn(3);
end
else if m[3,1]=1 then
begin
m[3,1]:=3;
check;
draw1(7);
win(3);
turn(3);
end;
end;
end;
begin
s:=0;
for i:=1 to 3 do
for j:=1 to 3 do
s:=m[i,j]+s;
if (s=10) and (m[2,2]=2) then
begin
m[1,1]:=3;
check;
draw1(1);
win(3);
turn(3);
end;
end;
s:=0;
for i:=1 to 3 do
for j:=1 to 3 do
s:=m[i,j]+s;
if (s=13) and (m[3,3]=2) then
begin
m[3,1]:=3;
check;
draw1(7);
win(3);
turn(3);
end;
for i:=1 to 3 do
for j:=1 to 3 do
begin
if m[i,j]=1 then
begin
if i=1 then
begin
if j=1 then
begin
m[i,j]:=3;
check;
draw1(1);
win(3);
turn(3);
end
else if j=2 then
begin
m[i,j]:=3;
check;
draw1(2);
win(3);
turn(3);
end
else if j=3 then
begin
m[i,j]:=3;
check;
draw1(3);
win(3);
turn(3);
end;
end
else if i=2 then
begin
if j=1 then
begin
m[i,j]:=3;
check;
draw1(4);
win(3);
turn(3);
end
else if j=2 then
begin
m[i,j]:=3;
check;
draw1(5);
win(3);
turn(3);
end
else
begin
m[i,j]:=3;
check;
draw1(6);
win(3);
turn(3);
end;
end
else
begin
if j=1 then
begin
m[i,j]:=3;
check;
draw1(7);
win(3);
turn(3);
end
else if j=2 then
begin
m[i,j]:=3;
check;
draw1(8);
win(3);
turn(3);
end
else
begin
m[i,j]:=3;
check;
draw1(9);
win(3);
turn(3);
end;
end;
end;
end;
end;
begin {Main}
clrscr;
textcolor(red);
getdate(sa,ma,ru,dw); {For Getting Date From System}
gotoxy(10,35);
writeln('CURRENT DATE: ',ru,'/',ma,'/',sa); {For Showing Date}
gotoxy(10,10);
textcolor(green);
writeln('THIS PROJECT DESIGNED BY SIBILIZATION GROUP'); {First Page}
gotoxy(10,15);
writeln('FOR PLAYING, USE NUMBER KEYS');
gotoxy(10,20);
textcolor(white);
writeln('IF YOU WANT TO PLAY PLEASE PRESS ENTER');
repeat until keypressed;
gotoxy(10,25);
write('PLAYER-CPU:1 CPU-PLAYER:2'); {For Knowing Who Is The First Player}
readln(q);
gotoxy(10,30);
textcolor(red);
write('PLEASE WRITE YOUR NAME:'); {Getting Name}
gotoxy(34,30);
readln(t);
for i:=1 to 3 do
for j:=1 to 3 do
m[i,j]:=1;
table;
if q=2 then {Player Is 2 In This Project}
turn(2)
else if q=1 then
turn(3); {Computer Is 3 In This Project}
end. {End Of Project,Main}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment