따라서 알파 베타 제거는 하드 코딩 (tic-tac-toe) 이외에도 가장 효율적인 알고리즘 인 것으로 보입니다. 그러나 링크에서 주어진 C++ 예제에서 알고리즘을 변환하는 데 문제가 있습니다 : http://www.webkinesia.com/games/gametree.php.C++에서 Ruby로 알파 베타 제거 알고리즘을 변환 할 때의 문제
플레이어는 1 또는 0이므로 플레이어를 전환하면 1-player
이 전환됩니다.
WIN = 1
LOSS = -1
DRAW = 0
INFINITY = 100
def calculate_ai_next_move
best_move = -1
best_score = -INFINITY
cur_player = COMPUTER
self.remaining_moves.each do |move|
self.make_move_with_index(move, cur_player)
score = -self.alphabeta(-INFINITY,INFINITY, 1 - cur_player)
self.undo_move(move)
if score > best_score
best_score = score
best_move = move
end
end
return best_move
end
def alphabeta(alpha, beta, player)
best_score = -INFINITY
if not self.has_available_moves?
return WIN if self.has_this_player_won?(player) == player
return LOSS if self.has_this_player_won?(1 - player) == 1 - player
return DRAW
else
self.remaining_moves.each do |move|
break if alpha > beta
self.make_move_with_index(move, player)
move_score = -alphabeta(-beta, -alpha, 1 - player)
self.undo_move(move)
if move_score > alpha
alpha = move_score
next_move = move
end
best_score = alpha
end
end
return best_score
end
현재 알고리즘은 매우 놀고 있습니다. 처음에는 마지막 공간을 선택하고 그 다음에 첫 번째 (왼쪽에서 오른쪽으로) 사용 가능한 공간을 선택합니다.
무엇이 잘못 되었습니까?
또한 TDD를 해왔으므로 self.has_this_player_won ?, self.undo_move 및 self.remaining_moves가 정확한지 확인했습니다.
명백한 것들 :'has_this_player_won?'은 부울 값을 반환하지만 그것을 정수와 비교합니다; 당신은 결코'DRAW'를 돌려 보내지 않습니다; 재귀 호출은 다음과 같아야합니다 :'move_score = -alphabeta (-beta, -alpha, 1-player)'그리고 TDD를 사용하고 있기 때문에, AI에게 간단한 위치 (2 또는 3 개의 자유 셀) 그리고 올바른 점수를 반환하는지 확인하십시오. – adamax
has_this_player_won? 획득 한 경우에만 플레이어를 반환합니다. 그렇지 않으면 false. 그리기를 추가했습니다. 내 재귀 호출이 어떻게 생겼습니까? – NullVoxPopuli
TDD의 경우,이 알고리즘에 대해 더 많이 배워야 만 몇 가지 테스트를 직접 만들 수 있습니다. – NullVoxPopuli