From c12f2ec477b497e460cb7511adc7dc096b9863e6 Mon Sep 17 00:00:00 2001 From: fiore Date: Sat, 15 Mar 2025 02:11:00 +0900 Subject: [PATCH] =?UTF-8?q?DO-31=20feat=20:=20=EC=9E=A5=EB=AA=A9=20?= =?UTF-8?q?=EA=B8=88=EC=88=98=20=EB=B0=98=ED=99=98=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Script/Renju/OverlineDetector.cs | 88 ++++++++++++++++++++ Assets/Script/Renju/OverlineDetector.cs.meta | 3 + 2 files changed, 91 insertions(+) create mode 100644 Assets/Script/Renju/OverlineDetector.cs create mode 100644 Assets/Script/Renju/OverlineDetector.cs.meta diff --git a/Assets/Script/Renju/OverlineDetector.cs b/Assets/Script/Renju/OverlineDetector.cs new file mode 100644 index 0000000..75c6531 --- /dev/null +++ b/Assets/Script/Renju/OverlineDetector.cs @@ -0,0 +1,88 @@ +using UnityEngine; + +public class OverlineDetector:RenjuDetectorBase +{ + /// + /// 특정 위치에 흑돌을 놓았을 때 장목(6목 이상)이 생기는지 검사 + /// + /// 현재 보드 상태 + /// 행 좌표 + /// 열 좌표 + /// 장목 금수 여부 (true: 금수, false: 금수 아님) + public bool IsOverline(Enums.PlayerType[,] board, int row, int col) + { + // 빈 위치가 아니면 검사할 필요 없음 + if (!IsEmptyPosition(board, row, col)) + { + return false; + } + + // 임시 보드 생성 + Enums.PlayerType[,] tempBoard = (Enums.PlayerType[,])board.Clone(); + + // 해당 위치에 흑돌 놓기 + tempBoard[row, col] = Enums.PlayerType.PlayerA; + + // 각 방향으로 연속된 돌 개수 검사 + foreach (Vector2Int dir in directions) + { + int count = CountConsecutiveStones(tempBoard, row, col, dir); + + // 6목 이상인 경우 장목 금수 + if (count >= 6) + { + Debug.Log($"장목 발견: ({row}, {col}) 방향: ({dir.x}, {dir.y}), 연속 돌 개수: {count}"); + return true; + } + } + + return false; + } + + /// + /// 특정 방향으로 연속된 같은 색 돌의 개수를 세는 함수 + /// + private int CountConsecutiveStones(Enums.PlayerType[,] board, int row, int col, Vector2Int dir) + { + Enums.PlayerType stone = board[row, col]; + if (stone != Enums.PlayerType.PlayerA) + { + return 0; + } + + int count = 1; // 현재 돌 포함 + int dRow = dir.x; + int dCol = dir.y; + + // 정방향 검사 + for (int i = 1; i <= 5; i++) // 최대 5칸까지만 검사 (5칸 이후로는 6목 이상이 확정) + { + int r = row + i * dRow; + int c = col + i * dCol; + + if (!IsValidPosition(r, c) || board[r, c] != stone) + { + break; + } + + count++; + } + + // 역방향 검사 + for (int i = 1; i <= 5; i++) // 최대 5칸까지만 검사 + { + int r = row - i * dRow; + int c = col - i * dCol; + + if (!IsValidPosition(r, c) || board[r, c] != stone) + { + break; + } + + count++; + } + + return count; + } + +} diff --git a/Assets/Script/Renju/OverlineDetector.cs.meta b/Assets/Script/Renju/OverlineDetector.cs.meta new file mode 100644 index 0000000..84997fc --- /dev/null +++ b/Assets/Script/Renju/OverlineDetector.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 0727203eebbc4c3e823fd4ee045cb713 +timeCreated: 1741966933 \ No newline at end of file