-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4DTIC.txt
574 lines (487 loc) · 12.5 KB
/
4DTIC.txt
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
Program FourDimensionalTicTacToePrg;
{Copyright 1994, Jacob Walker}
{This software is distributed as TradeWare}
{Please read the readme.txt file for licensing info}
Uses
Crt;
Type
DimArray = Array[1..42,1..4] of 1..3;
PlayerRecord = Record
Dim: DimArray;
LastPoint:0..42;
Control:-1..3;
End;
PlayerArray = Array['O'..'X'] of PlayerRecord;
WinPtSet = Set of 1..42;
Var
Player:PlayerArray;
Turn:Char;
TempString:string;
TempInt:Integer;
TempChar:Char;
Count:integer;
ConvError:integer;
WinPts:WinPtSet;
WhoWon:Char;
Done:Boolean;
Function Change(var turn:char):char;
{Changes the Turn}
Begin
Case Turn of
'X':Change := 'O';
'O':Change := 'X';
end;
End;
Procedure Init;
{Initializes the game}
Begin
{Sets Initial Variables}
ClrScr;
Randomize;
Player['X'].LastPoint := 0;
Player['O'].LastPoint := 0;
{Writes Title}
Textcolor(Blink+LightBlue);
GotoXY(32,4);
Write('4-d Tic Tac Toe');
Textcolor(Yellow);
GotoXY(28,6);
Write('Written By Jacob Walker');
{Get's X's Information}
Textcolor(White);
Repeat
GotoXy(1,20);
Write('Enter X''s skill level (0 to 3, 0 is human): ');
tempchar := Readkey;
Write(TempChar);
val(TempChar,tempint,ConvError);
Until ((tempint >= 0) and (tempint <=3 ) and (ConvError=0));
Player['X'].Control := tempint;
{Get's O's Information}
Repeat
GotoXy(1,21);
Write('Enter O''s skill level (0 to 3, 0 is human): ');
tempchar := Readkey;
Write(TempChar);
val(tempchar,tempint,ConvError);
Until ((tempint >= 0) and (tempint <= 3) and (ConvError=0));
Player['O'].control := tempint;
{Decides Who should go first}
GotoXy(1,22);
Write('Who should go first: ');
TempChar := Readkey;
Write(TempChar);
Case TempChar of
'X','x': turn := 'X';
'O','o': turn := 'O';
else
Case odd(Random(4269)) of
True:turn := 'X';
False:turn := 'O';
end;
end;
Turn := Change(turn);
End;
Procedure DrawDim(who:char;pos:integer);
{Places the cursor at the coresponding screen cordinate}
Begin
With Player[who] do
GotoXY(12*Dim[pos,1]+3*Dim[pos,3]-12,6*Dim[pos,2]+2*Dim[pos,4]-7);
End;
Procedure WriteBoard;
{Writes the board to the screen}
Procedure WriteBackBoard;
{Writes the board without symbols to the screen}
Var
Count1,Count2,StartX,Starty:integer;
Begin
For Count1 := 1 to 3 do
For Count2 := 1 to 3 do
Begin
Textcolor(LightGreen);
Startx := 12*count1-11;
Starty := 6*count2-5;
Gotoxy(startx,starty);
Write(' ³ ³ ');
Gotoxy(startx,starty+1);
Write('ÄÄÄÅÄÄÄÅÄÄÄ ');
Gotoxy(startx,starty+2);
Write(' ³ ³ ');
Gotoxy(startx,starty+3);
Write('ÄÄÄÅÄÄÄÅÄÄÄ ');
Gotoxy(startx,starty+4);
Write(' ³ ³ ');
End;
End;
Procedure WritePositions;
{Writes Symbols to the screen}
Var
Count,Players:Integer;
Who:'O'..'X';
Begin
For Players := 1 to 2 do
Begin
Case Players of
1:Who := 'X';
2:Who := 'O';
end;
With Player[who] do
For Count := 1 to LastPoint do
Begin
Drawdim(who,count);
Case Who of
'X':Textcolor(LightBlue);
'O':Textcolor(LightRed);
End;
If ((Count in WinPts) and (WhoWon = Who)) then
Case WhoWon of
'X':Textcolor(Blink+LightBlue);
'O':Textcolor(Blink+LightRed);
end;
Write(who);
End;
End;
End;
Begin
Clrscr;
WriteBackBoard;
WritePositions;
End;
Function Win(who:char):Boolean;
{Sees if someone won}
Var
Point:Array[1..3] of integer;
across:1..4;
Count:1..3;
TempWin:Boolean;
Function AllSame:Boolean;
Begin
AllSame := False;
With Player[who] do
If ((Dim[Point[1],across] = Dim[Point[2],across]) and
(Dim[Point[2],across] = Dim[Point[3],across])) then
AllSame := True;
End;
Function Up:Boolean;
Begin
Up := False;
With Player[who] do
If ((Dim[Point[1],across] = 1) and
(Dim[Point[2],across] = 2) and
(Dim[Point[3],across] = 3)) then
Up := True;
End;
Function Down:Boolean;
Begin
Down := False;
With Player[who] do
If ((Dim[Point[1],across] = 3) and
(Dim[Point[2],across] = 2) and
(Dim[Point[3],across] = 1)) then
Down := True;
End;
Function Repeated:Boolean;
Begin
Repeated := False;
If ((Point[1] = Point[2]) or
(Point[1] = Point[3]) or
(Point[2] = Point[3])) then
Repeated := True;
End;
Begin
TempWin := False;
WinPts := [];
With Player[who] do
For Point[1] := 1 to LastPoint do
For Point[2] := 1 to LastPoint do
For Point[3] := 1 to LastPoint do
If Not(Repeated) then
Begin
TempWin := True;
For across := 1 to 4 do
If not((AllSame) or (Up) or (Down)) then
TempWin := False;
If TempWin = True then
For Count := 1 to 3 do
Begin
WinPts := WinPts + [Point[count]];
Point[count] := LastPoint;
End;
End;
Win := TempWin;
End;
Function PutOk(who:char;where:integer):boolean;
var
BoardPlace:1..4;
ListPlace:1..42;
Temp:Boolean;
Check:0..4;
Begin
Temp:= True;
For BoardPlace := 1 to 4 do
If not(Player[who].dim[where,BoardPlace] in [1..3]) then
Temp := False;
For ListPlace := 1 to Player[change(who)].LastPoint do
Begin
Check := 0;
For BoardPlace := 1 to 4 do
If Player[who].dim[where,Boardplace] =
Player[change(who)].dim[ListPlace,BoardPlace] then
Check := Check + 1;
If Check >= 4 then
Temp := False;
End;
For ListPlace := 1 to Player[who].Lastpoint do
If Not(listplace = where) then
Begin
Check := 0;
For BoardPlace := 1 to 4 do
If Player[who].dim[where,boardplace] =
Player[who].dim[ListPlace,Boardplace] then
Check := check + 1;
If Check >= 4 then
Temp := False;
End;
PutOk := Temp;
End;
Procedure ChoosePosition(turn:char);
{Input to the board}
Procedure Human;
{Allows player to input position}
Procedure MoveUp;
Begin
With Player[turn] do
If not((Dim[lastpoint,2]=1) and (Dim[lastpoint,4]=1)) then
If Dim[lastpoint,4]=1 then
Begin
Dim[lastpoint,4]:=3;
Dim[lastpoint,2]:=Dim[lastpoint,2]-1;
End
else
Dim[lastpoint,4]:=Dim[lastpoint,4]-1;
End;
Procedure MoveDown;
Begin
With Player[turn] do
If not((Dim[lastpoint,2]=3) and (Dim[lastpoint,4]=3)) then
If Dim[lastpoint,4]=3 then
Begin
Dim[lastpoint,4]:=1;
Dim[lastpoint,2]:=Dim[lastpoint,2]+1;
End
else
Dim[lastpoint,4]:=Dim[lastpoint,4]+1;
End;
Procedure MoveLeft;
Begin
With Player[turn] do
If not((Dim[lastpoint,1]=1) and (Dim[lastpoint,3]=1)) then
If Dim[lastpoint,3]=1 then
Begin
Dim[lastpoint,3]:=3;
Dim[lastpoint,1]:=Dim[lastpoint,1]-1;
End
else
Dim[lastpoint,3]:=Dim[lastpoint,3]-1;
End;
Procedure MoveRight;
Begin
With Player[turn] do
If not((Dim[lastpoint,1]=3) and (Dim[lastpoint,3]=3)) then
If Dim[lastpoint,3]=3 then
Begin
Dim[lastpoint,3]:=1;
Dim[lastpoint,1]:=Dim[lastpoint,1]+1;
End
else
Dim[lastpoint,3]:=Dim[lastpoint,3]+1;
End;
{Procedure MoveHelp;}
Var
Input:char;
count:1..4;
Action:char;
Continue:Boolean;
Begin
With Player[turn] do
Begin
GotoXY(1,20);
Case Turn of
'X':Textcolor(LightBlue);
'O':Textcolor(LightRed);
End;
Write(turn,'''s turn');
LastPoint := LastPoint + 1;
For count := 1 to 4 do
Dim[lastpoint,count] := 2;
Continue := False;
Repeat
DrawDim(turn,lastpoint);
Action := Readkey;
Case Action of
'Q','q':
Halt;
{'H','h','?':
MoveHelp;}
'7':
Begin
MoveUp;
MoveLeft;
End;
'8','K','k','N','n':
MoveUp;
'9':
Begin
MoveUp;
MoveRight;
End;
'4','H','h','W','w':
MoveLeft;
'5',char(13),char(32):
If PutOk(turn,lastpoint) then
Continue := True;
'6','L','l','E','e':
MoveRight;
'1':
Begin
MoveLeft;
MoveDown;
End;
'2','J','j','S','s':
MoveDown;
'3':
Begin
MoveDown;
MoveRight;
End;
End;
Until Continue;
End;
End;
Procedure Computer(skill:integer);
Var
Other,Me:Char;
Procedure CanIWin; {Sees If Computer Can Win}
Var
Cord:Array[1..4] of 1..3;
Begin
With Player[turn] do
Begin
For Cord[1] := 1 to 3 do
For Cord[2] := 1 to 3 do
For Cord[3] := 1 to 3 do
For Cord[4] := 1 to 3 do
Begin
For Count := 1 to 4 do
Dim[Lastpoint,count] := Cord[count];
If ((PutOk(turn,LastPoint)) and (Win(turn))) then
Begin
Done := True;
For count := 1 to 4 do
Cord[count] := 3;
End;
End;
End;
End;
Procedure CanOpWin; {Sees If Opponent can Win}
Var
Cord:Array[1..4] of 1..3;
Begin
Other := Change(turn);
Me := Turn;
Player[other].LastPoint := Player[other].lastpoint + 1;
Player[me].Lastpoint := Player[me].lastpoint - 1;
For Cord[1] := 1 to 3 do
For Cord[2] := 1 to 3 do
For Cord[3] := 1 to 3 do
For Cord[4] := 1 to 3 do
Begin
With Player[other] do
For Count := 1 to 4 do
Dim[lastpoint,count] := Cord[Count];
If ((PutOk(other,player[other].lastpoint))
and (Win(other))) then
Begin
With Player[me] do
Begin
Done := True;
Lastpoint := Lastpoint + 1;
For Count := 1 to 4 do
Dim[lastpoint,count] := Cord[count];
End;
For Count := 1 to 4 do
Cord[count] := 3;
End;
End;
Player[other].Lastpoint := Player[other].lastpoint - 1;
If Not(Done) then
Player[me].lastpoint := player[me].lastpoint + 1;
End;
Procedure RandomPlacement;
Begin
With Player[turn] do
Begin
Repeat
For Count := 1 to 4 do
Dim[lastpoint,count] := (random(1000) mod 3) +1
Until PutOk(turn,lastpoint);
End;
End;
Begin
Done := False;
With Player[turn] do
LastPoint := LastPoint + 1;
If ((not(done)) and (skill >=2)) then
CanIWin;
If ((not(done)) and (skill >=3)) then
CanOpWin;
{If ((not(done)) and (skill >=4)) then
NextToOther;
If ((not(done)) and (skill >=5)) then
MostPossible;
If ((not(done)) and (skill >=6)) then
NextToandMostPossible;}
if Not(done) then
RandomPlacement;
End;
Begin
Case Player[turn].Control of
0:Human;
1..10:Computer(Player[turn].Control);
end;
End;
Procedure WinGame;
var
TempChar:Char;
Begin
WhoWon := Turn;
WriteBoard;
GotoXY(1,20);
Case WhoWon of
'X':Textcolor(LightBlue);
'O':Textcolor(LightRed);
end;
Write(WhoWon,'''s Won!!!!!');
Repeat
GotoXY(1,21);
Textcolor(White);
Write('Play Again? ');
TempChar := Readkey;
Until TempChar in ['Y','y','n','N'];
Case TempChar of
'Y','y':Done := False;
'N','n':Done := True;
end;
WhoWon := ' ';
End;
Begin
Repeat
Init;
Repeat
Turn := Change(turn);
WriteBoard;
ChoosePosition(turn);
Until Win(turn);
WinGame;
Until Done;
End.