This file is indexed.

/usr/share/gnu-smalltalk/examples/Tetris.st is in gnu-smalltalk-common 3.2.4-2.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
"======================================================================
|
|   BLOX Tetris... why not?
|
|
 ======================================================================"

 
"======================================================================
|
| Copyright 1999 Free Software Foundation, Inc.
| Written by Paolo Bonzini.
|
| This file is part of GNU Smalltalk.
|
| GNU Smalltalk is free software; you can redistribute it and/or modify it
| under the terms of the GNU General Public License as published by the Free
| Software Foundation; either version 2, or (at your option) any later version.
| 
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
| FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
| details.
| 
| You should have received a copy of the GNU General Public License along with
| GNU Smalltalk; see the file LICENSE.  If not, write to the Free Software
| Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  
|
 ======================================================================"

PackageLoader fileInPackage: 'Blox'!
Namespace current: BLOX!

Object subclass: #TetrisField
	instanceVariableNames: 'canvas rows currentPiece nextPiece'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Graphics-Tetris'!

Object subclass: #TetrisPiece
	instanceVariableNames: 'positions blocks rotation color origin'
	classVariableNames: 'BlockSize Pieces'
	poolDictionaries: ''
	category: 'Graphics-Tetris'!

Object subclass: #Tetris
	instanceVariableNames: 'pause delay level grid movingBlocks
				window statsWindow
				scoreLabel levelLabel linesLabel button canvas'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Graphics-Tetris'!

!TetrisField methodsFor: 'accessing'!

at: point
    ^(rows at: point y) at: point x
!

at: point put: value
    ^(rows at: point y) at: point x put: value
! !

!TetrisField class methodsFor: 'instance creation'!

new: canvas
    ^self basicNew initialize: canvas
!

!TetrisField methodsFor: 'initializing'!

initialize: aBCanvas
    canvas := aBCanvas.
    rows := (1 to: 28) collect: [ :each | ByteArray new: 14 ].
    
    rows do: [ :each |
	self initializeLine: each.
    ].
    (rows at: 25) atAllPut: 1.
!

initializeLine: line

    line
	at:    1	  put: 1;
	at:    2	  put: 1;
	atAll: (3 to: 12) put: 0;
	at:    13	  put: 1;
	at:    14	  put: 1
! !

!TetrisField methodsFor: 'removing filled lines'!

checkLine: y
    ^(rows at: y) allSatisfy: [ :each | each ~~ 0 ]
!

removeLines
    | removed lastLine firstLine |
    removed := 0.
    firstLine := self currentPiece y.
    lastLine := 24 min: firstLine + 3.
    lastLine - firstLine + 1 timesRepeat: [
	(self checkLine: lastLine)
	    ifTrue: [
		removed := removed + 1.
		self removeLine: lastLine.
	    ]
	    ifFalse: [
		lastLine := lastLine - 1
	    ]
    ].
    ^removed
!

removeLine: filledY
    | saved y shift line |
    saved := rows at: filledY.
    filledY to: 2 by: -1 do: [ :each |
	rows at: each put: (rows at: each - 1)
    ].
    self initializeLine: saved.
    rows at: 1 put: saved.

    "Now take care of the canvas"
    y := filledY * TetrisPiece blockSize.
    shift := 0 @ TetrisPiece blockSize.

    line := Rectangle
	origin: -1 @ (y + 2)
	corner: (15 * TetrisPiece blockSize) @ (y + 4).

    canvas items do: [ :each |
	each origin y < line origin y
	    ifTrue: [ 
		each corner y > line corner y
		    ifTrue: [ each remove ]
		    ifFalse: [ each moveBy: shift; redraw ]
	    ]
    ].
! !

!TetrisField methodsFor: 'moving pieces'!

dropPiece
    [ self slidePiece ] whileTrue: [ ]
!

movePieceLeft
    self currentPiece x: self currentPiece x - 1.
    ^self currentPiece
	moveInto: self
	ifFail: [ self currentPiece x: self currentPiece x + 1 ].
!

movePieceRight
    self currentPiece x: self currentPiece x + 1.
    ^self currentPiece
	moveInto: self
	ifFail: [ self currentPiece x: self currentPiece x - 1 ].
!

rotatePiece
    self currentPiece rotate: 1.
    ^self currentPiece
	moveInto: self
	ifFail: [ self currentPiece rotate: 3 ].
!

slidePiece
    self currentPiece y: self currentPiece y + 1.
    ^self currentPiece
	moveInto: self
	ifFail: [ self currentPiece y: self currentPiece y - 1 ].
! !

!TetrisField methodsFor: 'accessing piece variables'!

currentPiece
    ^currentPiece
!

currentPiece: piece
    currentPiece := piece.
    self currentPiece x: 4; y: 1.
!

nextPiece
    ^nextPiece
!

nextPiece: piece
    nextPiece := piece.
! !

!TetrisPiece class methodsFor: 'pieces'!

blockSize
    ^12
!

initialize
    "Initialize the class variables"
    BlockSize := self blockSize.
    Pieces := (Array new: 7)
	at: 1 put: (TetrisPiece new color: 'DarkKhaki'   initialize: self pieceL);
	at: 2 put: (TetrisPiece new color: 'Magenta'     initialize: self pieceInvertedL);
	at: 3 put: (TetrisPiece new color: 'Red'	 initialize: self pieceSkinny);
	at: 4 put: (TetrisPiece new color: 'BlueViolet'  initialize: self pieceBlock);
	at: 5 put: (TetrisPiece new color: 'Cyan'	 initialize: self pieceSlash);
	at: 6 put: (TetrisPiece new color: 'DarkOrange'  initialize: self pieceBackslash);
	at: 7 put: (TetrisPiece new color: 'ForestGreen' initialize: self pieceT);
	yourself
!

pieceL
    ^#( (0 0 0 0)   (0 0 0 0)   (0 0 0 0)   (0 0 0 0)
	(0 0 0 0)   (0 8 8 0)   (0 0 0 0)   (0 0 8 0)
	(8 8 8 0)   (0 8 0 0)   (8 0 0 0)   (0 0 8 0)
	(0 0 8 0)   (0 8 0 0)   (8 8 8 0)   (0 8 8 0))	!

pieceInvertedL
    ^#( (0 0 0 0)   (0 0 0 0)   (0 0 0 0)   (0 0 0 0)
	(0 0 0 0)   (8 0 0 0)   (0 0 0 0)   (8 8 0 0)
	(8 8 8 0)   (8 0 0 0)   (0 0 8 0)   (0 8 0 0)
	(8 0 0 0)   (8 8 0 0)   (8 8 8 0)   (0 8 0 0))	!

pieceSkinny
    ^#( (0 8 0 0)   (0 0 0 0)   (0 8 0 0)   (0 0 0 0)
	(0 8 0 0)   (0 0 0 0)   (0 8 0 0)   (0 0 0 0)
	(0 8 0 0)   (8 8 8 8)   (0 8 0 0)   (8 8 8 8)
	(0 8 0 0)   (0 0 0 0)   (0 8 0 0)   (0 0 0 0))	!

pieceBlock
    ^#( (0 0 0 0)   (0 0 0 0)   (0 0 0 0)   (0 0 0 0)
	(0 8 8 0)   (0 8 8 0)   (0 8 8 0)   (0 8 8 0)
	(0 8 8 0)   (0 8 8 0)   (0 8 8 0)   (0 8 8 0)
	(0 0 0 0)   (0 0 0 0)   (0 0 0 0)   (0 0 0 0))	!

pieceSlash
    ^#( (0 0 0 0)   (0 0 0 0)   (0 0 0 0)   (0 0 0 0)
	(0 0 8 0)   (0 0 0 0)   (0 0 8 0)   (0 0 0 0)
	(0 8 8 0)   (8 8 0 0)   (0 8 8 0)   (8 8 0 0)
	(0 8 0 0)   (0 8 8 0)   (0 8 0 0)   (0 8 8 0))	!

pieceBackslash
    ^#( (0 0 0 0)   (0 0 0 0)   (0 0 0 0)   (0 0 0 0)
	(0 8 0 0)   (0 0 0 0)   (0 8 0 0)   (0 0 0 0)
	(0 8 8 0)   (0 8 8 0)   (0 8 8 0)   (0 8 8 0)
	(0 0 8 0)   (8 8 0 0)   (0 0 8 0)   (8 8 0 0))	!

pieceT
    ^#( (0 0 0 0)   (0 0 0 0)   (0 0 0 0)   (0 0 0 0)
	(0 8 0 0)   (0 0 0 0)   (0 0 8 0)   (8 8 8 0)
	(0 8 8 0)   (0 8 0 0)   (0 8 8 0)   (0 8 0 0)
	(0 8 0 0)   (8 8 8 0)   (0 0 8 0)   (0 0 0 0))	!

random
    | piece |
    piece := Random between: 1 and: 7.
    ^(Pieces at: piece) copy
! !


!TetrisPiece methodsFor: 'initializing'!

color: myColor initialize: table
    color := myColor.
    positions := Array
	with: (self getPiece: table index: 1)
	with: (self getPiece: table index: 2)
	with: (self getPiece: table index: 3)
	with: (self getPiece: table index: 4)
!

getPiece: table index: index
    "Private - Answer an array of four points corresponding to the coordinates
     of the points in table which are such that table[y*4 + x + index] <> 0.
     table[index+12]. This is necessary so that a human being can prepare the
     tables..."

    | firstFree y position |
    firstFree := 1.
    y := 0.
    position := Array new: 4.
    index to: index + 12 by: 4 do: [ :each |
	firstFree := self
	    getPiece: (table at: each)
	    y: y
	    blocksFrom: firstFree
	    storeIn: position.
	y := y + 1
    ].
    ^position
!

getPiece: tableLine y: y blocksFrom: firstFree storeIn: blocks
    "Private - Store in the blocks Array, starting at index firstFree,
     points with the given y and varying x, corresponding to any
     values <> 0 in the tableLine Array.
     Answer the index, >= firstFree, of the last slot filled in blocks."

    | i |
    i := firstFree.
    tableLine doWithIndex: [ :each :x |
	each = 0 ifFalse: [
	    blocks at: i put: x @ y.
	    i := i + 1
	].
    ].
    ^i
! !

!TetrisPiece methodsFor: 'drawing'!

blockSize
    ^BlockSize
!

cementOn: field drawOn: canvas
    blocks do: [ :eachCoord |
	field at: eachCoord / self blockSize put: 1.

	(BRectangle new: canvas)
	    color: self color;
	    grayOut;
	    origin: eachCoord extent: self blockSize asPoint;
	    create
    ]
!

color
    ^color
!

drawWith: movingBlocks
    movingBlocks with: blocks do: [ :eachBRect :eachCoord |
	eachBRect
	    origin: eachCoord extent: self blockSize asPoint;
	    redraw.
    ]
!

!TetrisPiece methodsFor: 'moving'!

canMoveInto: field
    | point |
    point := Point new.
    (positions at: rotation) do: [ :position |
	point
	    x: self origin x + position x;
	    y: self origin y + position y.

	(field at: point) > 0 ifTrue: [ ^false ].
    ].
    ^true
!

move
    blocks with: (positions at: rotation) do: [ :block :position |
	block 
	    x: (self origin x + position x) * self blockSize;
	    y: (self origin y + position y) * self blockSize
    ].
!

moveInto: field ifFail: aBlock
    (self canMoveInto: field)
	ifFalse: [ aBlock value. ^false ].
    
    self move.
    ^true
!

rotate: howMany
    "Three lines are necessary because rotation is in the 1..4 range,
     while \\ likes a 0..3 range"
    rotation := rotation - 1.
    rotation := (rotation + howMany) \\ 4.
    rotation := rotation + 1
! !

!TetrisPiece methodsFor: 'accessing'!

origin
    ^origin
!

x
    ^self origin x
!

x: x
    self origin x: x
!

y
    ^self origin y
!

y: y
    self origin y: y
! !

!TetrisPiece methodsFor: 'basic'!

postCopy
    rotation := 1.
    origin := Point new.

    blocks := Array
	with: Point new
	with: Point new
	with: Point new
	with: Point new.
! !

!Tetris class methodsFor: 'game'!

new
    ^self basicNew
	layout;
	bindKeys: #('Left' 'Right' 'Up' 'Down' 'Return');
	yourself
!

play
    Blox dispatchEvents: self new
! !


!Tetris methodsFor: 'game'!

cycle
    | filledLines |
    grid := TetrisField new: canvas.
    grid nextPiece: TetrisPiece random.
    [
	grid currentPiece: grid nextPiece.
	grid nextPiece: TetrisPiece random.
	movingBlocks do: [ :each | each color: grid currentPiece color ].
	
	"If the piece cannot move, game over!"
	grid slidePiece
    ] whileTrue: [
	[   window exists ifFalse: [ ^self ].
	    grid currentPiece drawWith: movingBlocks.
	    grid slidePiece
	]   whileTrue: [ self delay ].

	grid currentPiece cementOn: grid drawOn: canvas.
	self resetMovingBlocks.
	filledLines := grid removeLines.

	filledLines > 0 ifTrue: [ self lines: self lines + filledLines ].

	(self lines - 1) // 10 > self level
	    ifTrue: [ self level: self level + 1 ].

	self score:
	    2 * self level squared +
	    (#(0 50 150 400 900) at: filledLines + 1) +
	    self score
    ].
    ^self
!

play
    button label: 'Pause'.
    button callback: self message: #pause.
    self
	activate;
	score: 0;
	level: 1;
	lines: 0;
	resetCanvas;
	cycle.
    button callback: self message: #play.
! !

!Tetris methodsFor: 'private'!

delay
    "I like this method a lot!"
    delay wait.

    "Especially this semaphore!!"
    pause wait.
    pause signal.
!

level
    ^levelLabel label asInteger
!

level: nextLevel
    | level |
    level := nextLevel min: 10.
    delay := Delay forMilliseconds: 825 - (75 * level).

    levelLabel label: level printString
!

lines
    ^linesLabel label asInteger
!

lines: newLines
    linesLabel label: newLines printString
!

score
    ^scoreLabel label asInteger
!

score: newScore
    scoreLabel label: newScore printString
! !

!Tetris methodsFor: 'events'!

advanceLevel
    self level: self level + 1
!

destroyed
    statsWindow exists ifTrue: [ statsWindow destroy ].
    window exists ifTrue: [ window destroy ].
    ^true
!

movePieceLeft
    ^grid movePieceLeft
!

movePieceRight
    ^grid movePieceRight
!

pause
    button label: 'Restart'.
    button callback: self message: #restart.

    "I like this semaphore a lot!"
    pause wait.
!

restart
    button label: 'Pause'.
    button callback: self message: #pause.
    self activate.

    "I like this semaphore a lot!"
    pause signal.
!

rotatePiece
    ^grid rotatePiece
!

slidePiece
    ^grid slidePiece
!

dropPiece
    ^grid dropPiece
! !

!Tetris methodsFor: 'user interface'!

bindKeys: keys
    keys
	with: #(#movePieceLeft #movePieceRight #rotatePiece #slidePiece #dropPiece)
	do: [ :key :selector | canvas onKeyEvent: key send: selector to: self ].
!

layout
    pause := Semaphore forMutualExclusion.

    (window := BWindow new: 'GNU Tetris!')
	width: TetrisPiece blockSize * 10
	height: TetrisPiece blockSize * 22.

    (canvas := BCanvas new: window)
	x: TetrisPiece blockSize * -3
	y: TetrisPiece blockSize * -3
	width: TetrisPiece blockSize * 14
	height: TetrisPiece blockSize * 28.

    window map.

    (statsWindow := BTransientWindow new: 'Scoring' in: window)
	x: window x + window width + 10
	y: window y
	width: 100
	height: 150.

    (BLabel new: statsWindow label: 'Score') x: 0 y: 0  width: 100 height: 16.
    (BLabel new: statsWindow label: 'Lines') x: 0 y: 33 width: 100 height: 16.
    (BLabel new: statsWindow label: 'Level') x: 0 y: 66 width: 100 height: 16.

    (scoreLabel := BLabel new: statsWindow label: '0') x: 0 y: 17 width: 100 height: 16.
    (linesLabel := BLabel new: statsWindow label: '0') x: 0 y: 50 width: 100 height: 16.
    (levelLabel := BLabel new: statsWindow label: '0') x: 0 y: 83 width: 100 height: 16.
    
    (button := BButton new: statsWindow label: 'Start')
	x: 0 y: 100 width: 100 height: 23;
	callback: self message: #play.

    (BButton new: statsWindow label: 'Next level')
	x: 0 y: 127 width: 100 height: 23;
	callback: self message: #advanceLevel.

    statsWindow bringToTop.	statsWindow callback: self message: #destroyed.
    window bringToTop.	 	window callback: self message: #destroyed.
!

resetCanvas
    canvas empty.

    movingBlocks := Array
	with: (BRectangle new: canvas)
	with: (BRectangle new: canvas)
	with: (BRectangle new: canvas)
	with: (BRectangle new: canvas).

    self resetMovingBlocks.
    movingBlocks do: [ :each | each create ]
!

resetMovingBlocks
    movingBlocks do: [ :each | 
	each origin: (-50 @ -50) extent: TetrisPiece blockSize asPoint.
    ].
!

window
    ^window
!

activate
    window activate.
    canvas activate.
! !

TetrisPiece initialize!
Namespace current: Smalltalk!