Search code examples
c#unity-game-engine2d-games

how do i check if my value match with other like a pair matching game


So i have a game with 9 boxes, there are 3 different number, each number spread 3 diferrent boxes at random position, every time i roll the wheel, 1 random number will be generate, which will open its corresponding. e.g if i roll number 3, box 3 will open... so far i've done that, but now i am stuck at how to detect that if they matching with the box i open before (mean if i open 2 box with same multiplier inside)

//here is my box class

public class Safe 
{
    public int multiplier;
    public GameObject reward;
    
}

 //and here is my gamemanager class
public class GameManager : MonoBehaviour
{
    public Text[] mText;
    public GameObject[] openBox;
    private int a, b, c;
    public Safe[] safe;
    // Start is called before the first frame update
    void Start()
    {
        GenerateRandom()
    }

    // Update is called once per frame
    void Update()
    {
        // this is where i check, e.g if i roll number 5, box 5 will open..
        switch (WheelController.finalAngle)
        {
            case 0:
                openBox[0].SetActive(true);
                mText[1].gameObject.SetActive(true);
                safe[1].reward.gameObject.SetActive(true);
                break;
            case 40:
                openBox[1].SetActive(true);
                mText[2].gameObject.SetActive(true);
                safe[2].reward.gameObject.SetActive(true);
                break;
            case 80:
                openBox[2].SetActive(true);
                mText[3].gameObject.SetActive(true);
                safe[3].reward.gameObject.SetActive(true);
                break;
            case 120:
                openBox[3].SetActive(true);
                mText[4].gameObject.SetActive(true);
                safe[4].reward.gameObject.SetActive(true);
                break;
            case 160:
                openBox[4].SetActive(true);
                mText[5].gameObject.SetActive(true);
                safe[5].reward.gameObject.SetActive(true);
                break;
            case 200:
                openBox[5].SetActive(true);
                mText[6].gameObject.SetActive(true);
                safe[6].reward.gameObject.SetActive(true);
                break;
            case 240:
                openBox[6].SetActive(true);
                mText[7].gameObject.SetActive(true);
                safe[7].reward.gameObject.SetActive(true);
                break;
            case 280:
                openBox[7].SetActive(true);
                mText[8].gameObject.SetActive(true);
                safe[8].reward.gameObject.SetActive(true);
                break;
            case 320:
                openBox[8].SetActive(true);
                mText[0].gameObject.SetActive(true);
                safe[0].reward.gameObject.SetActive(true);
                break;

        }
        Matching();

    }
    void GenerateRandom() //this is where i generate random Array 
    {
       a = Random.Range(15, 21);
        b = Random.Range(15, 21);
        c = Random.Range(15, 21);
        while ((a == b || b == c || a == c))
        {
            a = Random.Range(15, 21);
            b = Random.Range(15, 21);
            c = Random.Range(15, 21);
        } 
        for (int i = 0; i < mText.Length; i++)
        {
            if (i % 3==0)
            {
                mText[i].text = "x"+ a.ToString();
                safe[i].multiplier = a;
            }
            if (i % 3 == 1)
            {
                mText[i].text = "x" + b.ToString();
                safe[i].multiplier = b;
            }
            if (i % 3 == 2)
            {
                mText[i].text = "x" + c.ToString();
                safe[i].multiplier = c;
            }
        }
    }
    void Matching()
    {
        // I WANT TO CHECK IF THEY MATCHING AND WIN THE GAME. 
    }
}

Solution

  • You will need to cache the boxes you open somehow, either in their own fields for first/second/third opening and so on, or in an array.

    Then in your matching method, you can check the box you just opened against the values you have already cached. If they match, you know that the player has matched two (or three or whatever your game requires) in a row, and at that point you can trigger whatever you want to do when a player wins.

    If you check against your previous numbers and there was no match, you can just cache the most recent number as the last one picked, and let the player start over again picking numbers.