-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris.py
77 lines (68 loc) · 2.66 KB
/
tetris.py
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
#!/usr/bin/python
# -*- coding: utf8 -*-
#Author Παναγιώτης Πράττης/Panagiotis Prattis
'''
This is a program which keeps in memory a table of 10 columns and 20 rows and that plays "Tetris"
only by adding L randomly in the table. L is an element with a height of 3 blocks and width of 2 blocks.
The program ends when the L surpasses in height 20 blocks, then it prints how many L blocks managed to enter.
'''
import random
nRows = 20;
nColumns = 10;
# Create table, the empty space will be the zeroes and the L will be the aces
table = [[0 for i in range(nColumns)] for j in range(nRows)];
lWidth = 2;
lHeight = 3;
lCount = 0;
gameover = False;
while not gameover:
# Position of the L's vertix
posL = random.randint(0, nColumns-lWidth);
for i in range(0, nRows):
goNext = False;
for j in range(0, lWidth):
# if no L and not the end of table, continue checking
if(table[i][posL+j] == 0 and i < nRows-1):
continue;
# if end of table and no L yet, continue checking this row's cells
# to see if we can put the L here
elif( table[i][posL+j] == 0 and j < lWidth-1 and i == nRows-1 and i-lHeight+1 >= 0):
continue;
# If it reached the end of table and there is no L, put it there
elif( table[i][posL+j] == 0 and j == lWidth-1 and i == nRows-1 and i-lHeight+1 >= 0):
# draw L on this row
for k in range(0, lWidth):
table[i][posL+k] = 1;
for v in range(1, lHeight):
table[i-v][posL] = 1;
lCount+=1;
goNext = True;
break;
# If found an L in this row, put the L above it
elif( table[i][posL+j] == 1 and i-lHeight >= 0 ):
# draw L on row above
for k in range(0, lWidth):
table[i-1][posL+k] = 1;
for v in range(1, lHeight):
table[i-1-v][posL] = 1;
lCount+=1;
goNext = True;
break;
# BOOM!
else:
gameover = True;
goNext = True;
break;
# If an L was drawn or it is game over, restart the cycle that iterates
# over the rows
if(goNext):
break;
#Print steps
for i in range(0, nRows):
print (table[i])
#All steps till the game over
print('\n'.join([''.join(['{:4}'.format(item) for item in row])
for row in table]))
print('------------------------------------')
#Print how many L shaped blocks managed to enter
print (lCount, " L's were spawned.")