def set_sfen(self, sfen):
<EOL>parts = sfen.split()<EOL>if len(parts) != <NUM_LIT:4>:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(repr(sfen)))<EOL><DEDENT>rows = parts[<NUM_LIT:0>].split('<STR_LIT:/>')<EOL>if len(rows) != <NUM_LIT:9>:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(repr(sfen)))<EOL><DEDENT>for row in rows:<EOL><INDENT>field_sum = <NUM_LIT:0><EOL>previous_was_digit = False<EOL>previous_was_plus = False<EOL>for c in row:<EOL><INDENT>if c in ['<STR_LIT:1>', '<STR_LIT:2>', '<STR_LIT:3>', '<STR_LIT:4>', '<STR_LIT:5>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>if previous_was_digit:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(repr(sfen)))<EOL><DEDENT>if previous_was_plus:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(repr(sfen)))<EOL><DEDENT>field_sum += int(c)<EOL>previous_was_digit = True<EOL>previous_was_plus = False<EOL><DEDENT>elif c == '<STR_LIT:+>':<EOL><INDENT>if previous_was_plus:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(repr(sfen)))<EOL><DEDENT>previous_was_digit = False<EOL>previous_was_plus = True<EOL><DEDENT>elif c.lower() in ['<STR_LIT:p>', '<STR_LIT:l>', '<STR_LIT:n>', '<STR_LIT:s>', '<STR_LIT:g>', '<STR_LIT:b>', '<STR_LIT:r>', '<STR_LIT:k>']:<EOL><INDENT>field_sum += <NUM_LIT:1><EOL>if previous_was_plus and (c.lower() == '<STR_LIT:g>' or c.lower() == '<STR_LIT:k>'):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>previous_was_digit = False<EOL>previous_was_plus = False<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(repr(sfen)))<EOL><DEDENT><DEDENT>if field_sum != <NUM_LIT:9>:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(repr(sfen)))<EOL><DEDENT><DEDENT>if not parts[<NUM_LIT:1>] in ['<STR_LIT:b>', '<STR_LIT:w>']:<EOL><INDENT>raise ValueError("<STR_LIT>".format(repr(sfen)))<EOL><DEDENT>if int(parts[<NUM_LIT:3>]) < <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(repr(sfen)))<EOL><DEDENT>self.clear()<EOL>square_index = <NUM_LIT:0><EOL>previous_was_plus = False<EOL>for c in parts[<NUM_LIT:0>]:<EOL><INDENT>if c in ['<STR_LIT:1>', '<STR_LIT:2>', '<STR_LIT:3>', '<STR_LIT:4>', '<STR_LIT:5>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>square_index += int(c)<EOL><DEDENT>elif c == '<STR_LIT:+>':<EOL><INDENT>previous_was_plus = True<EOL><DEDENT>elif c == '<STR_LIT:/>':<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>piece_symbol = c<EOL>if previous_was_plus:<EOL><INDENT>piece_symbol = '<STR_LIT:+>' + piece_symbol<EOL><DEDENT>self.set_piece_at(square_index, Piece.from_symbol(piece_symbol))<EOL>square_index += <NUM_LIT:1><EOL>previous_was_plus = False<EOL><DEDENT><DEDENT>if parts[<NUM_LIT:1>] == '<STR_LIT:w>':<EOL><INDENT>self.turn = WHITE<EOL><DEDENT>else:<EOL><INDENT>self.turn = BLACK<EOL><DEDENT>self.pieces_in_hand = [collections.Counter(), collections.Counter()]<EOL>if parts[<NUM_LIT:2>] != '<STR_LIT:->':<EOL><INDENT>piece_count = <NUM_LIT:0><EOL>for c in parts[<NUM_LIT:2>]:<EOL><INDENT>if c in ['<STR_LIT:0>', '<STR_LIT:1>', '<STR_LIT:2>', '<STR_LIT:3>', '<STR_LIT:4>', '<STR_LIT:5>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>piece_count *= <NUM_LIT:10><EOL>piece_count += int(c)<EOL><DEDENT>else:<EOL><INDENT>piece = Piece.from_symbol(c)<EOL>if piece_count == <NUM_LIT:0>:<EOL><INDENT>piece_count = <NUM_LIT:1><EOL><DEDENT>self.add_piece_into_hand(piece.piece_type, piece.color, piece_count)<EOL>piece_count = <NUM_LIT:0><EOL><DEDENT><DEDENT><DEDENT>self.move_number = int(parts[<NUM_LIT:3>]) or <NUM_LIT:1><EOL>self.transpositions = collections.Counter((self.zobrist_hash(), ))<EOL>
Parses a SFEN and sets the position from it.
Rasies `ValueError` if the SFEN string is invalid.