-
Notifications
You must be signed in to change notification settings - Fork 4
/
CENTIPED.PAS
544 lines (519 loc) · 12.6 KB
/
CENTIPED.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
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
{ @author: Sylvain Maltais ([email protected])
@created: 2024
@website(https://www.gladir.com/7iles)
@abstract(Target: Turbo Pascal 7, Free Pascal 3.2)
}
Program CENTIPED; { CENTIPEDE.PAS }
{$R-}
Uses Crt,DOS;
Const
CharCannon=#127;
CharCannonBase=#219;
CharBall=#250;
CharCent=#15;
ScreenRow=23;
ScreenCol=40;
InitMoveWhen=12;
InitNumMushs=25;
Type
PtrSegmentRec=^SegmentRec;
SegmentRec=Record
Prev,Next:PtrSegmentRec;
Row,Col:Integer;
End;
PtrHeadTailRec=^HeadTailRec;
HeadTailRec=Record
Prev,Next:PtrHeadTailRec;
Head:PtrSegmentRec;
Tail:PtrSegmentRec;
RelCol:Integer;
End;
ScreenObjects=(Blank,WeakMushroom,StrongMushroom,WeakMushSeg,
StrongMushSeg,Cannon,Wall,Earth,Cent,Shot);
Var
Screen:Array[1..ScreenRow] of Array[1..ScreenCol] of ScreenObjects;
YouX,NumMoves:Integer;
ShotGoing:Boolean;
Score,Lives,NumMush,NumCent,MoveWhen:Integer;
SpiderY,SpiderX,SpiderDX,SpiderDY,ShotY,ShotX:Integer;
Finish:Boolean;
CentList:PtrHeadTailRec;
Move:Integer;
{$IFNDEF FPC}
Procedure CursorOff;
Var
Regs:Registers;
Begin
Regs.AH:=1;
Regs.CH:=32;
Regs.CL:=0;
Intr($10,Regs);
End;
Procedure CursorOn;
Var
Regs:Registers;
Begin
Regs.AX:=$0100;
Regs.CX:=(7 shl 8)+9;
Intr($10,Regs);
End;
{$ENDIF}
Function PadRight(S:String;Space:Byte):String;
Var
I:Byte;
Begin
If Length(S)<Space Then For I:=Length(S)+1 to Space do S:=S+' ';
PadRight:=S;
End;
Function Dupl(C:Char;Num:Integer):String;
Var
I:Byte;
S:String;
Begin
S:='';
For I:=1 to Num do S:=S+C;
Dupl:=S;
End;
Function Random(lb,ub:Integer):Integer;Begin
Random:=lb+Trunc(System.Random*(ub-lb+1));
End;
Procedure DeleteSegment(Var ShotY,ShotX:Integer);
Var
CentSegment:PtrSegmentRec;
NewCent,CentSearch:PtrHeadTailRec;
Found:Boolean;
Begin
GotoXY(ShotX,ShotY);
Write('O');
Screen[ShotY,ShotX]:=StrongMushSeg;
Found:=False;
CentSearch:=CentList;
While(Not Found)and(CentSearch<>NIL)do Begin
With CentSearch^ do If(ShotY=Head^.Row)and(ShotX=Head^.Col)Then Begin
If Head^.Next=NIL Then Begin
If CentList=CentSearch Then Begin
Dispose(Head);
CentList:=CentList^.next;
Dispose(CentSearch);
Found:=True;
End
Else
Begin
Dispose(Head);
CentSearch^.Prev^.Next:=CentSearch^.Next;
If(CentSearch^.Next <> NIL)Then CentSearch^.next^.Prev:=CentSearch^.Prev;
Dispose(CentSearch);
Found:=True;
End;
End
Else
Begin
CentSegment:=Head;
Head:=Head^.Next;
Head^.Prev:=NIL;
Dispose(CentSegment);
End;
End
Else
If(ShotX=Tail^.Col)and(ShotY=Tail^.Row)Then Begin
CentSegment:=Tail;
Tail:=Tail^.Prev;
Tail^.Next:=NIL;
Dispose(CentSegment);
End
Else
Begin
CentSegment:=Head^.Next;
While(CentSegment<>NIL)and(Not Found) do Begin
If(CentSegment^.Row=ShotY )and(CentSegment^.Col=ShotX)Then Begin
MoveWhen:=MoveWhen-1;
If MoveWhen<=1 Then MoveWhen:=2;
Found:=True;
New(NewCent);
NewCent^.Next:=CentSearch^.Next;
NewCent^.Prev:=CentSearch;
CentSearch^.Next:=NewCent;
If NewCent^.Next<>NIL Then NewCent^.Next^.Prev:=NewCent;
NewCent^.Head:=CentSegment^.Next;
NewCent^.Head^.Prev:=NIL;
NewCent^.Tail:=CentSearch^.Tail;
If Screen[NewCent^.Head^.Row,NewCent^.Head^.Col+1]=StrongMushroom Then NewCent^.RelCol:=1
Else NewCent^.RelCol:=-1;
CentSearch^.Tail:=CentSegment^.Prev;
CentSearch^.Tail^.Next:=NIL;
Dispose(CentSegment);
End
Else
CentSegment:=CentSegment^.Next;
End;
End;
CentSearch:=CentSearch^.Next;
End;
End;
Procedure MoveCentipede;
Const
BaseHeadX=39;
BaseHeadY=2;
InitLenCent=12;
Var
PrevSegment,New_Head,Segment:PtrSegmentRec;
CentToMove:PtrHeadTailRec;
Cnt,LenCent:Integer;
Procedure AddHead(Row,Col:Integer);
Var
Segment:PtrSegmentRec;
Begin
New(Segment);
With CentToMove^ Do Begin
Segment^.Row:=Row;
Segment^.Col:=Col;
Segment^.Next:=Head;
Head^.Prev:=Segment;
Segment^.Prev:=NIL;
Head:=Head^.Prev;
Screen[Tail^.Row,Tail^.Col]:=Blank;
GotoXY(Tail^.Col,Tail^.Row);
Write('~');
Segment:=Tail;
Tail:=Tail^.Prev;
Tail^.Next:=NIL;
Dispose(Segment);
GotoXY(Head^.Col,Head^.Row);
Write(CharCent);
Screen[Head^.Row,Head^.Col]:=Cent;
End;
End;
Procedure MoveThisCent(Var CentToMove:PtrHeadTailRec);
Var
Dir:Integer;
Begin
With CentToMove^ do Begin
Case Screen[Head^.Row,Head^.Col+RelCol] of
Blank:AddHead(Head^.Row, Head^.Col+RelCol);
Wall,Cent,WeakMushSeg,StrongMushSeg,WeakMushroom,StrongMushroom:Begin
Case Screen[Head^.Row+1,Head^.Col] Of
WeakMushSeg,StrongMushSeg,WeakMushroom,StrongMushroom:Begin
If Screen[Head^.Row+1,Head^.Col+RelCol]=Blank Then AddHead(Head^.Row+1,Head^.Col+RelCol)
Else
Begin
RelCol:=RelCol*(-1);
If Screen[Head^.Row+1,Head^.Col+RelCol]=Blank Then AddHead(Head^.Row+1,Head^.Col+RelCol)
Else
Begin
Case Random(1,3)of
1:Dir:=1;
2:Dir:=-1;
3:Dir:=0;
End;
If Screen[Head^.Row+1,Head^.Col+Dir]in[StrongMushroom,
WeakMushroom,WeakMushSeg,StrongMushSeg] Then Begin
If Screen[Head^.Row+1,Head^.Col+Dir]in[StrongMushroom,WeakMushroom]Then
NumMush:=NumMush-1;
AddHead( Head^.Row+1,Head^.Col+Dir);
End;
End;
End;
End;
Blank:Begin
RelCol:=RelCol*(-1);
AddHead(Head^.Row+1,Head^.Col);
End;
Cannon,Earth:Finish:=True;
End;
End;
Cannon,Earth:Finish:=True;
End;
End;
End;
Begin
If CentList=NIL Then Begin
New(CentList);
With CentList^ do Begin
NumCent:=NumCent+1;
MoveWhen:=InitMoveWhen-NumCent;
If MoveWhen<=1 Then MoveWhen:=2;
LenCent:=InitLenCent+NumCent;
If LenCent>=BaseHeadX Then LenCent:=BaseHeadX-2;
Next:=NIL;
Head:=NIL;
Prev:=NIL;
Tail:=NIL;
RelCol:=-1;
PrevSegment:=NIL;
For Cnt:=1 to LenCent do Begin
New(Segment);
If Head=NIL Then Begin
Head:=Segment;
End;
With Segment^ do Begin
Row:=BaseHeadY;
Col:=BaseHeadX-LenCent+Cnt-1;
Screen[Row,Col]:=Cent;
GotoXY(Col,Row);
Write(CharCent);
Segment^.Next:=NIL;
Segment^.Prev:=PrevSegment;
If PrevSegment<>NIL Then PrevSegment^.Next:=Segment;
End;
PrevSegment:=Segment;
End;
Tail:=Segment;
End
End
Else
Begin
CentToMove:=CentList;
While CentToMove<>NIL do Begin
Cnt:=0;
While(Cnt<(NumMoves mod MoveWhen))and(CentToMove<>NIL)do Begin
CentToMove:=CentToMove^.Next;
Cnt:=Cnt+1;
End;
If(CentToMove<>NIL)Then Begin
MoveThisCent(CentToMove);
While(Cnt<MoveWhen)and(CentToMove<>NIL) do Begin
CentToMove:=CentToMove^.Next;
Cnt:=Cnt+1;
End;
End;
End;
End;
End;
Procedure SetUp;
Var
Len,Cnt,Row,Col:Integer;
Begin
ClrScr;
Score:=50;
Lives:=3;
NumMoves:=1;
NumCent:=0;
WriteLn(#218+Dupl(#196,38)+#191);
For Col:=1 to ScreenCol do Begin
Screen[1,Col]:=Wall;
Screen[ScreenRow,Col]:=Earth;
End;
For Row:=2 to ScreenRow-1 do begin
Screen[Row,1]:=Wall;
Screen[Row,ScreenCol]:=Wall;
For Col:=2 to ScreenCol-1 do Screen[Row,Col]:=Blank;
WriteLn(#179,Dupl('~',38),#179);
End;
WriteLn(#192+Dupl(#196,38)+#217);
GotoXY(2,24);
Write(Dupl(#127,Lives));
GotoXY(21,24);
Write('Pointage : ',Score);
For Cnt:=1 to InitNumMushs do Begin
Row:=Random(2,ScreenRow-2);
Col:=Random(2,ScreenCol-1);
Screen[Row,Col]:=StrongMushroom;
GotoXY(Col,Row);
Write('O');
End;
YouX:=20;
GotoXY(YouX,ScreenRow-1);
Write(CharCannon);
GotoXY(YouX,ScreenRow);
Write(CharCannonBase);
Screen[ScreenRow-1,YouX]:=Cannon;
SpiderY:=2;
SpiderX:=10;
SpiderDY:=1;
SpiderDX:=1;
GotoXY(10,2);
Write('*');
NumMush:=InitNumMushs;
End;
Procedure AddScore(Num:Integer);Begin
Score:=Score+Num;
GotoXY(21,24);
Write('Pointage : ',Score);
End;
Procedure MoveShot;
Const
Speed_Shot=2;
Var
Cnt:Integer;
Begin
For Cnt:=1 to Speed_Shot do If ShotGoing Then Begin
If Screen[ShotY,ShotX]=Shot Then Begin
Screen[ShotY,ShotX]:=Blank;
GotoXY(ShotX,ShotY);
Write('~');
End;
ShotY:=ShotY-1;
Case Screen[ShotY,ShotX] of
Blank:Begin
Screen[ShotY,ShotX]:=Shot;
GotoxY(ShotX,ShotY);
Write(CharBall);
End;
WeakMushSeg:Begin
Screen[ShotY,ShotX]:=Blank;
GotoXY(ShotX,ShotY);
Write('~');
ShotGoing:=False;
AddScore(10);
End;
WeakMushroom:Begin
NumMush:=NumMush-1;
Screen[ShotY,ShotX]:=Blank;
GotoXY(ShotX,ShotY);
Write('~');
ShotGoing:=False;
AddScore(10);
End;
StrongMushroom:Begin
Screen[ShotY,ShotX]:=WeakMushroom;
ShotGoing:=False;
End;
StrongMushSeg:Begin
Screen[ShotY,ShotX]:=WeakMushSeg;
ShotGoing:=False;
End;
Cent:Begin
AddScore(100);
DeleteSegment(ShotY,ShotX);
ShotGoing:=False;
End;
Else ShotGoing:=False;
End;
End;
End;
Procedure LooseLife;Begin
Lives:=Lives-1;
If Lives>0 Then Begin
GotoXY(2,24);
Write(PadRight(Dupl(CharCannon,Lives),10));
SpiderY:=2;
SpiderX:=10;
SpiderDY:=1;
SpiderDX:=1;
GotoXY(10,2);
Write('*');
Delay(1000);
End
Else
Finish:=True;
End;
BEGIN
{$IFDEF FPC}
{$IFDEF WINDOWS}
SetUseACP(False);
{$ENDIF}
{$ENDIF}
ClrScr;
WriteLn(' ooooo o oo ooo ');
WriteLn('oo oo o oo oo ');
WriteLn('o o oo oo ');
WriteLn('o ooooo oo oooo oooooo ooo oo oooo ooooo ooo oo ooooo ');
WriteLn('o oo oo oo oo oo oo oo oo oo oooo ooo oo oo');
WriteLn('o ooooooo oo oo oo oo oo oo ooooooooo oo ooooooo');
WriteLn('o o oo oo oo oo oo oo oo oo oo oo oo ');
WriteLn('oo o oo o oo oo oo o oo ooo oo oo ooo oo oo o');
WriteLn(' ooooo ooooo oooo oo ooo oooo oo ooo ooooo oooo oo ooooo ');
WriteLn(' oo ');
WriteLn(' oooo ');
WriteLn;
WriteLn('Empchez le mille-pattes de descendre sur terre. Tirez-lui ');
WriteLn('dessus, faites attention
ne pas le diviser. Mfiez-vous de ');
WriteLn('l''araigne : elle ne peut pas tre tue mais peut dtruire ');
WriteLn('votre canon au contact.');
WriteLn;
Write('Presse une touche pour jouer...');
ReadKey;
WriteLn;
CursorOff;
SetUp;
While Not Finish do Begin
NumMoves:=NumMoves+1;
MoveCentipede;
If Odd(NumMoves)Then Begin
Case Screen[SpiderY,SpiderX] of
Cent:Begin
GotoXY(SpiderX,SpiderY);
Write(CharCent);
End;
StrongMushSeg,WeakMushSeg,StrongMushroom,WeakMushroom:Begin
GotoXY(SpiderX,SpiderY);
Write('O');
End;
Blank:Begin
If(SpiderY<ScreenRow-1)and(SpiderY>(ScreenRow div 3))and
(NumMush<InitNumMushs)Then Begin
If(Random(1,ScreenRow-SpiderY)=1)Then Begin
Screen[SpiderY,SpiderX]:=StrongMushroom;
GotoXY(SpiderX,SpiderY);
Write('O');
NumMush:=NumMush+1;
End
Else
Begin
GotoXY(SpiderX,SpiderY);
Write('~');
End;
End
Else
Begin
GotoXY(SpiderX,SpiderY);
Write('~');
End;
End;
Cannon:LooseLife;
End;
Case Screen[SpiderY+SpiderDY,SpiderX+SpiderDX] of
Cent,StrongMushSeg,WeakMushSeg,StrongMushroom,WeakMushroom,Blank:Begin
SpiderY:=SpiderY+SpiderDY;
SpiderX:=SpiderX+SpiderDX;
GotoXY(SpiderX,SpiderY);
Write('*');
End;
Wall,Earth:Begin
If(SpiderY+SpiderDY=ScreenRow)or(SpiderY+SpiderDY=1)Then SpiderDY:=SpiderDY*(-1);
If(SpiderX+SpiderDX=ScreenCol)or(SpiderX+SpiderDX=1)Then SpiderDX:=SpiderDX*(-1);
End;
Cannon:LooseLife;
End;
End;
Move:=0;
If(Keypressed)Then Case ReadKey of
#0:Case ReadKey of
#75:Move:=-1;
#77:Move:=1;
End;
'1','<',',':Move:=-1;
'3','>','.':Move:=1;
'4':Move:=-2;
'6':Move:=2;
'7':Move:=-3;
'9':Move:=3;
'e','E','q','Q':Finish:=True;
'2','5','8',' ':If Not(ShotGoing)Then Begin
ShotGoing:=True;
ShotY:=ScreenRow-1;
ShotX:=YouX;
Score:=Score-2;
End;
End;
If Move<>0 Then If(YouX+Move>1)and(YouX+Move<ScreenCol)Then Begin
GotoXY(YouX,ScreenRow-1);
Write('~');
GotoXY(YouX,ScreenRow);
Write(#196);
Screen[ScreenRow-1,YouX]:=Blank;
Screen[ScreenRow,YouX]:=Earth;
YouX:=YouX+Move;
Screen[ScreenRow,YouX]:=Cannon;
Screen[ScreenRow-1,YouX]:=Cannon;
GotoXY(YouX,ScreenRow-1);
Write(CharCannon);
GotoXY(YouX,ScreenRow);
Write(CharCannonBase);
End;
If(ShotGoing)Then MoveShot;
Delay(50);
End;
Delay(1000);
CursorOn;
END.