Unity/로또번호 추첨

[유니티] 로또번호 추첨하기(2)

Skull Crusher 2021. 4. 27. 10:48
728x90

1. C# Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LottoGame : MonoBehaviour
{
    //인스펙터에 생김
    //public int[] arrays;
    public int gameCount;
        
    // Start is called before the first frame update
    void Start()
    {
        int[] lottoBalls = new int[6];

        int[] lottoBallCounts = new int[45];

        int count = 0;
        for (count=0; count<gameCount; count++)
        {
            string lottoStr = "";

            for (int i = 0; i < lottoBalls.Length; i++)
            {
                lottoBalls[i] = Random.Range(1, 46);

                bool isSame = false;
                for (int j = 0; j < i; j++)
                {
                    if (lottoBalls[i] == lottoBalls[j])
                    {
                        isSame = true;
                        i--;
                        break;
                    }
                }

                if (isSame) continue;
                lottoStr += (lottoBalls[i].ToString() + ((i == lottoBalls.Length - 1) ? "" : ","));

                int ballNum = lottoBalls[i];

                lottoBallCounts[ballNum - 1]++;
            }

            Debug.Log(lottoStr);
        }

        string printMsg = "";
        for (count = 0; count < lottoBallCounts.Length; count++)
        {
            printMsg += (((count + 1) + "(" + lottoBallCounts[count] + ")") 
                + ((count == lottoBallCounts.Length - 1) ? "" : ", "));
        }

        Debug.Log(printMsg);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 

2. 인스펙터에서 Game Count 값 10 번 입력

주말에 이 번호로 사봐야겠다 ㅎㅎ