toolsmax0_COMP3911_Game/Assets/Scripts/Raw/AI/ShowMoney.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


class ShowMoney : MonoBehaviour
{

    [Tooltip("Money prefabs")]
    public GameObject money_100;
    public GameObject money_50;
    public GameObject money_20;
    public GameObject money_10;
    public Transform base_position;

    private Transform offset;
    private Transform next_pos;
    private int crt_amount = 0;

    private List<GameObject> showed_money;

    void Start()
    {
        offset = new GameObject().transform;
        showed_money = new List<GameObject>();
        offset.position = new Vector3(0.1f, 0, 0);
        next_pos = new GameObject().transform;
        next_pos.position = base_position.transform.position;
    }
    /// <summary>
    /// Show money on the table
    /// </param> greedy: if true, the money combination will  greedy (large money first), otherwise random

    /// </summary>
    public void ShowAmount(int amount, bool greedy = true)
    {
        //todo generated by copilot
        if (amount <= 0 || amount % 10 != 0)
        {
            throw new System.Exception("ShowMoney: amount must be a multiple of 10");
        }
        int[] money = Decompose(amount, greedy);
        Debug.Log("Attempt to ShowMoney: " + amount + " = " + money[0] + " * 10 + " + money[1] + " * 20 + " + money[2] + " * 50 + " + money[3] + " * 100");
        for (int i = 0; i < money.Length; i++)
        {

            GameObject money_object = null;
            switch (i)
            {
                case 3:
                    money_object = money_100;
                    crt_amount += 100;
                    break;
                case 2:
                    money_object = money_50;
                    crt_amount += 50;
                    break;
                case 1:
                    money_object = money_20;
                    crt_amount += 20;
                    break;
                case 0:
                    money_object = money_10;
                    crt_amount += 10;
                    break;
            }

            for (int j = 0; j < money[i]; j++)
            {
                next_pos.position += offset.position;
                // Debug.Log("ShowMoney: " + next_pos.position);
                //rotate 90 along x-axis

                GameObject new_money = Instantiate(money_object, next_pos.position, money_object.transform.rotation);
                //randomize rotation
                new_money.transform.parent = base_position;
                showed_money.Add(new_money);
            }

        }
    }


    public int GetMoneyAmountOnTable()
    {
        return crt_amount;
    }

    public void ClearTable()
    {
        foreach (GameObject go in showed_money)
        {
            Destroy(go);
        }
        showed_money.Clear();
        crt_amount = 0;
        next_pos.position = base_position.transform.position;

    }

    private int[] Decompose(int amount, bool greedy)
    {
        int remain = amount;

        //else randomized
        int[] money_amounts = new int[4] { 0, 0, 0, 0 };
        if (greedy)
        {
            money_amounts[3] = remain / 100;
            money_amounts[2] = (remain %= 100) / 50;
            money_amounts[1] = (remain %= 50) / 20;
            money_amounts[0] = (remain %= 20) / 10;
        }

        //decompose the amount into 10, 20, 50, 100
        while (remain > 0)
        {
            if (remain >= 100)
            {
                //33 % prob
                if (Random.Range(0, 3) == 0)
                {
                    money_amounts[3]++;
                    remain -= 100;
                }
            }
            if (remain >= 50)
            {
                if (Random.Range(0, 3) == 0)
                {
                    money_amounts[2]++;
                    remain -= 50;
                }
            }
            if (remain >= 20)
            {
                if (Random.Range(0, 2) == 0)
                {
                    money_amounts[1]++;
                    remain -= 20;
                }
            }
            if (remain >= 10)
            {
                if (Random.Range(0, 2) == 0)
                {
                    money_amounts[0]++;
                    remain -= 10;
                }
            }
        }
        return money_amounts;
    }



    public void Main()
    {
        for (int i = 0; i < 1000; i++)
        {
            int n = Random.Range(0, 100);
            n *= 10;
            int[] res = Decompose(n, false);
            if (res[0] * 10 + res[1] * 20 + res[2] * 50 + res[3] * 100 != n)
            {
                Debug.Log("Error: " + n + " = " + res[0] + " * 10 + " + res[1] + " * 20 + " + res[2] + " * 50 + " + res[3] + " * 100");
            }
        }
    }
}
