-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpieces.py
79 lines (71 loc) · 2.78 KB
/
pieces.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
#
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
from board import SquareType
import color
from enum import Enum
PieceType = Enum("PieceType", "L_RED L_GREEN L_BLUE L_BLACK I_RED I_ORANGE")
def BuildOrientations(piece):
def RotateValue(v):
# Rotate block with orientation
if v in (SquareType.UP, SquareType.RIGHT, SquareType.DOWN, SquareType.LEFT):
if v is SquareType.LEFT:
return SquareType.UP
return SquareType(v.value + 1)
# AIR/GLASS don't change value
return v
orientations = []
for i in range(4):
# Rotate the piece 90deg clock wise
new = np.zeros_like(piece)
new[0, 0] = RotateValue(piece[1, 0])
new[0, 1] = RotateValue(piece[0, 0])
new[1, 0] = RotateValue(piece[1, 1])
new[1, 1] = RotateValue(piece[0, 1])
orientations.append(new)
piece = new
return orientations
# Piece is a 2x2 block. For example the red L shaped piece looks like:
# GLASS AIR
# RIGHT GLASS
#
# We store all 4 possible orientations for each piece in the following
# dictionary.
Orientations = {
PieceType.L_RED: BuildOrientations(np.array(
[SquareType.GLASS, SquareType.AIR, SquareType.RIGHT, SquareType.GLASS
]).reshape((2, 2))),
PieceType.L_BLACK: BuildOrientations(np.array(
[SquareType.GLASS, SquareType.AIR, SquareType.LEFT, SquareType.GLASS
]).reshape((2, 2))),
PieceType.L_BLUE: BuildOrientations(np.array(
[SquareType.DOWN, SquareType.AIR, SquareType.GLASS, SquareType.GLASS
]).reshape((2, 2))),
PieceType.I_ORANGE: BuildOrientations(np.array(
[SquareType.DOWN, SquareType.AIR, SquareType.GLASS, SquareType.AIR
]).reshape((2, 2))),
PieceType.I_RED: BuildOrientations(np.array(
[SquareType.LEFT, SquareType.AIR, SquareType.GLASS, SquareType.AIR
]).reshape((2, 2))),
PieceType.L_GREEN: BuildOrientations(np.array(
[SquareType.GLASS, SquareType.AIR, SquareType.GLASS, SquareType.UP
]).reshape((2, 2))),
}
Colors = {PieceType.L_RED: color.Color("darkred"),
PieceType.L_GREEN: color.Color("green"),
PieceType.L_BLUE: color.Color("blue"),
PieceType.L_BLACK: color.Color("black"),
PieceType.I_RED: color.Color("red"),
PieceType.I_ORANGE: color.Color("yellow")}