Tic-tac-toe: Difference between revisions
→{{header|Pascal}}: Changed the input loop lest the program accept the move 0 or a move to assigned position. In addition, formatted the code according to JEDI Code Format.
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
(→{{header|Pascal}}: Changed the input loop lest the program accept the move 0 or a move to assigned position. In addition, formatted the code according to JEDI Code Format.) |
||
Line 9,582:
I would expect this version should compile with most Pascal variants including Delphi, but YMMR.
<syntaxhighlight lang="pascal">program
type
var
procedure
var
begin
for
for J := 0 to 2
begin
end;
WriteLn;
if I < 2 then
WriteLn('---------');
end;
end;
begin
if Player = Computer then
SwapPlayer := Human
else
SwapPlayer := Computer;
end;
function
var
begin
for
begin
{ first horizontal solution }
if (CheckWinner = Unassigned) and (B[I, 0] <> Unassigned) and
(B[I, 1] = B[I, 0]) and (B[I, 2] = B[I, 0]) then
else
{ now vertical solution }
if (CheckWinner = Unassigned) and (B[0, I] <> Unassigned) and
CheckWinner := B[0, I];
end;
{ now check the paths of the two cross line slants that share the middle position }
if (CheckWinner = Unassigned) and (B[1, 1] <> Unassigned) then
begin
if (B[1, 1] = B[0, 0]) and (B[2, 2] = B[0, 0]) then
CheckWinner := B[0, 0]
else if (B[1, 1] = B[2, 0]) and (B[0, 2] = B[1, 1]) then
CheckWinner := B[1, 1];
end;
end;
{ Basic strategy test - is this te best solution we have seen }
function
begin
if CurScore = CurBest then
SaveBest := False
else if (
else if (CurScore = Computer) and ((CurBest = Unassigned) or
(CurBest = Human)) then
SaveBest := False
else
SaveBest := True;
end;
{ Basic strategy - recursive depth first
if computer can win save it, otherwise block if need be, else do deeper.
At each level modify the board for the next call, but clean up as go back up,
by remembering the modified position on the call stack. }
var
begin
if
begin
if Score = Val then
else
end
else
begin
for I := 0
for J
begin
if B[I, J] =
Changed := Val;
B[I, J] := Val;
{ the value for now and try wioth the other player }
Score := TestMove(SwapPlayer(Val), Depth + 1);
if Score <> Unassigned then
Score := SwapPlayer(Score);
B[I, J] := Unassigned;
if SaveBest(Score, Best) then
begin
if Depth = 0 then
begin { top level, so remember actual position }
BestI := I;
BestJ := J;
end;
Best := Score;
end;
end;
end;
if Changed <> Unassigned then
TestMove := Best
else
TestMove := Unassigned;
end;
end;
function
var
begin
for
for J := 0 to 2 do
B[I, J] := Unassigned;
WriteLn('You have O, I have X.');
WriteLn;
if Whom = Human then
begin
repeat
Write('Your move: ');
ReadLn(Move);
if (Move < 1) or (Move > 9) then
WriteLn('Opps: enter a number between 1 - 9.');
Dec(Move);
{humans do 1 -9, but the computer wants 0-8 for modulus to work}
I := Move div 3; { convert from range to corridinated of the array }
J := Move mod 3;
if B[I, J] <> Unassigned then
WriteLn('Opps: move ', Move + 1, ' was already done.')
until (Move >= 0) and (Move <= 8) and (B[I, J] = Unassigned);
B[I, J] := Human;
end;
if Whom = Computer then
begin
{ randomize if computer opens, so its not always the same game }
if K = 1 then
begin
BestJ :=
Win := TestMove(Computer, 0);
B[BestI, BestJ] := Computer;
WriteLn('My move: ', BestI
end;
Win := CheckWinner;
if Win <> Unassigned
if Win = Human then
PlayGame
else
else
begin
Whom :=
end;
until (Win <> Unassigned) or
if Win = Unassigned then
PlayGame := 'A draw.';
end;
begin
Randomize;
end.</syntaxhighlight>
|