-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetrominoes.py
135 lines (127 loc) · 5.13 KB
/
tetrominoes.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
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
"""
All the "place" functions place a tetrominoe
in the first 4 rows of the board. First we check if the
pixels are empty. If not, the game ends (we return False).
All available tetrominoes in the original game can be found
here: https://tetris.fandom.com/wiki/Tetromino
"""
import random
class Tetrominoes(object):
def __init__(self,board):
self.available={
'I':self.place_i,
'J':self.place_j,
'L':self.place_l,
'O':self.place_o,
'S':self.place_s,
'T':self.place_t,
'Z':self.place_z,
}
self.board=board
def place_random(self):
"""
Adds a random
"""
selection = random.choice(list(self.available.keys()))
self.available[selection]()
return selection
def check_available(self):
"""
Checks if the 4x2 pixels at the top of the
board are available.
:param list board: Current board of the game.
:return bool: if all of them are empty.
"""
cond1=self.board.board[0][int(self.board.dimensions[0]/2)]==0
cond2=self.board.board[1][int(self.board.dimensions[0]/2)]==0
cond3=self.board.board[0][int(self.board.dimensions[0]/2)+1]==0
cond4=self.board.board[1][int(self.board.dimensions[0]/2)+1]==0
cond5=self.board.board[0][int(self.board.dimensions[0]/2)+2]==0
cond6=self.board.board[1][int(self.board.dimensions[0]/2)+2]==0
cond7=self.board.board[0][int(self.board.dimensions[0]/2)+3]==0
cond8=self.board.board[1][int(self.board.dimensions[0]/2)+3]==0
return cond1 and cond2 and cond3 and cond4 and cond5 and cond6 and cond7 and cond8
def place_i(self):
"""
:param list board: Current board of the game.
:return bool: If the game can continue.
"""
if self.check_available():
self.board.board[0][int(self.board.dimensions[0]/2)]=2
self.board.board[0][int(self.board.dimensions[0]/2)+1]=2
self.board.board[0][int(self.board.dimensions[0]/2)+2]=2
self.board.board[0][int(self.board.dimensions[0]/2)+3]=2
return True
return False
def place_j(self):
"""
:param list board: Current board of the game.
:return bool: If the game can continue.
"""
if self.check_available():
self.board.board[0][int(self.board.dimensions[0]/2)]=2
self.board.board[1][int(self.board.dimensions[0]/2)]=2
self.board.board[1][int(self.board.dimensions[0]/2)+1]=2
self.board.board[1][int(self.board.dimensions[0]/2)+2]=2
return True
return False
def place_l(self):
"""
:param list board: Current board of the game.
:return bool: If the game can continue.
"""
if self.check_available():
self.board.board[0][int(self.board.dimensions[0]/2)+2]=2
self.board.board[1][int(self.board.dimensions[0]/2)]=2
self.board.board[1][int(self.board.dimensions[0]/2)+1]=2
self.board.board[1][int(self.board.dimensions[0]/2)+2]=2
return True
return False
def place_o(self):
"""
:param list board: Current board of the game.
:return bool: If the game can continue.
"""
if self.check_available():
self.board.board[0][int(self.board.dimensions[0]/2)]=2
self.board.board[0][int(self.board.dimensions[0]/2)+1]=2
self.board.board[1][int(self.board.dimensions[0]/2)]=2
self.board.board[1][int(self.board.dimensions[0]/2)+1]=2
return True
return False
def place_s(self):
"""
:param list board: Current board of the game.
:return bool: If the game can continue.
"""
if self.check_available():
self.board.board[1][int(self.board.dimensions[0]/2)]=2
self.board.board[0][int(self.board.dimensions[0]/2)+1]=2
self.board.board[1][int(self.board.dimensions[0]/2)+1]=2
self.board.board[0][int(self.board.dimensions[0]/2)+2]=2
return True
return False
def place_t(self):
"""
:param list board: Current board of the game.
:return bool: If the game can continue.
"""
if self.check_available():
self.board.board[0][int(self.board.dimensions[0]/2)+1]=2
self.board.board[1][int(self.board.dimensions[0]/2)]=2
self.board.board[1][int(self.board.dimensions[0]/2)+1]=2
self.board.board[1][int(self.board.dimensions[0]/2)+2]=2
return True
return False
def place_z(self):
"""
:param list board: Current board of the game.
:return bool: If the game can continue.
"""
if self.check_available():
self.board.board[0][int(self.board.dimensions[0]/2)+1]=2
self.board.board[0][int(self.board.dimensions[0]/2)]=2
self.board.board[1][int(self.board.dimensions[0]/2)+1]=2
self.board.board[1][int(self.board.dimensions[0]/2)+2]=2
return True
return False