Category : C Source Code
Archive   : TKTKTOE.ZIP
Filename : TKTKTOE.PAS
Output of file : TKTKTOE.PAS contained in archive : TKTKTOE.ZIP
Const PLUS_INFINITY : Integer = MaxInt;
MINUS_INFINITY : Integer = -MaxInt;
Type BoardType = array[1..3] of array[1..3] of Char;
{-------------------------------}
Function eval_3_positions(c1,c2,c3 : Char) : Integer;
begin
{ check if only one piece: no big deal }
if (c1 = 'X') or (c2 = 'X') or (c3 = 'X') then eval_3_positions := -1;
if (c1 = 'O') or (c2 = 'O') or (c3 = 'O') then eval_3_positions := 1;
if ((c1='X') and (c2='X')) or { check for 2 pieces: time to worry }
((c1='X') and (c3='X')) or
((c2='X') and (c3='X')) then eval_3_positions := -5;
if ((c1='O') and (c2='O')) or
((c1='O') and (c3='O')) or
((c2='O') and (c3='O')) then eval_3_positions := 5;
if ((c1='X') or (c2='X') or (c3='X')) and { mixed row: can't win }
((c1='O') or (c2='O') or (c3='O')) then eval_3_positions := 0;
{ all blank: who cares }
if (c1 = ' ') and (c2 = ' ') and (c3 = ' ') then eval_3_positions := 0;
{ winner: HOT! }
if (c1 = 'X') and (c2 = 'X') and (c3 = 'X') then eval_3_positions := -100;
if (c1 = 'O') and (c2 = 'O') and (c3 = 'O') then eval_3_positions := 100;
end;
{-------------------------------}
Function eval_board( VAR bd : Boardtype ) : Integer;
Var row,col,temp_val : Integer;
bd_val : Integer;
begin
bd_val := 0;
for row := 1 to 3 do begin { check each up&down }
temp_val := eval_3_positions( bd[row,1],bd[row,2],bd[row,3] );
if (temp_val = 100) or (temp_val = -100) then begin { if a winner }
eval_board := temp_val;
EXIT;
end;
bd_val := bd_val + temp_val;
end;
for col := 1 to 3 do begin { check each across }
temp_val := eval_3_positions(bd[1,col],bd[2,col],bd[3,col]);
if (temp_val = 100) or (temp_val = -100) then begin { if a winner }
eval_board := temp_val;
EXIT;
end;
bd_val := bd_val + temp_val;
end;
temp_val := eval_3_positions(bd[1,1],bd[2,2],bd[3,3]); { check \ diag }
if (temp_val = 100) or (temp_val = -100) then begin { if a winner }
eval_board := temp_val;
EXIT;
end;
bd_val := bd_val + temp_val;
temp_val := eval_3_positions(bd[1,3],bd[2,2],bd[3,1]); { check / diag }
if (temp_val = 100) or (temp_val = -100) then begin { if a winner }
eval_board := temp_val;
EXIT;
end;
bd_val := bd_val + temp_val;
eval_board := bd_val;
end;
{-------------------------------------------}
Function win_check(bd : BoardType) : Char;
Var value : Integer;
begin
win_check := ' ';
value := eval_board(bd);
if (value = -100) then win_check := 'X';
if (value = 100) then win_check := 'O';
end;
{-------------------------------------------}
Procedure computer_move( VAR bd : BoardType );
Var o_row,x_row,o_col,x_col,save_row,save_col : Integer;
level_1,level_2 : Integer;
board_val : Integer;
begin
level_1 := MINUS_INFINITY;
{ put 'O' in blank spaces and evaluate board }
for o_row := 1 to 3 do begin
for o_col := 1 to 3 do begin
{ only do something if the position is blank }
if (bd[o_row,o_col] = ' ') then begin
bd[o_row,o_col] := 'O';
if (win_check(bd) = 'O') then EXIT; { comp wins! do it. }
{ make all possible 'X' moves & evaluate each one }
level_2 := PLUS_INFINITY; { play all counter-moves }
for x_row := 1 to 3 do begin
for x_col := 1 to 3 do begin
if (bd[x_row,x_col] = ' ') then begin
bd[x_row,x_col] := 'X';
board_val := eval_board(bd);
if (board_val < level_2) then level_2 := board_val;
bd[x_row,x_col] := ' ';
end;
end;
end;
{ check for the greatest level_2 value }
if (level_2 > level_1) then begin { i.e. best for comp }
level_1 := level_2;
save_row := o_row;
save_col := o_col;
end;
{ set 'O' back to blank }
bd[o_row,o_col] := ' ';
end;
end;
end;
bd[save_row,save_col] := 'O'; { play the move }
end;
{-------------------------------------------}
procedure human_move( VAR bd : boardtype);
Var row,col : Integer;
Work : String;
begin
Repeat
Repeat
Work := ' ';
Write('Please enter a row (A-C) : ');
Readln( Work );
Row := Ord(UpCase(Work[1]))-64;
Until (Row >= 1) and (Row <= 3);
Repeat
Work := ' ';
Write('Please enter a column (1-3) : ');
Readln( Work );
Col := Ord(UpCase(Work[1]))-48;
Until (Col >= 1) and (Col <= 3);
If (bd[row,col] <> ' ') then Writeln('** Invalid, try again **');
Until (bd[row,col] = ' ');
bd[row,col] := 'X';
end;
{-------------------------------}
procedure print_board( VAR bd : boardtype);
Var i,index : Integer;
begin
Writeln;
Writeln(' 1 2 3');
for index := 1 to 3 do begin
Writeln( Chr(64+Index)+' ',bd[index,1],' ³ ',bd[index,2],' ³ ',bd[index,3]);
If Index < 3 then begin
Writeln(' ÄÄÄÅÄÄÄÅÄÄÄ');
end;
end;
end;
{-------------------------------------------}
{-------------------------------------------}
Var board : BoardType;
i,j,move_count : integer;
begin
move_count := 0;
for i := 1 to 3 do begin
for j := 1 to 3 do begin
board[i,j] := ' ';
end;
end;
print_board(board);
while (1=1) do begin
{ accept and display human move }
Inc(move_count);
human_move(board);
print_board(board);
if (win_check(board) = 'X') then begin
writeln('X has won');
HALT;
end;
if (move_count = 9) then begin
writeln('Stalemate');
HALT;
end;
{ compute and display computers move }
Inc(move_count);
computer_move(board);
print_board(board);
if (win_check(board) = 'O') then begin
writeln('O has won');
HALT;
end;
end;
end.
Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!
This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.
But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/