Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Memory Game

Discussion in 'Editor & General Support' started by Alvarezmd90, Dec 4, 2019.

  1. Alvarezmd90

    Alvarezmd90

    Joined:
    Jul 21, 2016
    Posts:
    151
    Hello forum,



    I'm creating a memory game with an opponent that perfectly remembers certain cards opened by either the AI or the player. Will release full code if I manage to make it work.
    Thing is, it has one major flaw:

    Code (CSharp):
    1.  
    2.     private IEnumerator AITurnCard(int Idx, float Delay)
    3.     {
    4.         yield return new WaitForSeconds(Delay);
    5.         if (Choice.Count > 0)
    6.         {
    7.             Debug.Break();
    8.             while (Deck[Idx]==Choice[0])
    9.             {
    10.                 Debug.Log("Detected a faulty selection");
    11.                 Idx = Random.Range(0, Deck.Count);
    12.                 yield return null;
    13.             }
    14.         }
    15.         Deck[Idx].GetComponent<Card>().Choose();
    16.     }
    17.  
    The opponent draws a random card from available from the deck but sometimes, it wants to flip the same card as the first one/choice. I have multiple variables and while loops checking so it doesn't happen, and yet, the debug.log doesn't appear. Does anyone know what's going on?
    Thanks in advance.
     
    Last edited: Dec 4, 2019
  2. Alvarezmd90

    Alvarezmd90

    Joined:
    Jul 21, 2016
    Posts:
    151
    I think I found the issue so after I run a few tests I'll upload the project as an imported asset as promised.
    The issue was that I created two different types of lists. Deck holds gameobjects and choice held the component with the 'card' script. So
    Code (CSharp):
    1. while (Deck[Idx]==Choice[0])
    had to be changed to
    Code (CSharp):
    1. while (Deck[Idx]==Choice[0].gameObject)
     
  3. Alvarezmd90

    Alvarezmd90

    Joined:
    Jul 21, 2016
    Posts:
    151
    Here's the unity package. Have fun getting owned by the AI. As it's mostly flawless as I had set it's memory capacity to 8 in the inspector. :D
     

    Attached Files: