Esempio n. 1
0
 def _get_fen_board(self):
     pieces = [None] * 64
     for sq, pc in self.squares.iteritems():
         if pc:
             val = piece.str(pc)
             if self._get_square_color(sq) == 1:
                 val = val.upper()
             pieces[square.index(sq)] = val
     ranks = []
     for rank in xrange(8):
         count = 0
         out = ''
         for file in xrange(8):
             pc = pieces[square.index(square.from_a8(rank, file))]
             if pc is None:
                 count += 1
                 if file == 7:
                     out += '%d' % count
             else:
                 if count:
                     out += '%d' % count
                     count = 0
                 out += pc
         ranks.append(out)
     return '/'.join(ranks)
Esempio n. 2
0
 def _parse_fen_board(self, board_string):
     try:
         ranks = board_string.split('/')
         board = [[None] * 8 for x in range(8)]
         for rank, data in enumerate(ranks):
             file = 0
             for char in data:
                 if char.isdigit():
                     num = int(char)
                     if num < 1 or num > 8:
                         raise ValueError('bad integer in board data')
                     file += num
                 else:
                     try:
                         pc = piece.pc(char)
                         cl = self._parse_piece_color(char)
                         self.set_square(square.from_a8(rank, file), pc, cl)
                         file += 1
                     except ValueError:
                         raise ValueError('bad piece in board data')
             if file != 8:
                 raise ValueError('bad number of files')
         if rank != 7:
             raise ValueError('wrong number of ranks')
         return board
     except ValueError:
         raise
     except Exception:
         raise
         raise ValueError('error parsing fen board data')
Esempio n. 3
0
 def _get_fen_board(self):
     pieces = [None] * 64
     for sq, pc in self.squares.iteritems():
         if pc:
             val = piece.str(pc)
             if self._get_square_color(sq) == 1:
                 val = val.upper()
             pieces[square.index(sq)] = val
     ranks = []
     for rank in xrange(8):
         count = 0
         out = ''
         for file in xrange(8):
             pc = pieces[square.index(square.from_a8(rank, file))]
             if pc is None:
                 count += 1
                 if file == 7:
                     out += '%d' % count
             else:
                 if count:
                     out += '%d' % count
                     count = 0
                 out += pc
         ranks.append(out)
     return '/'.join(ranks)
Esempio n. 4
0
 def _parse_fen_board(self, board_string):
     try:
         ranks = board_string.split('/')
         board = [[None] * 8 for x in range(8)]
         for rank, data in enumerate(ranks):
             file = 0
             for char in data:
                 if char.isdigit():
                     num = int(char)
                     if num < 1 or num > 8:
                         raise ValueError('bad integer in board data')
                     file += num
                 else:
                     try:
                         pc = piece.pc(char)
                         cl = self._parse_piece_color(char)
                         self.set_square(square.from_a8(rank, file), pc, cl)
                         file += 1
                     except ValueError:
                         raise ValueError('bad piece in board data')
             if file != 8:
                 raise ValueError('bad number of files')
         if rank != 7:
             raise ValueError('wrong number of ranks')
         return board
     except ValueError:
         raise
     except Exception:
         raise
         raise ValueError('error parsing fen board data')
Esempio n. 5
0
def str(bb):
    out = []
    for rank in xrange(8):
        for file in xrange(8):
            sq = square.from_a8(rank, file)
            out.append(BITS[int(sq & bb != 0)])
        out.append("\n")
    return ''.join(out)
Esempio n. 6
0
 def __str__(self):
     out = []
     for rank in xrange(8):
         for file in xrange(8):
             sq = square.from_a8(rank, file)
             piece_str = piece.str(self.squares[sq])
             if self.color_bbs[1] & sq:
                 piece_str = piece_str.upper()
             out.append(piece_str)
         out.append('\n')
     return ''.join(out)
Esempio n. 7
0
 def __str__(self):
     out = []
     for rank in xrange(8):
         for file in xrange(8):
             sq = square.from_a8(rank, file)
             piece_str = piece.str(self.squares[sq])
             if self.color_bbs[1] & sq:
                 piece_str = piece_str.upper()
             out.append(piece_str)
         out.append('\n')
     return ''.join(out)
Esempio n. 8
0
def parse(val):
    out = 0
    val = val.replace('\n', '')
    if len(val) != 64:
        raise ValueError("invalid size for bitboard: %s" % len(val))
    for rank in xrange(8):
        for file in xrange(8):
            ch = val[(7 - rank) * 8 + file]
            if ch not in BITS:
                raise ValueError("invalid char in bitboard: %s" % sq)
            out += BITS_TO_INT[ch] * square.from_a8(rank, file)
    return out