def calc(self): data = {} if self._color == UNKNOWN: figures = [f for f in self._model.figures] else: figures = [ f for f in self._model.figures if f.color == self._color ] cells = reduce(lambda a, b: a + b, [f.getVisibleCells() for f in figures]) for cell in cells: data[pos2coors(*cell)] = FigureSerializer( self._model.cell2Figure(*cell)).calc() for fig in figures: data[pos2coors(fig.x, fig.y)] = FigureSerializer(fig).calc() if self._color == UNKNOWN: for x in range(1, 9): for y in range(1, 9): coor = pos2coors(x, y) if coor not in data: data[coor] = {} data['cuts'] = [] for fig in self._model.cuts: data['cuts'].append({ 'kind': FIGURES[fig[0]], 'color': COLORS[fig[1]], }) return data
def calc(self): if not self._model: return {} return { 'kind': FIGURES[self._model.kind], 'color': COLORS[self._model.color], 'position': pos2coors(self._model.x, self._model.y), 'moves': [pos2coors(*m) for m in self._model.getMoves()], }
def test_pos2coors(self): with self.assertRaises(TypeError): pos2coors('1', 1) with self.assertRaises(TypeError): pos2coors(1.0, 1) with self.assertRaises(ValueError): pos2coors(0, 1) self.assertEqual(pos2coors(1, 1), 'a1') self.assertEqual(pos2coors(8, 8), 'h8')
def move(self, color, pos1, pos2): if color != self.current_player: raise WrongTurnError figure = self.board.cell2Figure(*pos1) if not figure: raise NotFoundError if figure.color != color: raise WrongFigureError try: castled = isinstance(figure, King) and figure.try_to_castle(*pos2) except EndGame as exc: exc.figure = figure raise exc if not castled: result = figure, '{}-{}'.format(pos2coors(*pos1), pos2coors(*pos2)) try: figure.move(*pos2) except EndGame as exc: exc.figure, exc.move = result raise exc else: result = figure, castled self.current_player = invert_color(self.current_player) return result
def calc(self): return { 'moves': [(m['figure'].symbol, pos2coors(m['x1'], m['y1']), pos2coors(m['x2'], m['y2'])) for m in self._model.moves], }
def __str__(self): return '{}{}'.format(self.symbol, pos2coors(self.x, self.y))