saralk
Veteran Member
Posts: 2598 Reputation: 38
35 / /
Joined: Feb 2003
|
O.P. Tic-Tac-Toe
I made a game of tic-tac-toe in computing today!
Its pretty cool, you can play against a computer, although the computer is very easy to beat, there is a severe lack of interface and there are a few debug things left over.
this is the code to it:
code: program tictactoe;
{Saral Kaushik}
{$apptype console}
var
Board : array [1..3, 1..3] of char;
Turn : integer;
PlayGame : boolean;
procedure SetBoard;
var
SetLoop : integer;
SetLoop2 : integer;
begin
for SetLoop := 1 to 3 do
for SetLoop2 := 1 to 3 do
Board[SetLoop, SetLoop2] := '.';
end;
procedure DisplayBoard;
var
DisplayLoop : integer;
DisplayLoop2 : integer;
begin
writeln('1 2 3');
for DisplayLoop := 1 to 3 do
begin
for DisplayLoop2 := 1 to 3 do
write(Board[DisplayLoop, DisplayLoop2],' ');
writeln(DisplayLoop,' ');
end;
end;
function PlayerTurn(Character : char):boolean;
var
XPosition : integer;
YPosition : integer;
Move : boolean;
begin
Move := FALSE;
while Move = FALSE do
begin
write('Enter X Position: ');
readln(XPosition);
write('Enter Y Position: ');
readln(YPosition);
if Board [YPosition, XPosition] = '.' then
begin
Board [YPosition, XPosition] := Character;
Move := TRUE;
end
else
writeln('Position Taken');
end;
end;
function BoardFull():boolean;
var
FullLoop : integer;
FullLoop2 : integer;
Total : integer;
begin
Total := 0;
for FullLoop := 1 to 3 do
for FullLoop2 := 1 to 3 do
if (Board[FullLoop, FullLoop2] <> '.') then Total := Total + 1;
if (Total = 9) then BoardFull := TRUE
else BoardFull := FALSE;
end;
function IsWin(CheckFor:char):boolean;
const
WinString = '111000000000111000000000111100010001001010100100100100010010010001001001';
var
CheckLoopX : integer;
CheckLoopY : integer;
MatchCount : integer;
StringNo : integer;
begin
MatchCount := 0;
StringNo := 1;
repeat
for CheckLoopX := 1 to 3 do
for CheckLoopY := 1 to 3 do
begin
if ((Board [CheckLoopX, CheckLoopY] = CheckFor) and (WinString[StringNo] = '1')) then
MatchCount := MatchCount + 1;
//writeln (CheckLoopX, ' ',CheckLoopY,' ',Board[CheckLoopX, CheckLoopY],' ',WinString[StringNo],' ',MatchCount);
StringNo := StringNo + 1;
end;
//writeln;
if (MatchCount < 3) then MatchCount := 0;
until (MatchCount >= 3) or (StringNo >= 72);
if MatchCount >= 3 then begin
IsWin := TRUE; //writeln('Win')
end
else IsWin := FALSE;
end;
function MakeMove(Player:char):boolean;
const
MoveString = '112000000000112000000000112211000000000211000000000211121000000000121000000000121100100200010010020001001002200100100020010010002001001100200100010020010001002001100010002200010001100020001002010100001010200001020100';
var
CheckLoopX : integer;
CheckLoopY : integer;
MatchCount : integer;
StringNo : integer;
MoveX : integer;
MoveY : integer;
OtherPlayer : char;
begin
//check to see if a win is possible
MatchCount := 0;
StringNo := 1;
MoveX := 0;
MoveY := 0;
repeat
for CheckLoopX := 1 to 3 do
begin
for CheckLoopY := 1 to 3 do
begin
if (MoveString [StringNo] = '2') and (Board [CheckLoopX, CheckLoopY] = '.') then
begin
MoveX := CheckLoopX;
MoveY := CheckLoopY;
end;
if (MoveString[StringNo] = '1') and (Board [CheckLoopX, CheckLoopY] = Player) then
begin
MatchCount := MatchCount + 1;
//writeln('matched');
//readln;
end;
//writeln(StringNo,' ',Board[CheckLoopX, CheckLoopY],' ',MoveString[StringNo],' ',MatchCount);
StringNo := StringNo + 1;
end;
end;
if (MatchCount < 2) then MatchCount := 0;
//writeln;
until (((MatchCount >= 2) and (MoveX <> 0)) or (StringNo >= 216));
if ((MatchCount >= 2) and (MoveX <> 0)) then
begin
Board [MoveX, MoveY] := Player;
writeln('Win Possible Move Made');
end
else
begin
//check to see if a block is possible
MatchCount := 0;
StringNo := 1;
repeat
for CheckLoopX := 1 to 3 do
for CheckLoopY := 1 to 3 do
begin
if (MoveString [StringNo] = '2') and (Board [CheckLoopX, CheckLoopY] = '.') then
begin
MoveX := CheckLoopX;
MoveY := CheckLoopY;
end;
if (Player = 'X') then OtherPlayer := 'O'
else OtherPlayer := 'X';
if (MoveString[StringNo] = '1') and (Board [CheckLoopX, CheckLoopY] = OtherPlayer) then MatchCount := MatchCount + 1;
//writeln(StringNo,' ',Board[CheckLoopY, CheckLoopX],' ',MoveString[StringNo],' ',MatchCount);
StringNo := StringNo + 1;
end;
if (MatchCount < 2) then MatchCount := 0;
until (((MatchCount >= 2) and (MoveX <> 0)) or (StringNo >= 216));
if (MatchCount >= 2) and (MoveX <> 0) then
begin
Board [MoveX, MoveY] := Player;
writeln('Block Possible Move Made', MatchCount);
end
else
begin
repeat
randomize;
MoveX := Random(4);
MoveY := Random(4);
until (Board [MoveX, MoveY] = '.');
Board [MoveX, MoveY] := Player;
writeln('Random Move Made');
end;
end;
end;
begin
SetBoard;
DisplayBoard;
begin
Turn := 1;
PlayGame := TRUE;
repeat
if Turn = 1 then
begin
writeln('Player 1');
PlayerTurn('X');
DisplayBoard;
Turn := 0;
end
else
begin
{writeln('Player 2');
PlayerTurn('O');
DisplayBoard;
if IsWin('O') then
begin
writeln('Player 2 Wins');
PlayGame := FALSE;
end;}
MakeMove('O');
DisplayBoard;
Turn := 1;
end;
if (IsWin('X') = TRUE) then writeln('Human Wins, Well Done Human')
else if (IsWin('O') = TRUE) then writeln('Computer Wins, Better Luck Next Time Human');
until ((BoardFull = TRUE) or (PlayGame = FALSE));
end;
readln;
end.
Attachment: tictactoe.exe (18 KB)
This file has been downloaded 212 time(s).
This post was edited on 11-29-2005 at 06:35 PM by saralk.
The Artist Formerly Known As saralk
London · New York · Paris
Est. 1989
|
|