def move(self, frompos, topos, promotion=QUEEN): assert(frompos != topos) piece = self.grid.get(frompos) if piece is None: # could have been killed in between raise InvalidMove("no piece at frompos") piece.promotion = promotion victim = self.grid.get(topos) pstart = frompos.y == mgen.PAWN_START[piece.color] if not mgen.allowed_move(piece.piece, topos - frompos, mgen.FORWARDDIR[piece.color], victim is not None, pstart): raise InvalidMove("illegal direction for %s" % FENCODE[piece.piece]) if self.in_between(frompos, topos, piece): raise InvalidMove("position in between occupied") if victim is not None: if victim.color == piece.color: raise InvalidMove("occupied by own color") else: piece.attack(topos) else: if piece.delay == 0: self.relocate(frompos, topos)
def attack_on_pos(self, gridpos, attacker_color): """returns the first possible attack at gridpos""" for kdir in mgen.RELMOVES_KNIGHT: frompos = gridpos + kdir cand = self.grid.get(frompos) if cand is not None and cand.piece == KNIGHT and cand.color == attacker_color: return frompos for dir in mgen.MAINDIRS: frompos = gridpos + dir while mgen.on_board(frompos) and not frompos in self.grid: frompos += dir if mgen.on_board(frompos): attacker = self.grid[frompos] if attacker.color == attacker_color and mgen.allowed_move(attacker.piece, gridpos - frompos, mgen.FORWARDDIR[attacker.color], True, False): return frompos return None