You are on page 1of 3

for i,te in enumerate(tetrominoes):

print(te,i)
r=4
if i==0: r=0
elif 1<=i<=3: r=2
rm=tetrominoes[te]
for i in range(0,r):
rm=list([list(x) for x in zip(*rm)])
for l in rm:
for j in range(0,len(l)):
if l[j]=='0':l[j]='1'
print(''.join(l))
print()
print('original')
rm=tetrominoes[te]
for l in rm:
print(''.join(l))
print()

# index 0 is no rotable, 1,2,3 rotates two times, 4,5,6 rotates 4 times


tetrominoes={
'O':[
['0','0'],
['0','0']],
'I':[
['0','0','0','0']],
'S':[
['.','0','0'],
['0','0','.']],
'Z':[
['0','0','.'],
['.','0','0']],
'L':[
['.','.','0'],
['0','0','0']],
'J':[
['0','.','.'],
['0','0','0']],
'T':[
['.','0','.'],
['0','0','0']],
}
rotationsetetro={
'O':0,'I':2,'S':2,'Z':2,'L':4,'J':4,'T':4
}

def solver(board,pieces):
# try the pieces and it's rotation
# search a place to put on board
# take out the piece from the list, temp var
# substract nr of squares occupied, if 0 remained display solution
# else put the piece go into reccursion
# when back from recurrsion remove the piece from the board, put back on the
list

n,m=len(board),len(board[0])
if n*m%4!=0:
print('board can\'t be tiled with tetrominoes')
return

def print_board():
for l in board:
print(''.join(l))
print()

def piece_fits_at(i,j,pt):
if i+len(pt)>n or j+len(pt[0])>m:return False
for pi in range(0,len(pt)):
for pj in range(0,len(pt[0])):
if pt[pi][pj]=='0' and board[i+pi][j+pj]!='.':return False
return True
def this_state():
if board[0][0]==board[1][0]==board[2][0]==board[3][0]=='0' and\
board[0][1]==board[1][1]==board[2][1]==board[3][1]=='1': return
True
return False
# why the L doesn't appear in the list
def worker(board,pieces,Nc=n*m,Kc=0):
for eip,p in enumerate(pieces):
pt=tetrominoes[p]
for ri in range(0,rotationsetetro[p]):
for i in range(0,n):
for j in range(0,m):
if piece_fits_at(i,j,pt): # and check if placed
it doesn't create regions of 1,2 or 3 cells
strKc=str(Kc) if Kc<10 else chr(97+Kc-10)
for pi in range(0,len(pt)):
for pj in range(0,len(pt[0])):
if pt[pi]
[pj]=='0':board[i+pi][j+pj]=strKc
Kc+=1;Nc-=4
del pieces[eip]
if this_state():
print(''.join(pieces))
print_board()
input()
if Nc==0:
print('solution:')
print_board()
else:
worker(board,pieces,Nc,Kc)
for pi in range(0,len(pt)):
for pj in range(0,len(pt[0])):
if pt[pi]
[pj]=='0':board[i+pi][j+pj]='.'
pieces.insert(eip,p)
Nc+=4;Kc-=1
pt=list(zip(*pt))[::-1]

worker(board,pieces)

with open('tetrominoes.txt','r') as f:
line=f.readline()[:-1].split(' ')
if line[0]=='rectangle':
n,m=int(line[1]),int(line[2])
board=[['.']*m for _ in range(0,n)]
pieces=list(f.readline()[:-1])

solver(board,pieces)

You might also like