using UnityEngine;
using System.Collections.Generic;
public class PairGenerator : MonoBehaviour
{
void Start()
{
// 그룹 A, B, C에 숫자 할당
List<int> groupA = new List<int>() { 1, 2, 3, 4, 5, 6 };
List<int> groupB = new List<int>() { 7, 8, 9, 10, 11, 12, 13 };
List<int> groupC = new List<int>() { 14, 15, 16, 17, 18, 19, 20 };
// 숫자 쌍을 저장할 리스트
List<List<int>> pairs = new List<List<int>>();
// 겹치는 그룹이 없는 10개의 숫자 쌍을 무작위로 뽑기
for (int i = 0; i < 10; i++)
{
List<int> selectedPair = new List<int>();
// 각 그룹에서 숫자를 무작위로 선택
List<int> availableGroups = new List<int> { 0, 1, 2 }; // A: 0, B: 1, C: 2
while (selectedPair.Count < 2 && availableGroups.Count > 0)
{
int randomGroupIndex = Random.Range(0, availableGroups.Count);
int groupIndex = availableGroups[randomGroupIndex];
List<int> selectedGroup = GetGroupByIndex(groupIndex, groupA, groupB, groupC);
if (selectedGroup.Count > 0)
{
int randomIndex = Random.Range(0, selectedGroup.Count);
selectedPair.Add(selectedGroup[randomIndex]);
selectedGroup.RemoveAt(randomIndex);
availableGroups.Remove(groupIndex);
}
}
if (selectedPair.Count == 2)
{
pairs.Add(selectedPair);
}
}
// 결과 출력
foreach (List<int> pair in pairs)
{
Debug.Log(pair[0] + " " + pair[1]);
}
}
// 인덱스에 따라 그룹을 반환
List<int> GetGroupByIndex(int index, List<int> groupA, List<int> groupB, List<int> groupC)
{
switch (index)
{
case 0:
return groupA;
case 1:
return groupB;
case 2:
return groupC;
default:
return new List<int>();
}
}
}
이거 실행하면 유니티 멈춰버림 ㅅㅂ 왜 멈추는거지????????????????????????????
버전은 2021.3.20f1
유니티 문제가 아니라 니 코드가 무한루프에 빠져서 그런거잖아. 각 그룹에 원소가 6,7,7개 있는데 페어를 10개 뽑고 이 과정이 랜덤이니까 그룹 a,b에서 6쌍, b,c에서 1쌍을 뽑는 경우 같은게 발생하면 남는 원소가 0,0,6이 돼서 조건 만족하는 그룹 2개를 절대 못 뽑으니 무한루프에 빠지지
ㅋㅋ 어지간하면 이유없이 안멈춤 조건이 무한루프에 빠질 가능성이 존재하네
질문 좀 해본 놈이고 ㅋㅋㅋㅋ
질문좀 해본늄이노
위에서 이미 코드문제는 말해줬는데 리스트다루는거 보니까 리스트가 참조형식인거 모르는 느낌인데 중간에 결과 출력하면서 확인 ㄱㄱ