def evaluate(board: chess.Board): ''' returns a value for the board based on a simple evaluation function ''' ret = 0 player = not not ((board.board().contents.flags) & 0b10000 ) # True if black, False if white for rk in range(0, 8): for offs in range(0, 8): pc = (board.board().contents.ranks[rk] >> (offs << 2)) & 0xf # if (pc / 6) == player: if (pc >= BPAWN and pc <= BKING) == player: ret += pcval(pc) else: ret -= pcval(pc) return ret
def board_tensor(board: chess.Board, one_hot=False, unsqueeze=False, half=False): ''' Returns an (8,8) shape torch.tensor form of the board's ranks array ''' t = ((torch.tensor(board.board().contents.ranks, dtype=torch.int64).reshape(8, 1) & masks) >> shift) if (one_hot): t = F.one_hot(t, num_classes=13) # 13 = 6 white + 6 black + 1 empty if (unsqueeze): t = t.unsqueeze(0) if (half): t = t.half() else: t = t.float() return t