Merge pull request #29 from Degulleo/DO-4-AI-Controller

DO-4 AI 코드 중간 마무리
This commit is contained in:
Sehyeon 2025-03-20 16:25:46 +09:00 committed by GitHub
commit 97465ffafd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 20645 additions and 1798 deletions

File diff suppressed because it is too large Load Diff

19688
Assets/KSH/GameCopy_AI.unity Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: e200b684d5479a643aa06e6361c430c9
guid: e61de5ff6b71b2e45b0878ccd8c8033a
DefaultImporter:
externalObjects: {}
userData:

View File

@ -0,0 +1,12 @@
// AI에서만 사용하는 상수 모음
public class AIConstants
{
// 방향 상수
public static readonly int[][] Directions = new int[][]
{
new int[] {1, 0}, // 수직
new int[] {0, 1}, // 수평
new int[] {1, 1}, // 대각선 ↘ ↖
new int[] {1, -1} // 대각선 ↙ ↗
};
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: de0993e48b9548668a73768a38c11b6d
timeCreated: 1742362879

View File

@ -0,0 +1,580 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public static class AIEvaluator
{
// 패턴 가중치 상수
public struct PatternScore
{
// AI 패턴 점수
public const float FIVE_IN_A_ROW = 100000f;
public const float OPEN_FOUR = 15000f;
public const float HALF_OPEN_FOUR = 5000f;
public const float CLOSED_FOUR = 500f;
public const float OPEN_THREE = 3000f;
public const float HALF_OPEN_THREE = 500f;
public const float CLOSED_THREE = 50f;
public const float OPEN_TWO = 100f;
public const float HALF_OPEN_TWO = 30f;
public const float CLOSED_TWO = 10f;
public const float OPEN_ONE = 10f;
public const float CLOSED_ONE = 1f;
// 복합 패턴 점수
public const float DOUBLE_THREE = 8000f;
public const float DOUBLE_FOUR = 12000f;
public const float FOUR_THREE = 10000f;
// 위치 가중치 기본값
public const float CENTER_WEIGHT = 1.2f;
public const float EDGE_WEIGHT = 0.8f;
}
private static readonly int[][] Directions = AIConstants.Directions;
// 보드 전체 상태 평가
public static float EvaluateBoard(Enums.PlayerType[,] board, Enums.PlayerType aiPlayer)
{
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[])>();
// 1. 기본 패턴 평가
score += EvaluateBoardPatterns(board, aiPlayer, size, aiOpen3Positions, playerOpen3Positions,
ai4Positions, player4Positions);
// 2. 복합 패턴 평가
score += EvaluateComplexPatterns(aiOpen3Positions, playerOpen3Positions, ai4Positions, player4Positions, aiPlayer);
return score;
}
// 기본 패턴 (돌의 연속, 열린 끝 등) 평가
private static float EvaluateBoardPatterns(
Enums.PlayerType[,] board,
Enums.PlayerType aiPlayer,
int size,
List<(int row, int col, int[] dir)> aiOpen3Positions,
List<(int row, int col, int[] dir)> playerOpen3Positions,
List<(int row, int col, int[] dir)> ai4Positions,
List<(int row, int col, int[] dir)> player4Positions)
{
float score = 0;
Enums.PlayerType opponentPlayer = (aiPlayer == Enums.PlayerType.PlayerA) ?
Enums.PlayerType.PlayerB : Enums.PlayerType.PlayerA;
for (int row = 0; row < size; row++)
{
for (int col = 0; col < size; col++)
{
if (board[row, col] == Enums.PlayerType.None) continue;
Enums.PlayerType currentPlayer = board[row, col];
int playerScore = (currentPlayer == aiPlayer) ? 1 : -1; // AI는 양수, 플레이어는 음수
// 위치 가중치 계산
float positionWeight = CalculatePositionWeight(row, col, size);
foreach (var dir in Directions)
{
var (count, openEnds) = MiniMaxAIController.CountStones(board, row, col, dir, currentPlayer);
// 기본 패턴 평가 및 점수 계산
float patternScore = EvaluatePattern(count, openEnds);
// 깨진 패턴 평가
var (isBroken, brokenCount, brokenOpenEnds) =
DetectBrokenPattern(board, row, col, dir, currentPlayer);
if (isBroken) // 깨진 패턴이 있을 시 비교 후 더 높은 점수 할당
{
float brokenScore = EvaluateBrokenPattern(brokenCount, brokenOpenEnds);
patternScore = Math.Max(patternScore, brokenScore);
}
// 패턴 수집 (복합 패턴 감지용)
CollectPatterns(row, col, dir, count, openEnds, currentPlayer, aiPlayer,
aiOpen3Positions, playerOpen3Positions, ai4Positions, player4Positions);
// 위치 가중치 적용
patternScore *= positionWeight;
// 최종 점수 (플레이어는 음수)
score += playerScore * patternScore;
}
}
}
return score;
}
// 개별 패턴 평가 (돌 개수와 열린 끝 기준)
private static float EvaluatePattern(int count, int openEnds)
{
if (count >= 5)
{
return PatternScore.FIVE_IN_A_ROW;
}
else if (count == 4)
{
return (openEnds == 2) ? PatternScore.OPEN_FOUR :
(openEnds == 1) ? PatternScore.HALF_OPEN_FOUR :
PatternScore.CLOSED_FOUR;
}
else if (count == 3)
{
return (openEnds == 2) ? PatternScore.OPEN_THREE :
(openEnds == 1) ? PatternScore.HALF_OPEN_THREE :
PatternScore.CLOSED_THREE;
}
else if (count == 2)
{
return (openEnds == 2) ? PatternScore.OPEN_TWO :
(openEnds == 1) ? PatternScore.HALF_OPEN_TWO :
PatternScore.CLOSED_TWO;
}
else if (count == 1)
{
return (openEnds == 2) ? PatternScore.OPEN_ONE : PatternScore.CLOSED_ONE;
}
return 0;
}
// 복합 패턴 평가 (3-3, 4-4, 4-3 등)
private static float EvaluateComplexPatterns(
List<(int row, int col, int[] dir)> aiOpen3Positions,
List<(int row, int col, int[] dir)> playerOpen3Positions,
List<(int row, int col, int[] dir)> ai4Positions,
List<(int row, int col, int[] dir)> player4Positions,
Enums.PlayerType aiPlayer)
{
float score = 0;
// 삼삼(3-3) 감지
int aiThreeThree = DetectDoubleThree(aiOpen3Positions);
int playerThreeThree = DetectDoubleThree(playerOpen3Positions);
// 사사(4-4) 감지
int aiFourFour = DetectDoubleFour(ai4Positions);
int playerFourFour = DetectDoubleFour(player4Positions);
// 사삼(4-3) 감지
int aiFourThree = DetectFourThree(ai4Positions, aiOpen3Positions);
int playerFourThree = DetectFourThree(player4Positions, playerOpen3Positions);
// 복합 패턴 점수 합산
score += aiThreeThree * PatternScore.DOUBLE_THREE;
score -= playerThreeThree * PatternScore.DOUBLE_THREE;
score += aiFourFour * PatternScore.DOUBLE_FOUR;
score -= playerFourFour * PatternScore.DOUBLE_FOUR;
score += aiFourThree * PatternScore.FOUR_THREE;
score -= playerFourThree * PatternScore.FOUR_THREE;
return score;
}
// 위치 가중치 계산 함수
private static float CalculatePositionWeight(int row, int col, int size)
{
float boardCenterPos = (size - 1) / 2.0f;
// 현재 위치와 중앙과의 거리 계산 (0~1 사이 값)
float distance = Math.Max(Math.Abs(row - boardCenterPos), Math.Abs(col - boardCenterPos)) / boardCenterPos;
// 중앙(거리 0)은 1.2배, 가장자리(거리 1)는 0.8배
return PatternScore.CENTER_WEIGHT - ((PatternScore.CENTER_WEIGHT - PatternScore.EDGE_WEIGHT) * distance);
}
// 방향이 평행한지 확인하는 함수
private static bool AreParallelDirections(int[] dir1, int[] dir2) // Vector로 변경
{
return (dir1[0] == dir2[0] && dir1[1] == dir2[1]) ||
(dir1[0] == -dir2[0] && dir1[1] == -dir2[1]);
}
// 패턴 수집 함수 (복합 패턴 감지용)
private static void CollectPatterns(
int row, int col, int[] dir, int count, int openEnds,
Enums.PlayerType currentPlayer, Enums.PlayerType aiPlayer,
List<(int row, int col, int[] dir)> aiOpen3Positions,
List<(int row, int col, int[] dir)> playerOpen3Positions,
List<(int row, int col, int[] dir)> ai4Positions,
List<(int row, int col, int[] dir)> player4Positions)
{
// 열린 3 패턴 수집
if (count == 3 && openEnds == 2)
{
if (currentPlayer == aiPlayer)
aiOpen3Positions.Add((row, col, dir));
else
playerOpen3Positions.Add((row, col, dir));
}
// 4 패턴 수집
if (count == 4 && openEnds >= 1)
{
if (currentPlayer == aiPlayer)
ai4Positions.Add((row, col, dir));
else
player4Positions.Add((row, col, dir));
}
}
#region Complex Pattern (3-3, 4-4, 4-3)
// 삼삼(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;
}
// 사사(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
#region Evaluate Move Position
// 이동 평가 함수
public static float EvaluateMove(Enums.PlayerType[,] board, int row, int col, Enums.PlayerType AIPlayer)
{
float score = 0;
Enums.PlayerType opponentPlayer = (AIPlayer == Enums.PlayerType.PlayerA) ?
Enums.PlayerType.PlayerB : Enums.PlayerType.PlayerA;
// 복합 패턴 감지를 위한 위치 저장 리스트
List<(int[] dir, int count, int openEnds)> aiPatterns = new List<(int[], int, int)>();
List<(int[] dir, int count, int openEnds)> opponentPatterns = new List<(int[], int, int)>();
// AI 관점에서 평가
board[row, col] = AIPlayer;
foreach (var dir in Directions)
{
// 평가를 위한 가상 보드이기에 캐시 데이터에 저장X
var (count, openEnds) = MiniMaxAIController.CountStones(board, row, col, dir, AIPlayer, false);
aiPatterns.Add((dir, count, openEnds));
if (count >= 4)
{
score += PatternScore.FIVE_IN_A_ROW / 10;
}
else if (count == 3)
{
score += (openEnds == 2) ? PatternScore.OPEN_THREE / 3 :
(openEnds == 1) ? PatternScore.HALF_OPEN_THREE / 5 :
PatternScore.CLOSED_THREE / 5;
}
else if (count == 2)
{
score += (openEnds == 2) ? PatternScore.OPEN_TWO / 2 :
(openEnds == 1) ? PatternScore.HALF_OPEN_TWO / 3 :
PatternScore.CLOSED_TWO / 5;
}
else if (count == 1)
{
score += (openEnds == 2) ? PatternScore.OPEN_ONE :
PatternScore.CLOSED_ONE;
}
// 깨진 패턴 평가
var (isBroken, brokenCount, brokenOpenEnds) = DetectBrokenPattern(board, row, col, dir, AIPlayer);
if (isBroken)
{
float brokenScore = EvaluateBrokenPattern(brokenCount, brokenOpenEnds);
score = Math.Max(score, brokenScore);
}
}
// AI 복합 패턴 점수 계산 (새로 추가)
score += EvaluateComplexMovePatterns(aiPatterns, true);
// 상대 관점에서 평가 (방어 가치)
board[row, col] = opponentPlayer;
foreach (var dir in Directions)
{
var (count, openEnds) = MiniMaxAIController.CountStones(board, row, col, dir, opponentPlayer, false);
opponentPatterns.Add((dir, count, openEnds));
// 상대 패턴 차단에 대한 가치 (약간 낮은 가중치)
if (count >= 4)
{
score += PatternScore.FIVE_IN_A_ROW / 12.5f;
}
else if (count == 3)
{
score += (openEnds == 2) ? PatternScore.OPEN_THREE / 3.75f :
(openEnds == 1) ? PatternScore.HALF_OPEN_THREE / 6.25f :
PatternScore.CLOSED_THREE / 6.25f;
}
else if (count == 2)
{
score += (openEnds == 2) ? PatternScore.OPEN_TWO / 2.5f :
(openEnds == 1) ? PatternScore.HALF_OPEN_TWO / 3.75f :
PatternScore.CLOSED_TWO / 5f;
}
else if (count == 1)
{
score += (openEnds == 2) ? PatternScore.OPEN_ONE / 1.25f :
PatternScore.CLOSED_ONE;
}
}
score += EvaluateComplexMovePatterns(opponentPatterns, false);
// 원래 상태로 복원
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 EvaluateComplexMovePatterns(List<(int[] dir, int count, int openEnds)> patterns, bool isAI)
{
float score = 0;
// 열린 3 패턴 및 4 패턴 찾기
var openThrees = patterns.Where(p => p.count == 3 && p.openEnds == 2).ToList();
var fours = patterns.Where(p => p.count == 4 && p.openEnds >= 1).ToList();
// 3-3 패턴 감지
if (openThrees.Count >= 2)
{
for (int i = 0; i < openThrees.Count; i++)
{
for (int j = i + 1; j < openThrees.Count; j++)
{
if (!AreParallelDirections(openThrees[i].dir, openThrees[j].dir))
{
float threeThreeScore = PatternScore.DOUBLE_THREE / 4; // 복합 패턴 가중치
score += isAI ? threeThreeScore : threeThreeScore;
break;
}
}
}
}
// 4-4 패턴 감지
if (fours.Count >= 2)
{
for (int i = 0; i < fours.Count; i++)
{
for (int j = i + 1; j < fours.Count; j++)
{
if (!AreParallelDirections(fours[i].dir, fours[j].dir))
{
float fourFourScore = PatternScore.DOUBLE_FOUR / 4;
score += isAI ? fourFourScore : fourFourScore;
break;
}
}
}
}
// 4-3 패턴 감지
if (fours.Count > 0 && openThrees.Count > 0)
{
float fourThreeScore = PatternScore.FOUR_THREE / 4;
score += isAI ? fourThreeScore : fourThreeScore;
}
return score;
}
// 깨진 패턴 (3-빈칸-1) 감지
private static (bool isDetected, int count, int openEnds) DetectBrokenPattern(
Enums.PlayerType[,] board, int row, int col, int[] dir, Enums.PlayerType player)
{
int size = board.GetLength(0);
int totalStones = 1; // 현재 위치의 돌 포함
int gapCount = 0; // 빈칸 개수
int openEnds = 0; // 열린 끝 개수
// 정방향 탐색
int r = row, c = col;
for (int i = 1; i <= 5; i++)
{
r += dir[0];
c += dir[1];
if (r < 0 || r >= size || c < 0 || c >= size)
break;
if (board[r, c] == player)
{
totalStones++;
}
else if (board[r, c] == Enums.PlayerType.None)
{
if (gapCount < 1) // 최대 1개 빈칸만 허용
{
gapCount++;
}
else
{
openEnds++;
break;
}
}
else // 상대방 돌
{
break;
}
}
// 역방향 탐색
r = row; c = col;
for (int i = 1; i <= 5; i++)
{
r -= dir[0];
c -= dir[1];
if (r < 0 || r >= size || c < 0 || c >= size)
break;
if (board[r, c] == player)
{
totalStones++;
}
else if (board[r, c] == Enums.PlayerType.None)
{
if (gapCount < 1)
{
gapCount++;
}
else
{
openEnds++;
break;
}
}
else
{
break;
}
}
// 깨진 패턴 감지: 총 돌 개수 ≥ 4 그리고 빈칸이 1개
bool isDetected = (totalStones >= 4 && gapCount == 1);
return (isDetected, totalStones, openEnds);
}
// 깨진 패턴 점수 계산 함수
private static float EvaluateBrokenPattern(int count, int openEnds)
{
if (count >= 5) // 5개 이상 돌이 있으면 승리
{
return PatternScore.FIVE_IN_A_ROW;
}
else if (count == 4) // 깨진 4
{
return (openEnds == 2) ? PatternScore.OPEN_FOUR * 0.8f :
(openEnds == 1) ? PatternScore.HALF_OPEN_FOUR * 0.8f :
PatternScore.CLOSED_FOUR * 0.7f;
}
else if (count == 3) // 깨진 3
{
return (openEnds == 2) ? PatternScore.OPEN_THREE * 0.7f :
(openEnds == 1) ? PatternScore.HALF_OPEN_THREE * 0.7f :
PatternScore.CLOSED_THREE * 0.6f;
}
return 0;
}
#endregion
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 34f6533696ad41518da4bcc203309338
timeCreated: 1742357084

View File

@ -5,34 +5,33 @@ using UnityEngine;
public static class MiniMaxAIController
{
// To-Do List
// 탐색 시간 개선: 캐싱(_stoneInfoCache), 좋은 수부터 탐색(Move Ordering)
// AI 난이도 개선
private const int SEARCH_DEPTH = 3; // 탐색 깊이 제한 (3 = 빠른 응답, 4 = 좀 더 강한 AI 그러나 느린)
private const int WIN_COUNT = 5;
private static int[][] _directions = new int[][]
{
new int[] {1, 0}, // 수직
new int[] {0, 1}, // 수평
new int[] {1, 1}, // 대각선 ↘ ↖
new int[] {1, -1} // 대각선 ↙ ↗
};
private static int[][] _directions = AIConstants.Directions;
private static int _playerLevel = 1; // 급수 설정
private static float _mistakeMove;
private static Enums.PlayerType _AIPlayerType = Enums.PlayerType.PlayerB;
// 중복 계산을 방지하기 위한 캐싱 데이터. 위치(row, col) 와 방향(dirX, dirY) 중복 계산 방지
private static Dictionary<(int, int, int, int), (int count, int openEnds)> _stoneInfoCache
= new Dictionary<(int, int, int, int), (int count, int openEnds)>();
private static System.Random _random = new System.Random(); // 랜덤 실수용 Random 함수
// 중복 계산을 방지하기 위한 캐싱 데이터. 위치 기반 (그리드 기반 해시맵)
private static Dictionary<(int row, int col), Dictionary<(int dirX, int dirY), (int count, int openEnds)>>
_spatialStoneCache = new Dictionary<(int row, int col), Dictionary<(int dirX, int dirY), (int count, int openEnds)>>();
// AI Player Type 변경 (AI가 선수로 둘 수 있을지도 모르니..)
public static void SetAIPlayerType(Enums.PlayerType AIPlayerType)
{
_AIPlayerType = AIPlayerType;
}
// 급수 설정 -> 실수 넣을 때 계산
public static void SetLevel(int level)
{
_playerLevel = level;
// 레벨에 따른 실수율? 설정
_mistakeMove = GetMistakeProbability(_playerLevel);
}
@ -71,7 +70,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)
@ -88,7 +87,7 @@ public static class MiniMaxAIController
}
// 랜덤 실수
if (secondBestMove != null && UnityEngine.Random.value < _mistakeMove) // UnityEngine.Random.value == 0~1 사이 반환
if (secondBestMove != null && _random.NextDouble() < _mistakeMove)
{
Debug.Log("AI Mistake");
return secondBestMove;
@ -100,9 +99,9 @@ public static class MiniMaxAIController
private static float DoMinimax(Enums.PlayerType[,] board, int depth, bool isMaximizing, float alpha, float beta,
int recentRow, int recentCol)
{
if (CheckGameWin(Enums.PlayerType.PlayerA, board, recentRow, recentCol)) return -100 + depth;
if (CheckGameWin(Enums.PlayerType.PlayerB, board, recentRow, recentCol)) return 100 - depth;
if (depth == 0) return EvaluateBoard(board);
if (CheckGameWin(Enums.PlayerType.PlayerA, board, recentRow, recentCol, true)) return -100 + depth;
if (CheckGameWin(Enums.PlayerType.PlayerB, board, recentRow, recentCol, true)) return 100 - depth;
if (depth == 0) return AIEvaluator.EvaluateBoard(board, _AIPlayerType);
float bestScore = isMaximizing ? float.MinValue : float.MaxValue;
List<(int row, int col, float score)> validMoves = GetValidMoves(board); // 현재 놓을 수 있는 자리 리스트
@ -111,13 +110,11 @@ public static class MiniMaxAIController
{
board[row, col] = isMaximizing ? Enums.PlayerType.PlayerB : Enums.PlayerType.PlayerA;
ClearCachePartial(row, col); // 부분 초기화
// ClearCache();
float minimaxScore = DoMinimax(board, depth - 1, !isMaximizing, alpha, beta, row, col);
board[row, col] = Enums.PlayerType.None;
ClearCachePartial(row, col);
// ClearCache();
if (isMaximizing)
{
@ -147,14 +144,18 @@ public static class MiniMaxAIController
{
if (board[row, col] == Enums.PlayerType.None && HasNearbyStones(board, row, col))
{
float score = EvaluateBoard(board);
// 보드 전체가 아닌 해당 돌에 대해서만 Score 계산
float score = AIEvaluator.EvaluateMove(board, row, col, _AIPlayerType);
validMoves.Add((row, col, score));
}
}
}
// score가 높은 순으로 정렬 -> 더 좋은 수 먼저 계산하도록 함
validMoves.Sort((a, b) => b.Item3.CompareTo(a.Item3));
return validMoves;
// 시간 단축을 위해 상위 10-15개만 고려. 일단 15개
return validMoves.Take(15).ToList();
}
private static bool HasNearbyStones(Enums.PlayerType[,] board, int row, int col, int distance = 3)
@ -176,14 +177,16 @@ public static class MiniMaxAIController
}
// 특정 방향으로 같은 돌 개수와 열린 끝 개수를 계산하는 함수
private static (int count, int openEnds) CountStones(
Enums.PlayerType[,] board, int row, int col, int[] direction, Enums.PlayerType player)
public static (int count, int openEnds) CountStones(
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);
var posKey = (row, col);
var dirKey = (dirX, dirY);
// 캐시에 존재하면 바로 반환 (탐색 시간 감소)
if (_stoneInfoCache.TryGetValue(key, out var cachedResult))
if (_spatialStoneCache.TryGetValue(posKey, out var dirCache) &&
dirCache.TryGetValue(dirKey, out var cachedResult))
{
return cachedResult;
}
@ -222,53 +225,57 @@ public static class MiniMaxAIController
}
var resultValue = (count, openEnds);
_stoneInfoCache[key] = resultValue; // 결과 저장
if(isSaveInCache) // 결과 저장
{
if (!_spatialStoneCache.TryGetValue(posKey, out dirCache))
{
dirCache = new Dictionary<(int, int), (int, int)>();
_spatialStoneCache[posKey] = dirCache;
}
dirCache[dirKey] = (count, openEnds);
}
return resultValue;
}
#region Cache Clear
// 캐시 초기화, 새로운 돌이 놓일 시 실행
private static void ClearCache()
{
_stoneInfoCache.Clear();
_spatialStoneCache.Clear();
}
// 캐시 부분 초기화 (현재 변경된 위치 N에서 반경 5칸만 초기화)
private static void ClearCachePartial(int centerRow, int centerCol, int radius = 5)
// 캐시 부분 초기화 (현재 변경된 위치 N에서 반경 4칸만 초기화)
private static void ClearCachePartial(int centerRow, int centerCol, int radius = 4)
{
// 캐시가 비어있으면 아무 작업도 하지 않음
if (_stoneInfoCache.Count == 0) return;
// 제거할 키 목록
List<(int, int, int, int)> keysToRemove = new List<(int, int, int, int)>();
// 모든 캐시 항목을 검사
foreach (var key in _stoneInfoCache.Keys)
{
var (row, col, _, _) = key;
// 거리 계산
int distance = Math.Max(Math.Abs(row - centerRow), Math.Abs(col - centerCol));
if (_spatialStoneCache.Count == 0) return;
// 지정된 반경 내에 있는 캐시 항목을 삭제 목록에 추가
if (distance <= radius)
{
keysToRemove.Add(key);
}
}
// 반경 내의 키 제거
foreach (var key in keysToRemove)
for (int r = centerRow - radius; r <= centerRow + radius; r++)
{
_stoneInfoCache.Remove(key);
for (int c = centerCol - radius; c <= centerCol + radius; c++)
{
// 반경 내 위치 확인
if (Math.Max(Math.Abs(r - centerRow), Math.Abs(c - centerCol)) <= radius)
{
// 해당 위치의 캐시 항목 제거
_spatialStoneCache.Remove((r, c));
}
}
}
}
#endregion
// 최근에 둔 돌 위치 기반으로 게임 승리를 판별하는 함수
public static bool CheckGameWin(Enums.PlayerType player, Enums.PlayerType[,] board, int row, int col)
// MinimaxAIController 밖의 cs파일은 호출 시 맨 마지막을 false로 지정해야 합니다.
public static bool CheckGameWin(Enums.PlayerType player, Enums.PlayerType[,] board,
int row, int col, bool isSavedCache)
{
foreach (var dir in _directions)
{
var (count, _) = CountStones(board, row, col, dir, player);
var (count, _) = CountStones(board, row, col, dir, player, isSavedCache);
// 자기 자신 포함하여 5개 이상일 시 true 반환
if (count + 1 >= WIN_COUNT)
@ -305,12 +312,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++)
{
@ -320,21 +406,191 @@ public static class MiniMaxAIController
Enums.PlayerType player = board[row, col];
int playerScore = (player == _AIPlayerType) ? 1 : -1; // AI는 양수, 플레이어는 음수
// 위치 가중치 계산. 중앙 중심으로 돌을 두도록 함
float positionWeight = CalculatePositionWeight(row, col, size);
foreach (var dir in _directions)
{
var (count, openEnds) = CountStones(board, row, col, dir, player);
// 점수 계산
if (count == 4)
score += playerScore * (openEnds == 2 ? 10000 : 1000);
float patternScore = 0;
if (count >= 5)
{
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)
score += playerScore * (openEnds == 2 ? 1000 : 100);
{
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)
score += playerScore * (openEnds == 2 ? 100 : 10);
{
patternScore = (openEnds == 2) ? 100 : (openEnds == 1) ? 30 : 10;
}
else if (count == 1)
{
patternScore = (openEnds == 2) ? 10 : 1;
}
// 위치 가중치 적용
patternScore *= positionWeight;
// 최종 점수 적용 (플레이어는 음수)
score += playerScore * patternScore;
}
}
}
// 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;
}
// 위치 가중치 계산 함수
private static float CalculatePositionWeight(int row, int col, int size)
{
float boardCenterPos = (size - 1) / 2.0f;
// 현재 위치와 중앙과의 거리 계산 (0~1 사이 값)
float distance = Math.Max(Math.Abs(row - boardCenterPos), Math.Abs(col - boardCenterPos)) / boardCenterPos;
// 중앙(거리 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
*/
}

View File

@ -0,0 +1,26 @@
using System;
using UnityEngine;
using System.Threading.Tasks;
public class OmokAI : MonoBehaviour
{
public static OmokAI Instance;
private void Awake()
{
Instance = this;
}
public async void StartBestMoveSearch(Enums.PlayerType[,] board, Action<(int, int)?> callback)
{
(int row, int col)? bestMove = await Task.Run(() => MiniMaxAIController.GetBestMove(board));
callback?.Invoke(bestMove);
}
// true: Win, false: Lose
public bool CheckGameWin(Enums.PlayerType player, Enums.PlayerType[,] board, int row, int col)
{
bool isWin = MiniMaxAIController.CheckGameWin(player, board, row, col, false);
return isWin;
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 576baa0fe98d40608bf48109ba5ed788
timeCreated: 1742286909

View File

@ -1,84 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
public class TestGameManager : MonoBehaviour
{
[SerializeField] private TMP_InputField rowText;
[SerializeField] private TMP_InputField colText;
[SerializeField] private TMP_Text boardText;
private Enums.PlayerType[,] _board;
private void Start()
{
_board = new Enums.PlayerType[15, 15];
MiniMaxAIController.SetLevel(1); // 급수 설정 테스트
ResultBoard();
}
public void OnClickGoButton()
{
int row = int.Parse(rowText.text);
int col = int.Parse(colText.text);
if (_board[row, col] != Enums.PlayerType.None)
{
Debug.Log("중복 위치");
return;
}
_board[row, col] = Enums.PlayerType.PlayerA;
Debug.Log($"Player's row: {row} col: {col}");
// var isEnded = MiniMaxAIController.CheckGameWin(Enums.PlayerType.PlayerA, _board, row, col);
// Debug.Log("PlayerA is Win: " + isEnded);
// 인공지능 호출
var result = MiniMaxAIController.GetBestMove(_board);
if (result.HasValue)
{
Debug.Log($"AI's row: {result.Value.row} col: {result.Value.col}");
_board[result.Value.row, result.Value.col] = Enums.PlayerType.PlayerB;
// isEnded = MiniMaxAIController.CheckGameWin(Enums.PlayerType.PlayerB, _board, result.Value.row, result.Value.col);
// Debug.Log("PlayerB is Win: " + isEnded);
}
ResultBoard();
}
private void ResultBoard()
{
boardText.text = "";
// player 타입에 따라 입력받는 건 무조건 A로 해서 A, AI는 B로 나타내고 None은 _
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
if (_board[i, j] == Enums.PlayerType.PlayerA)
{
boardText.text += 'A';
}
else if (_board[i, j] == Enums.PlayerType.PlayerB)
{
boardText.text += 'B';
}
else if (_board[i, j] == Enums.PlayerType.None)
{
boardText.text += '_';
}
boardText.text += ' ';
}
boardText.text += '\n';
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: facc79abb6042e846bb0a2b099b58e9c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -86,6 +86,11 @@ public class AIState: BasePlayerState
{
gameLogic.fioTimer.StartTimer();
//TODO: AI이식
OmokAI.Instance.StartBestMoveSearch(gameLogic.GetBoard(), (bestMove) =>
{
if(bestMove.HasValue)
HandleMove(gameLogic, bestMove.Value.Item1, bestMove.Value.Item2);
});
}
public override void OnExit(GameLogic gameLogic)
@ -210,7 +215,7 @@ public class GameLogic : MonoBehaviour
{
case Enums.GameType.SinglePlay:
firstPlayerState = new PlayerState(true);
secondPlayerState = new PlayerState(false);
secondPlayerState = new AIState();
SetState(firstPlayerState);
break;
case Enums.GameType.MultiPlay:
@ -221,6 +226,12 @@ public class GameLogic : MonoBehaviour
break;
}
}
public Enums.PlayerType[,] GetBoard()
{
return _board;
}
//착수 버튼 클릭시 호출되는 함수
public void OnConfirm()
{
@ -288,6 +299,7 @@ public class GameLogic : MonoBehaviour
LastNSelectedSetting(row, col);
ReplayManager.Instance.RecordStonePlaced(Enums.StoneType.White, row, col);
break;
}
}