Tetris Piece Rotation Algorithm

前端 未结 15 861
傲寒
傲寒 2020-11-30 17:57

What are the best algorithms (and explanations) for representing and rotating the pieces of a tetris game? I always find the piece rotation and representation schemes confu

15条回答
  •  一生所求
    2020-11-30 18:12

    Python:

    pieces = [
        [(0,0),(0,1),(0,2),(0,3)],
        [(0,0),(0,1),(1,0),(1,1)],
        [(1,0),(0,1),(1,1),(1,2)],
        [(0,0),(0,1),(1,0),(2,0)],
        [(0,0),(0,1),(1,1),(2,1)],
        [(0,1),(1,0),(1,1),(2,0)]
    ]
    
    def get_piece_dimensions(piece):
        max_r = max_c = 0
        for point in piece:
            max_r = max(max_r, point[0])
            max_c = max(max_c, point[1])
        return max_r, max_c
    
    def rotate_piece(piece):
        max_r, max_c = get_piece_dimensions(piece)
        new_piece = []
        for r in range(max_r+1):
            for c in range(max_c+1):
                if (r,c) in piece:
                    new_piece.append((c, max_r-r))
        return new_piece
    

提交回复
热议问题