Files @ fba1a2015cf3
Branch filter:

Location: Tetris/pieces.py

Laman
rotace dílků
import numpy as np


board=[[0]*4 for r in range(3)]
steamboat=[[0,0,0],[1,1,1],[0,1,0]]


def rotate(obj,axis,multiple):
	"""[ # z
		[[1,2,3], # y,x
		[4,5,6]],

		[[7,8,9],
		[10,11,12]]
	]"""
	matrices=[
		np.array([[1,0,0],[0,0,-1],[0,1,0]]),
		np.array([[0,0,1],[0,1,0],[-1,0,0]]),
		np.array([[0,-1,0],[1,0,0],[0,0,1]])
	]
	m=1
	for i in range(multiple): m=np.dot(m,matrices[axis])

	shape=np.array(obj.shape)
	shape_=abs(np.dot(shape,m))
	middle=(shape-1)/2
	middle_=(shape_-1)/2
	res=np.zeros(shape_)

	for (z,plane) in enumerate(obj):
		for (y,row) in enumerate(plane):
			for (x,item) in enumerate(row):
				point=np.array([z,y,x])
				point_=np.dot(point-middle,m)+middle_
				res[tuple(map(int,point_))]=item

	return res


def fits(board,piece,offset):
	(dz,dy,dx)=offset
	res=[]

	for (z,plane) in enumerate(piece):
		for (y,row) in enumerate(plane):
			for (x,item) in enumerate(row):
				if item==0: continue
				coords=(z+dz,y+dy,x+dx)
				if board[coords]: return None
				res.append(coords)
	return res


if __name__=="__main__":
	# for r in range(-2,3):
	# 	for c in range(-2,4):
	# 		x=fits(board,steamboat,(r,c))
	# 		if x: print((r,c),x)

	# print(rotZ([[0,1,0,0],[0,1,0,0],[0,1,0,0],[0,0,0,0]]))

	arr=np.array([ # z
		[[1,2,3], # y,x
		[4,5,6]],

		[[7,8,9],
		[10,11,12]]
	])
	print(rotate(arr,2,4))