-
Notifications
You must be signed in to change notification settings - Fork 0
/
piece.py
65 lines (52 loc) · 1.8 KB
/
piece.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
import yaml
import os
from settings import APP_ROOT
from point import Point
class PieceHelper:
@staticmethod
def alter_moves_list(func, side):
return {
move_type: set(func(relative_moves))
for move_type, relative_moves
in side.iteritems()
}
@staticmethod
def enpointen(moves_list):
return [Point(**move) for move in moves_list]
#return map(Point, moves_list)
@classmethod
def load_pieces(cls):
pieces = yaml.load(open(os.path.join(APP_ROOT, 'yamlpieces.yaml'), 'r'))
for piece, sides in pieces.iteritems():
for side_name, side in sides.iteritems():
pieces[piece][side_name] = cls.alter_moves_list(
cls.enpointen,
side,
)
return pieces
class Piece:
pieces = PieceHelper.load_pieces()
def __init__(self, name, color, current_side=None, x=None, y=None):
self.name = name
self.color = color
self.current_side = current_side
self.x = x
self.y = y
self.sides = self.pieces[self.name.capitalize()]
def __repr__(self):
return "{color} {name} ({current_side})".format(
color=self.color,
name=self.name,
current_side=self.current_side
)
def is_on_board(self, move):
return move.x in range(6) and move.y in range(6)
def add_position(self, move):
return Point(self.x, self.y) + move
def destinations(self, relative_moves):
return filter(self.is_on_board, map(self.add_position, relative_moves))
def available_moves(self):
return PieceHelper.alter_moves_list(
self.destinations,
self.sides[self.current_side],
)