def json_to_examples(line, examples, proba=None): try: game_json = json.loads(line) except json.decoder.JSONDecodeError: print("Problem with", line) return board = Board.from_fen(game_json["initial_fen"]) is_blue = True round = 0 if game_json["winner"] == "BLUE": winner = 1 else: winner = -1 for move in game_json["moves"]: played = Action.from_uci_usi(move["played"]) node = MCTSNode() node.total_N = move["total_N"] for action_uci_usi, value in move["N"].items(): node.N[Action.from_uci_usi(action_uci_usi)] = value update_examples(board, examples, node.get_policy, is_blue, proba, round, winner=winner) board.apply_action(played) round += 1 is_blue = not is_blue
def from_fen(cls, player_blue, player_red, fen): fen = fen.replace("--", "- -") board = Board.from_fen(fen) game = Game(player_blue, player_red, board) fen_split = fen.split(" ") if fen_split[1] == "w": game.current_player = Color.BLUE else: game.current_player = Color.RED game.round = int(fen_split[-1]) * 2 - 1 if game.current_player == Color.BLUE: game.round -= 1 game.starting_fen = fen # fen_split[-2] contains number of half moves return game
def __repr__(self): return repr(Board.from_fen(str(self)))
def _raw_to_examples(line_iterator, proba=None): game_number = 1 blue_starting = None red_starting = None fen_starting = None board = None is_blue = True round = 0 examples = [] for line in line_iterator: line = line.strip() if "{" in line: examples = [] json_to_examples(line, examples, proba) game_number += 1 for example in examples: yield example examples = [] elif line == "": if is_blue: winner = Color.RED else: winner = Color.BLUE set_winner(examples, winner) for example in examples: yield example # End game blue_starting = None red_starting = None fen_starting = None board = None is_blue = True round = 0 examples = [] game_number += 1 elif "/" in line: fen_starting = line elif fen_starting is None and blue_starting is None: blue_starting = line elif fen_starting is None and red_starting is None: red_starting = line else: if board is None: if fen_starting is None: board = Board(start_blue=blue_starting, start_red=red_starting) else: board = Board.from_fen(fen_starting) if line == "XXXX": action = None get_policy = get_none_action_policy else: action = Action(int(line[0]), int(line[1]), int(line[2]), int(line[3])) get_policy = action.get_policy update_examples(board, examples, get_policy, is_blue, proba, round) try: board.apply_action(action) except AttributeError: print("Wrong action", action, ", round:", round) round += 1 is_blue = not is_blue if is_blue: winner = Color.RED else: winner = Color.BLUE set_winner(examples, winner) for example in examples: yield example
def test_strange_move_chariot(self): board = Board.from_fen("2b1akb1B/2r6/4C2c1/5p3/1p1P1Pp2/9/1PnN5/5R3/2B1K4/5AN2 w - - 1 67") actions = board.get(2, 5).get_actions() self.assertNotIn(Action(2, 5, 1, 4), actions)
def test_from_fen(self): board = Board.from_fen("rnba1abnr/4k4/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/4K4/RNBA1ABNR w - - 0 1") self.assertEqual(board.get_score(Color.BLUE), 72)
def test_read_strange(self): board = Board.from_fen("1bnaa1bn1/R8/5k1cr/1p2p1B1p/2p6/9/1PP2P2P/4CCN2/1N2K4/2BA1A2R w - - 0 1") self.assertNotEqual(board.get_actions(Color.RED), [Action(0, 0, 0, 0)])