-
Notifications
You must be signed in to change notification settings - Fork 1
/
ansi-connect-four.tiger
276 lines (276 loc) · 5.52 KB
/
ansi-connect-four.tiger
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
let
type intRow = array of int
type intGrid = array of intRow
type Game = {turn : int, rows : int, columns : int, grid : intGrid}
var defaultRows := 6
var defaultColumns := 7
var defaultTurn := -1
function Game (turn : int, rows : int, columns : int) : Game = (
let
var turn := turn
var rows := (
if rows >= defaultRows then (
rows
) else (
defaultRows
)
)
var columns := (
if columns >= defaultColumns then (
columns
) else (
defaultColumns
)
)
var grid := intGrid [rows] of intRow [0] of 0
in
for row := 0 to rows - 1 do (
grid [row] := intRow [columns] of 0
);
Game {turn = turn, rows = rows, columns = columns, grid = grid}
end
)
function changeTurn (game : Game) = (
game.turn := -game.turn
)
function addToken (game : Game, column : int) : int = (
if column >= 0 & column < game.columns then (
let
var game := game
var turn := game.turn
var rows := game.rows
var grid := game.grid
var row := 0
var column := column
in
while row < rows & not (grid [row] [column]) do (
row := row + 1
);
if (
row := row - 1;
row + 1
) then (
grid [row] [column] := turn
);
row
end
) else (
-1
)
)
function getToken (game : Game, row : int, column : int) : int = (
if 0 <= row & row < game.rows & 0 <= column & column < game.columns then (
game.grid [row] [column]
) else (
0
)
)
function hasWinner (game : Game, row : int, column : int) : int = (
let
var game := game
var turn := game.turn
var row := row
var column := column
var counts := intRow [4] of 0
var hasWinner := 0
in
for i := -3 to 3 do (
let
var game := game
var turn := turn
var row := row
var column := column
var counts := counts
in
for j := 0 to 3 do (
let
var game := game
var turn := turn
var row := (
if j = 0 then (
row
) else (
row + i
)
)
var column := (
if j = 2 then (
column
) else if j = 3 then (
column - i
) else (
column + i
)
)
var counts := counts
in
counts [j] := (
if getToken (game, row, column) = turn then (
let
var counts := counts
var count := counts [j]
in
if count = 3 then (
hasWinner := 1;
break
);
count + 1
end
) else (
0
)
)
end
);
if hasWinner then (
break
)
end
);
hasWinner
end
)
function isFull (game : Game) : int = (
let
var game := game
var grid := game.grid
var row := grid [0]
var maxColumn := game.columns - 1
var isFull := 1
in
for column := 0 to maxColumn do (
if not (row [column]) then (
isFull := 0;
break
)
);
isFull
end
)
function printGrid (rows: int, columns : int) = (
print ("\^[[0;34m");
for row := 1 to rows do (
for column := 1 to columns do (
print ("| ")
);
print ("|\n")
);
for column := 1 to columns do (
print ("+--")
);
print ("+\n");
for column := 1 to columns do (
print ("|\^[[0m");
if column < 10 then (
print (" ")
);
printi (column);
print ("\^[[0;34m")
);
print ("|\n");
for column := 1 to columns do (
print ("+--")
);
print ("+\n\^[[0m")
)
function printToken (turn : int) = (
print ("\^[[0;3");
printi (turn + 2);
print ("m");
if turn > 0 then (
print ("[]")
) else (
print ("<>")
);
print ("\^[[0m")
)
var scores := intRow [2] of 0
var nextTurn := defaultTurn
in
while 1 do (
let
var game := Game (
nextTurn,
(
print ("Nombre de lignes (minimum ");
printi (defaultRows);
print (") ? ");
read ()
),
(
print ("Nombre de colonnes (minimum ");
printi (defaultColumns);
print (") ? ");
read ()
)
)
in
print ("\^[[2A\^[[2M");
printGrid (game.rows, game.columns);
while 1 do (
print ("Au tour du joueur ");
printToken (game.turn);
print (" !\n");
let
var row := 0
var column := 0
in
while 1 do (
column := (
print ("Colonne (entre 1 et ");
printi (game.columns);
print (") ? ");
read () - 1
);
row := addToken (game, column);
if row <> -1 then (
print ("\^[[");
printi (5 + game.rows - row);
print ("F\^[[");
printi (column * 3 + 1);
print ("C\^[[");
printToken (game.turn);
print ("\^[[");
printi (3 + game.rows - row);
print ("E");
break
);
print ("\^[[1A\^[[1M")
);
if hasWinner (game, row, column) then (
let
var player := (game.turn + 1) / 2
in
scores [player] := scores [player] + 1
end;
nextTurn := -game.turn;
print ("\^[[2MVictoire du joueur ");
printToken (game.turn);
print (" !\n");
break
)
end;
if isFull (game) then (
nextTurn := -nextTurn;
print ("\^[[2MMatch nul...\n");
break
);
changeTurn (game)
);
print ("Les scores sont de \^[[0;32m");
printi (scores [0]);
print ("\^[[0m pour le joueur ");
printToken (-1);
print (" et \^[[0;32m");
printi (scores [1]);
print ("\^[[0m pour le joueur ");
printToken (1);
print (".\nVoulez-vous arreter de jouer (1 pour oui et 0 pour non) ? ");
if read () then (
print ("\^[[1A\^[[1M");
break
);
print ("\^[[1A\^[[1M")
end
)
end