def main(sente=True): board = Board.init_board() ai = MiniMaxAI(5) print(view_board(board)) hands = [] while True: if not extract_valid_hand(board): board.side ^= True hands.append(Hand.pass_hand()) if not extract_valid_hand(board): break if board.side ^ sente: hand = input_hand(board) else: hand = ai.put(board, hands) hands.append(hand) print("AI put: {}".format(hand)) if (board.board == 0).sum() < 12: # 計算時間に余裕があるのでdeepに読む ai.depth = 8 board = put_and_reverse(hand, board) print(view_board(board)) print("=" * 10 + " GAME OVER " + "=" * 10) x_count = (board.board == 1).sum() o_count = (board.board == -1).sum() print("x: {}, o: {}".format(x_count, o_count))
def extract_valid_hand(board: Board) -> List[Hand]: ret = [] for i in range(8): for j in range(8): if is_valid_hand((i, j), board): ret.append(Hand((i, j), board)) return ret
def parse(s: str) -> Tuple[int, int]: x, y = s[0], s[1] x = ord(x) - ord("a") y = int(y) - 1 # to 0-indexed # check array index out of bound Hand((y, x), Board.init_board()) return y, x
def play(network, ai_param=None): if ai_param is None: ai_param = dict(play_count=100) board = Board.init_board() ai1 = AlphaZero(network, history=True, **ai_param) ai2 = AlphaZero(network, history=True, **ai_param) hands = [] while True: if not extract_valid_hand(board): board.side ^= True hands.append(Hand.pass_hand()) if not extract_valid_hand(board): break if board.side: hand = ai1.put(board, hands) else: hand = ai2.put(board, hands) hands.append(hand) board = put_and_reverse(hand, board) result = judge_simple(board) history1 = ai1.history if isinstance(ai1, AlphaZero) else [] history2 = ai2.history if isinstance(ai1, AlphaZero) else [] for x in history1: x[-1] = result for x in history2: x[0] = x[0] * -1 x[-1] = -result return history1 + history2
def evaluate(ai1, ai2, sente=True, is_view=False): board = Board.init_board() if is_view: print(view_board(board)) hands = [] while True: if not extract_valid_hand(board): board.side ^= True hands.append(Hand.pass_hand()) # print("pass hand!!") if not extract_valid_hand(board): break if board.side is sente: hand = ai1.put(board, hands) else: hand = ai2.put(board, hands) hands.append(hand) board = put_and_reverse(hand, board) if is_view: print(view_board(board)) # print(ai2.history) if is_view: print("=" * 10 + " GAME OVER " + "=" * 10) x_count = (board.board == 1).sum() o_count = (board.board == -1).sum() print("x: {}, o: {}".format(x_count, o_count)) return judge_simple(board) * (1 if sente else -1)
def _evaluate_board(self, board: Board): p, v = self._predict(board) p, hands = self._calc_valid_hand_p(p, board) # print("select node:", p) # print("select node:", v) if len(p) == 0: p = np.ones(1) hands = [Hand.pass_hand()] return p, hands, v
def _extract_valid_hand(self, board: Board): ret = extract_valid_hand(board) if ret: return ret else: return [Hand.pass_hand()]
def test_invalid_shape(self): with self.assertRaises(IllegalIndexException): Hand((0, -1), Board.init_board())