•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Blog Entries

• phi

I love this community !

# A short Tic-Tac-Toe implementation in Pascal/Delphi

tic-tac-toe tic tac toe pascal short

### #1 medwatt

medwatt

CC Lurker

• Just Joined
• 1 posts

Posted 13 August 2013 - 03:43 PM

Hello,

I'm new to this forum. This will be my first post. I am a program enthusiast, not a programmer. I wrote this code just for fun. I made use of the fact that since Tic-Tac-Toe is played on a 3x3 grid any straight line will have a sum of 15 after 5 cells have been used. So it is just a matter of checking whether a player who has selected 3 or more cells has one of its combinations summing up to 15.

Here is the code and feel free to use it.

```program tictactoe_full;
uses crt;

var
M:array[1..9] of byte = (8,1,6,3,5,7,4,9,2);
choices:array[1..9] of byte;
player,count,i,cell,res1,res2:byte;
contin:char;

function check(player:byte):byte;
var start,elements:byte;
begin
if player=1 then begin
start:=1;elements:=count div 2 + 1;end
else begin start:=2;elements:=count div 2;end;
if elements>=3 then
check:=choices[start]+choices[start+2]+choices[start+4];
if (elements>3)and(check<>15)  then
check:=choices[start]+choices[start+2]+choices[start+6];
if (elements>3)and(check<>15)  then
check:=choices[start]+choices[start+4]+choices[start+6];
if (elements>3)and(check<>15)  then
check:=choices[start+2]+choices[start+4]+choices[start+6];
end;

procedure populate();
begin
writeln('Tic Tac Toe !!');
writeln;
writeln('---|---|---');
writeln(' 1 | 2 | 3 ');
writeln('---|---|---');
writeln(' 4 | 5 | 6 ');
writeln('---|---|---');
writeln(' 7 | 8 | 9 ');
writeln();
writeln('Select any number from 1..9');
writeln();
end;

procedure insert(letter:char);
var x,y:byte;
begin
case cell of
1..3: gotoxy(2+(cell-1)*4,4);
4..6: gotoxy(2+(cell-4)*4,6);
7..9: gotoxy(2+(cell-7)*4,8);
end;
writeln(letter); gotoxy(1,12);

end;

begin
repeat
clrscr;
res1:=0;res2:=0;i:=1;
populate;
repeat
player:=count mod 2;
if player=0 then write('Player X : ') else write('Player O : ');
if player=0 then insert('X') else insert('O');
choices[i]:=M[cell];
count := count + 1;
if (count>4) and (count<9) and (player =0) then res1:=check(1);
if (count>4) and (count<9) and (player =1) then res2:=check(2);
i:=i+1;
until (res1=15)or(res2=15)or(i>9);

if res1=15 then writeln('Player X WINS !!')
else if res2=15 then writeln('Player O WINS !!')
else writeln('The game was a DRAW');
write('Continue ?? Press ''n'' to stop : ');
until(contin<>'y');
end.

```

• 1

### #2 Luthfi

Luthfi

• Expert Member
• 1320 posts
• Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
• Learning:C, Java, PHP

Posted 14 August 2013 - 07:55 PM

Hi Medwatt, welcome to CodeCall. I am glad there is another coder beside me post Delphi/Pascal tutorials. I wish you will be active in this forum.

Edit:

Could you introduce yourself in introduction forum?

Edited by Luthfi, 14 August 2013 - 07:57 PM.

• 0

### Also tagged with one or more of these keywords: tic-tac-toe, tic, tac, toe, pascal, short

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download