def decide(_status): result_list = [] score_list = [0] * 6 for i in range(6): if _status[i] == 0: score_list[i] = -float("inf") result_list.append({ "status": False, "finish": False, "again": False, "robbery": False }) continue temp_status = _status.copy() temp_result = mancala.move(temp_status, i) if temp_result["again"] == True: return i score_list[i] = temp_result["status"][6] - _status[6] result_list.append(temp_result) if score_list.count(max(score_list)) == 1: return score_list.index(max(score_list)) else: max_diff = max(score_list) for i in range(6): if score_list[i] == max_diff: score_list[i] = sum([ y - x for (x, y) in zip(result_list[i]["status"][7:14], _status[7:14]) ]) else: score_list[i] = -float("inf") return score_list.index(max(score_list))
def decide_sub(cls, _status, _original_status): """ ピッタリゴールの再帰処理をするために、 直前の状態と、元の状態を受け取って 今回の判断と、最終状態を返す """ result_list = [None] * 6 score_list = [0] * 6 for i in range(6): if _status[i] == 0: score_list[i] = -float("inf") result_list.append({ "status": False, "finish": False, "again": False, "robbery": False }) continue temp_status = _status.copy() temp_result = mancala.move(temp_status, i) if temp_result["again"] == True: if temp_result["finish"] == True: return i, temp_result temp_decision, temp_result = AI.decide_sub( temp_result["status"], _original_status) score_list[i] = sum([ x - y for (x, y) in zip(temp_result["status"][0:6], _original_status[0:6]) ]) + (temp_result["status"][6] - _original_status[6]) * AI.GOAL_RATIO result_list[i] = temp_result temp_decision = score_list.index(max(score_list)) return temp_decision, result_list[temp_decision]
def decide(_status): result_list = [] score_list = [0]*6 for i in range(6): if _status[i] == 0: score_list[i] = -float("inf") result_list.append({"status":False, "finish":False, "again":False, "robbery":False}) continue temp_status = _status.copy() temp_result = mancala.move(temp_status, i) if temp_result["robbery"] == True or temp_result["again"] == True: # 横取りやピッタリゴールが発生した時のみ、ゴールの玉の数の差異をスコアにする score_list[i] = temp_result["status"][6]-_status[6] else: # それ以外は0点 score_list[i] = 0 result_list.append(temp_result) # 最大値のものからランダムに選ぶ。特にピッタリゴールや横取りが無ければタダのランダム。 return random.choice([i for i, x in enumerate(score_list) if x == max(score_list)])
def decide_sub(_status, _original_status): """ ピッタリゴールの再帰処理をするために、 直前の状態と、元の状態を受け取って 今回の判断と、最終状態を返す """ result_list = [None] * 6 score_list = [0] * 6 for i in range(6): if _status[i] == 0: score_list[i] = -float("inf") result_list.append({ "status": False, "finish": False, "again": False, "robbery": False }) continue temp_status = _status.copy() temp_result = mancala.move(temp_status, i) if temp_result["again"] == True: if temp_result["finish"] == True: return i, temp_result temp_decision, temp_result = AI.decide_sub( temp_result["status"], _original_status) score_list[i] = temp_result["status"][6] - _original_status[6] result_list[i] = temp_result if score_list.count(max(score_list)) == 1: temp_decision = score_list.index(max(score_list)) return temp_decision, result_list[temp_decision] else: max_diff = max(score_list) for i in range(6): if score_list[i] == max_diff: score_list[i] = sum([ y - x for (x, y) in zip(result_list[i]["status"][7:14], _original_status[7:14]) ]) else: score_list[i] = -float("inf") temp_decision = score_list.index(max(score_list)) return temp_decision, result_list[temp_decision]
def test_move_3(): b = mancala.Board() bp, again = mancala.move(b, 0, 3) assert again == False assert bp.pits == [[4, 4, 4, 0, 5, 5], [5, 4, 4, 4, 4, 4]] assert bp.score == [1, 0]
def test_move_2(): b = mancala.Board() bp, again = mancala.move(b, 0, 2) assert again == True assert bp.pits == [[4, 4, 0, 5, 5, 5], [4, 4, 4, 4, 4, 4]] assert bp.score == [1, 0]
def test_move_0(): b = mancala.Board() bp, again = mancala.move(b, 0, 0) assert again == False assert bp.pits == [[0, 5, 5, 5, 5, 4], [4, 4, 4, 4, 4, 4]] assert bp.score == [0, 0]