-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.pas
167 lines (144 loc) · 3.66 KB
/
main.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
program TickTackToe;
uses
sysUtils;
type
Cell = (X, O, _);
TGridArray = array [1..3, 'a'..'c'] of Cell;
TPlayer = record
PlayerName: string;
IsHisMove: boolean;
end;
var
Grid: TGridArray;
Player1, Player2 : TPlayer;
Winner : Cell;
MoveCount : integer = 0;
procedure InitGrid(var Grid: TGridArray);
var
i: integer;
j: char;
begin
for i:= 1 to 3 do
for j:= 'a' to 'c' do
// Grid[i,j] := sysUtils.intToStr(i) + j;
Grid[i,j] := Cell._;
end;
// Print current Grid in a more readable way to the player
procedure PrintGrid(Grid: TGridArray);
var
i: integer;
j: char;
begin
write(' ');
for j:='a' to 'c' do
begin
write(j: 2)
end;
writeln();
for i:= 1 to 3 do
begin
write(i, ' ');
for j := 'a' to 'c' do
begin
write(Grid[i,j]:2);
end;
writeln();
end;
end;
function GetInput(var Player1, Player2: TPlayer): TGridArray;
var
Row: integer;
Col: char;
Move: string;
begin
Player1.PlayerName := 'Player1';
Player2.PlayerName := 'Player2';
write('Enter your move [(col)(row)]: ');
readln(Move);
Col := copy(Move, 1, 1)[1];
Row := sysUtils.strToInt(copy(Move, 2, 1));
// writeln('Row:', Row, ' ', 'Col:', Col);
if Grid[Row, Col] = Cell._ then
begin
if Player1.IsHisMove = True then
begin
Player1.IsHisMove := False;
Player2.IsHisMove := True;
Grid[Row, Col] := Cell.X;
end
else
begin
Player2.IsHisMove := False;
Player1.IsHisMove := True;
Grid[Row, Col] := Cell.O;
end;
end
else
begin
writeln('Illegal Move')
end;
GetInput := Grid;
end;
function FindWinner(var Grid: TGridArray; var MoveCount: integer): Cell;
var
Row : integer;
Col : char;
Winner: Cell;
begin
MoveCount := MoveCount + 1;
Winner := Cell._;
for Row := 1 to 3 do
begin
if Grid[Row,'a'] = Grid[Row,'b'] then
if Grid[Row,'b'] = Grid[Row,'c'] then
if Grid[Row,'c'] <> Cell._ then
Winner := Grid[Row,'a'];
end;
for Col := 'a' to 'c' do
begin
if Grid[1,Col] = Grid[2,Col] then
if Grid[2,Col] = Grid[3,Col] then
if Grid[3,Col] <> Cell._ then
Winner := Grid[1, Col];
end;
if Grid[1,'a'] = Grid[2,'b'] then
if Grid[2,'b'] = Grid[3,'c'] then
if Grid[3,'c'] <> Cell._ then
Winner := Grid[1,'a'];
if Grid[1,'c'] = Grid[2,'b'] then
if Grid[2,'b'] = Grid[3,'a'] then
if Grid[3,'a'] <> Cell._ then
Winner := Grid[1,'a'];
if MoveCount = 9 then
begin
writeln('DRAW!');
Exit;
end;
FindWinner := Winner;
end;
// Begining of Main
begin
// set the Player1 as the first to move
Player1.IsHisMove := True;
// Initiating the grid
InitGrid(Grid);
PrintGrid(Grid);
While True do
begin
Grid := GetInput(Player1, Player2);
PrintGrid(Grid);
Winner := FindWinner(Grid, MoveCount);
if Winner <> Cell._ then
begin
if Winner = Cell.X then
begin
writeln('Winner is Player1[x]');
end
else if Winner = Cell.O then
begin
writeln('Winner is Player2[O]');
end;
break;
end;
end;
end.