File size: 2,909 Bytes
9aed787
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Define the pentomino pieces and handle their rotations
# Output should be an array/list of 1x2 blocks to be marked in the pentomino grid environment

import numpy as np

# Piece definitions for 0 rotation
# Pieces are P, T, U, W, X, Z

# Shift from 5x5 to 3x3 grid
P = [
    [0, 1, 1],
    [0, 1, 1],
    [0, 1, 0]]

T = [
    [1, 1, 1],
    [0, 1, 0],
    [0, 1, 0]]

U = [
    [1, 0, 1],
    [1, 1, 1],
    [0, 0, 0]]

W = [
    [1, 0, 0],
    [1, 1, 0],
    [0, 1, 1]]

X = [
    [0, 1, 0],
    [1, 1, 1],
    [0, 1, 0]]

Z = [
    [1, 1, 0],
    [0, 1, 0],
    [0, 1, 1]]

# Create a dictionary item for the pieces
pieces_dict = {
    'P': P,
    'T': T,
    'U': U,
    'W': W,
    'X': X,
    'Z': Z
}

# Define a list of piece names from above dict - to select a piece randomly
PIECE_NAMES = ['P', 'T', 'U', 'W', 'X', 'Z']

# Define colours
# Define a dict of colours in RGB format- str: tuple

COLOURS = {
    'red': (255, 0, 0),
    'blue': (0, 0, 255),
    'green': (0, 255, 0),
    'yellow': (255, 255, 0),
    'cyan': (0, 0, 255),
    'magenta': (255, 0, 255)
}

# Define a list of colour names from above dict - to select a colour randomly
COLOUR_NAMES = ['red', 'blue', 'green', 'yellow', 'cyan', 'magenta']

class PentominoPiece():
    '''
    Intialize a pentomino piece with a symbol, rotation and position
    Args:
        symbol: A single letter string correesponding to the piece shape
        rotation: An integer r in {0, 1, 2, 3} to define the angle of rotation corresponding to  r*pi/2
        position: The block in the Pentomino Board Grid coressponding to the center of the Piece Grid
    '''
    def __init__(self, symbol: str, rotation: int, position: np.array):
        self.symbol = symbol
        self.rotation = rotation
        self.position = [position[0]-1, position[1]-1] # Convert to 0-indexing - top-left corner of the piece
        
    def get_grid_locations(self) -> np.array:
        '''
        Get the locations of blocks to mark as a piece in the Pentomino Board Grid
        Returns:
            grid_marks: An np.array containing multiple vectors of length 2 for a single piece
                        (co-ordinates to be marked in the Pentomino Board Grid)
        '''
        
        default_piece_grid = pieces_dict[self.symbol]
        rotated_piece_grid = np.rot90(default_piece_grid, self.rotation)
     
        # Get the grid marks for the piece accoding to the Gym coordinate system = Inverted Y - axis
        grid_marks = []
        for i in range(rotated_piece_grid.shape[0]):
            for j in range(rotated_piece_grid.shape[1]):
                if rotated_piece_grid[i][j] == 1:
                    grid_marks.append(self.position + np.array([j, i]))
        grid_marks = np.array(grid_marks)

        return grid_marks

if __name__ == '__main__':
    piece1 = PentominoPiece('P', 0, np.array([1, 1]))
    print(piece1.get_grid_locations())