示例#1
0
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))
示例#2
0
文件: helper.py 项目: pytran3/Othello
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
示例#3
0
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
示例#4
0
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
示例#5
0
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)
示例#6
0
 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
示例#7
0
文件: search.py 项目: pytran3/Othello
 def _extract_valid_hand(self, board: Board):
     ret = extract_valid_hand(board)
     if ret:
         return ret
     else:
         return [Hand.pass_hand()]
示例#8
0
 def test_invalid_shape(self):
     with self.assertRaises(IllegalIndexException):
         Hand((0, -1), Board.init_board())