DO-4 [Fix] score 평가 개선

This commit is contained in:
Sehyeon 2025-03-18 16:31:14 +09:00
parent 4050a8d4ea
commit ec5f2d82e7

View File

@ -25,7 +25,7 @@ public static class MiniMaxAIController
private static Enums.PlayerType _AIPlayerType = Enums.PlayerType.PlayerB;
// 중복 계산을 방지하기 위한 캐싱 데이터. 위치(row, col) 와 방향(dirX, dirY) 중복 계산 방지
private static Dictionary<(int, int, int, int), (int count, int openEnds)> _stoneInfoCache
private static Dictionary<(int, int, int, int), (int count, int openEnds)> _stoneCountCache
= new Dictionary<(int, int, int, int), (int count, int openEnds)>();
// 급수 설정 -> 실수 넣을 때 계산
@ -71,7 +71,7 @@ public static class MiniMaxAIController
foreach (var (row, col, _) in validMoves)
{
board[row, col] = _AIPlayerType;
float score = DoMinimax(board, SEARCH_DEPTH, false, -1000, 1000, row, col);
float score = DoMinimax(board, SEARCH_DEPTH, false, -1000000, 1000000, row, col);
board[row, col] = Enums.PlayerType.None;
if (score > bestScore)
@ -145,12 +145,14 @@ public static class MiniMaxAIController
{
if (board[row, col] == Enums.PlayerType.None && HasNearbyStones(board, row, col))
{
float score = EvaluateBoard(board);
// 보드 전체가 아닌 해당 돌에 대해서만 Score 계산
float score = EvaluateMove(board, row, col);
validMoves.Add((row, col, score));
}
}
}
// score가 높은 순으로 정렬 -> 더 좋은 수 먼저 계산하도록 함
validMoves.Sort((a, b) => b.Item3.CompareTo(a.Item3));
return validMoves;
}
@ -175,13 +177,13 @@ public static class MiniMaxAIController
// 특정 방향으로 같은 돌 개수와 열린 끝 개수를 계산하는 함수
private static (int count, int openEnds) CountStones(
Enums.PlayerType[,] board, int row, int col, int[] direction, Enums.PlayerType player)
Enums.PlayerType[,] board, int row, int col, int[] direction, Enums.PlayerType player, bool isSaveInCache = true)
{
int dirX = direction[0], dirY = direction[1];
var key = (row, col, dirX, dirY);
// 캐시에 존재하면 바로 반환 (탐색 시간 감소)
if (_stoneInfoCache.TryGetValue(key, out var cachedResult))
if (_stoneCountCache.TryGetValue(key, out var cachedResult))
{
return cachedResult;
}
@ -220,27 +222,33 @@ public static class MiniMaxAIController
}
var resultValue = (count, openEnds);
_stoneInfoCache[key] = resultValue; // 결과 저장
if(isSaveInCache) // 결과 저장
{
_stoneCountCache[key] = resultValue;
}
return resultValue;
}
#region Cache Clear
// 캐시 초기화, 새로운 돌이 놓일 시 실행
private static void ClearCache()
{
_stoneInfoCache.Clear();
_stoneCountCache.Clear();
}
// 캐시 부분 초기화 (현재 변경된 위치 N에서 반경 5칸만 초기화)
private static void ClearCachePartial(int centerRow, int centerCol, int radius = 5)
{
// 캐시가 비어있으면 아무 작업도 하지 않음
if (_stoneInfoCache.Count == 0) return;
if (_stoneCountCache.Count == 0) return;
// 제거할 키 목록
List<(int, int, int, int)> keysToRemove = new List<(int, int, int, int)>();
// 모든 캐시 항목을 검사
foreach (var key in _stoneInfoCache.Keys)
foreach (var key in _stoneCountCache.Keys)
{
var (row, col, _, _) = key;
@ -257,10 +265,12 @@ public static class MiniMaxAIController
// 반경 내의 키 제거
foreach (var key in keysToRemove)
{
_stoneInfoCache.Remove(key);
_stoneCountCache.Remove(key);
}
}
#endregion
// 최근에 둔 돌 위치 기반으로 게임 승리를 판별하는 함수
public static bool CheckGameWin(Enums.PlayerType player, Enums.PlayerType[,] board, int row, int col)
{
@ -304,12 +314,91 @@ public static class MiniMaxAIController
return fiveInARowMoves;
}
#region Evaluate Score
// 특정 위치의 Score를 평가하는 새로운 함수
private static float EvaluateMove(Enums.PlayerType[,] board, int row, int col)
{
float score = 0;
board[row, col] = _AIPlayerType;
foreach (var dir in _directions)
{
// CountStones를 사용하나 캐시에 저장X, 가상 계산이기 때문..
var (count, openEnds) = CountStones(board, row, col, dir, _AIPlayerType, false);
if (count >= 4)
{
score += 10000;
}
else if (count == 3)
{
score += (openEnds == 2) ? 1000 : (openEnds == 1) ? 100 : 10;
}
else if (count == 2)
{
score += (openEnds == 2) ? 50 : (openEnds == 1) ? 10 : 5;
}
else if (count == 1)
{
score += (openEnds == 2) ? 10 : (openEnds == 1) ? 5 : 1;
}
}
// 상대 돌로 바꿔서 평가
board[row, col] = Enums.PlayerType.PlayerB;
foreach (var dir in _directions)
{
// 캐시 저장X
var (count, openEnds) = CountStones(board, row, col, dir, Enums.PlayerType.PlayerB, false);
// 상대 패턴 차단에 대한 가치 (방어 점수)
if (count >= 4)
{
score += 8000;
}
else if (count == 3)
{
score += (openEnds == 2) ? 800 : (openEnds == 1) ? 80 : 8;
}
else if (count == 2)
{
score += (openEnds == 2) ? 40 : (openEnds == 1) ? 8 : 4;
}
else if (count == 1)
{
score += (openEnds == 2) ? 8 : (openEnds == 1) ? 4 : 1;
}
}
// 원래 상태로 복원
board[row, col] = Enums.PlayerType.None;
// 중앙에 가까울수록 추가 점수
int size = board.GetLength(0);
float centerDistance = Math.Max(
Math.Abs(row - (size - 1) / 2.0f),
Math.Abs(col - (size - 1) / 2.0f)
);
float centerBonus = 1.0f - (centerDistance / ((size - 1) / 2.0f)) * 0.3f; // 30% 가중치
return score * centerBonus;
}
// 현재 보드 평가 함수
private static float EvaluateBoard(Enums.PlayerType[,] board)
{
float score = 0;
int size = board.GetLength(0);
// 복합 패턴 감지를 위한 위치 저장 리스트
List<(int row, int col, int[] dir)> aiOpen3Positions = new List<(int, int, int[])>();
List<(int row, int col, int[] dir)> playerOpen3Positions = new List<(int, int, int[])>();
List<(int row, int col, int[] dir)> ai4Positions = new List<(int, int, int[])>();
List<(int row, int col, int[] dir)> player4Positions = new List<(int, int, int[])>();
for (int row = 0; row < size; row++)
{
for (int col = 0; col < size; col++)
@ -331,16 +420,34 @@ public static class MiniMaxAIController
if (count >= 5)
{
Debug.Log("count 평가: " + count);
Debug.Log("over 5 counts. count amount: " + count);
patternScore = 100000;
}
else if (count == 4)
{
patternScore = (openEnds == 2) ? 15000 : (openEnds == 1) ? 5000 : 500;
// 4 패턴 위치 저장
if (openEnds >= 1)
{
if (player == _AIPlayerType)
ai4Positions.Add((row, col, dir));
else
player4Positions.Add((row, col, dir));
}
}
else if (count == 3)
{
patternScore = (openEnds == 2) ? 3000 : (openEnds == 1) ? 500 : 50;
// 3 패턴 위치 저장
if (openEnds == 2)
{
if (player == _AIPlayerType)
aiOpen3Positions.Add((row, col, dir));
else
playerOpen3Positions.Add((row, col, dir));
}
}
else if (count == 2)
{
@ -359,6 +466,27 @@ public static class MiniMaxAIController
}
}
}
// 2. 복합 패턴 감지 및 점수 부여 (4,4 / 3,3 / 4,3)
int aiThreeThree = DetectDoubleThree(aiOpen3Positions);
int playerThreeThree = DetectDoubleThree(playerOpen3Positions);
int aiFourFour = DetectDoubleFour(ai4Positions);
int playerFourFour = DetectDoubleFour(player4Positions);
int aiFourThree = DetectFourThree(ai4Positions, aiOpen3Positions);
int playerFourThree = DetectFourThree(player4Positions, playerOpen3Positions);
// 복합 패턴 점수 추가
score += aiThreeThree * 8000;
score -= playerThreeThree * 8000;
score += aiFourFour * 12000;
score -= playerFourFour * 12000;
score += aiFourThree * 10000;
score -= playerFourThree * 10000;
return score;
}
@ -373,4 +501,96 @@ public static class MiniMaxAIController
// 중앙(거리 0)은 1.2배, 가장자리(거리 1)는 0.8배
return 1.2f - (0.4f * distance);
}
// 삼삼(3-3) 감지 함수
private static int DetectDoubleThree(List<(int row, int col, int[] dir)> openThreePositions)
{
int doubleThreeCount = 0;
var checkedPairs = new HashSet<(int, int)>();
for (int i = 0; i < openThreePositions.Count; i++)
{
var (row1, col1, dir1) = openThreePositions[i];
for (int j = i + 1; j < openThreePositions.Count; j++)
{
var (row2, col2, dir2) = openThreePositions[j];
// 같은 돌에서 다른 방향으로 두 개의 열린 3이 형성된 경우
if (row1 == row2 && col1 == col2 && !AreParallelDirections(dir1, dir2))
{
if (!checkedPairs.Contains((row1, col1)))
{
doubleThreeCount++;
checkedPairs.Add((row1, col1));
}
}
}
}
return doubleThreeCount;
}
// 방향이 평행한지 확인하는 함수
private static bool AreParallelDirections(int[] dir1, int[] dir2)
{
return (dir1[0] == dir2[0] && dir1[1] == dir2[1]) ||
(dir1[0] == -dir2[0] && dir1[1] == -dir2[1]);
}
// 사사(4-4) 감지 함수
private static int DetectDoubleFour(List<(int row, int col, int[] dir)> fourPositions)
{
int doubleFourCount = 0;
var checkedPairs = new HashSet<(int, int)>();
for (int i = 0; i < fourPositions.Count; i++)
{
var (row1, col1, dir1) = fourPositions[i];
for (int j = i + 1; j < fourPositions.Count; j++)
{
var (row2, col2, dir2) = fourPositions[j];
if (row1 == row2 && col1 == col2 && !AreParallelDirections(dir1, dir2))
{
if (!checkedPairs.Contains((row1, col1)))
{
doubleFourCount++;
checkedPairs.Add((row1, col1));
}
}
}
}
return doubleFourCount;
}
// 사삼(4-3) 감지 함수
private static int DetectFourThree(List<(int row, int col, int[] dir)> fourPositions,
List<(int row, int col, int[] dir)> openThreePositions)
{
int fourThreeCount = 0;
var checkedPairs = new HashSet<(int, int)>();
foreach (var (row1, col1, _) in fourPositions)
{
foreach (var (row2, col2, _) in openThreePositions)
{
// 같은 돌에서 4와 열린 3이 동시에 형성된 경우
if (row1 == row2 && col1 == col2)
{
if (!checkedPairs.Contains((row1, col1)))
{
fourThreeCount++;
checkedPairs.Add((row1, col1));
}
}
}
}
return fourThreeCount;
}
#endregion
}