def _read_thor_file(self, file_name): file_header_size = 16 record_header_size = 8 shots = 60 record_size = 68 games = [] with open(file_name, "rb") as f: c = f.read() board_size = _byte_to_int(c[12]) if board_size == 8 or board_size == 0: for i in xrange(file_header_size, len(c), record_size): moves = [] b = Board() player = Board.BLACK black_score = _byte_to_int(c[i + 6]) for j in xrange(record_header_size, record_size): play = _byte_to_int(c[i + j]) if play > 0: column = (play % 10) - 1 row = (play // 10) - 1 if not b.is_feasible(row, column, player): player = Board.opponent(player) moves.append((player, row, column)) b.flip(row, column, player) player = Board.opponent(player) score = b.score(Board.BLACK) if b.score(Board.BLACK) > b.score(Board.WHITE): score += b.score(Board.BLANK) if score == black_score: games.append((moves, black_score * 2 - 64)) else: self.inconsistencies += 1 return games
def validate(db): import random for moves, result in db.games: if random.random() < 0.9: continue b = Board() for p, r, c in moves: assert b.is_feasible(r, c, p) b.flip(r, c, p) black_score = b.score(Board.BLACK) white_score = b.score(Board.WHITE) score = black_score if b.score(Board.BLACK) > b.score(Board.WHITE): score = b.score(Board.BLANK) + black_score assert result in (black_score - white_score, 2 * score - 64)