Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Zero147 · Aug 30, 2014 at 02:14 AM · indexof

My code works, I just don't understand WHY it works

 #pragma strict
 
 import System.Collections.Generic;
 
 var cols:int = 4; // the number of columns in the card grid
 var rows:int = 4; // the number of rows in the card grid
 var totalCards:int = 16;
 var matchesNeededToWin:int = totalCards * 0.5; // If there are 16 cards, the player needs to find 8 matches to clear the board
 var matchesMade:int = 0; // At the outset, the player has not made any matches
 var cardW:int = 100; // Each card's width and height is 100 pixels
 var cardH:int = 100;
 var aCards:List.<Card>; // We'll store all the cards we create in this List
 var aGrid:Card[,]; // This 2d array will keep track of the shuffled, dealt cards
 var aCardsFlipped:List.<Card>; // This generic array list will store the two cards that the player flips over
 var playerCanClick:boolean; // We'll use this flag to prevent the player from clicking buttons when we don't want him to
 var playerHasWon:boolean = false; // Store whether or not the player has won. This should probably start out false :)
 
 function Start ()
 {
     playerCanClick = true; // We should let the player play, don't you think?
 
     // Initialize some empty Collections:
     aCards = new List.<Card>(); // this Generic List is our deck of cards. It can only ever hold instances of the Card class.
     aGrid = new Card[rows,cols]; // The rows and cols variables help us define the dimensions of this 2D array
     aCardsFlipped = new List.<Card>(); // This List will store the two cards the player flips over. 
 
     BuildDeck();
     
     // Loop through the total number of rows in our aGrid List:  
     for(var i:int = 0; i<rows; i++)
     {
 
         // For each individual grid row, loop through the total number of columns in the grid:
         for(var j:int = 0; j<cols; j++)
         { 
             var someNum:int = Random.Range(0,aCards.Count);
             aGrid[i,j] = aCards[someNum];
             aCards.RemoveAt(someNum);
         }
     }
     //for(i=0; i<1000; i++)
     //{
     //    Debug.Log(Random.Range(0,10));  // Uncomment to play around with Random
     //}
 }
 
 function OnGUI ()
 {
     GUILayout.BeginArea (Rect (0,0,Screen.width,Screen.height));    
     BuildGrid();
     GUILayout.EndArea();
     //print("building grid!");
 }
 
 function BuildGrid()
 {
     GUILayout.BeginVertical();
     GUILayout.FlexibleSpace();
     for(var i:int=0; i<rows; i++)
       {
         GUILayout.BeginHorizontal();
         GUILayout.FlexibleSpace();
         for(var j:int=0; j<cols; j++)
         {
               var card:Card = aGrid[i,j];
               var img:String;
               if(card.isFaceUp)
               {
                   img = card.img;
               }    else {
                   img = "wrench";
               }         
               if(GUILayout.Button(Resources.Load(img), GUILayout.Width(cardW)))
               {
                    if(playerCanClick)
                   {
                   FlipCardFaceUp(card);
                 //Debug.Log(card.img);
                   }
             }
         }
         GUILayout.FlexibleSpace();
         GUILayout.EndHorizontal();   
       }
       GUILayout.FlexibleSpace();
       GUILayout.EndVertical();
 }
 
 class Card extends System.Object
 {
     var isFaceUp:boolean = false;
     var isMatched:boolean = false;
     var img:String;
     
     function Card(img:String)
     {
         this.img = img;
     }
 }
 
 function BuildDeck()
 {
     var totalRobots:int = 4; // We've got four robots to work with
     var card:Card; // this stores a reference to a card
     
     for(var i:int=0; i<totalRobots; i++)
     {
         var aRobotParts:List.<String> = new List.<String>();
         aRobotParts.Add("Head");
         aRobotParts.Add("Arm");
         aRobotParts.Add("Leg");
         for(var j:int=0; j<2; j++)
         {
             var someNum:int = Random.Range(0, aRobotParts.Count);
             var theMissingPart:String = aRobotParts[someNum];
             
             aRobotParts.RemoveAt(someNum);
             
             card = new Card("robot" + (i+1) + "Missing" + theMissingPart);
             aCards.Add(card);
                 
             card = new Card("robot" + (i+1) + theMissingPart);
             aCards.Add(card);
         }    
     }
 }
 
 function FlipCardFaceUp(card:Card)
 {
     card.isFaceUp = true;
     if(aCardsFlipped.IndexOf(card) < 0 )
     {
         aCardsFlipped.Add(card);
     }
     Debug.Log(aCardsFlipped.IndexOf(card));
     
     if(aCardsFlipped.Count == 2)
     {
         playerCanClick = false;
         
         yield WaitForSeconds(1);
         
         aCardsFlipped[0].isFaceUp = false;
         aCardsFlipped[1].isFaceUp = false;
         
         aCardsFlipped = new List.<Card>();
         playerCanClick = true;
     }
     
 }

What we're going to look at is the FlipCardFaceUp function near the end of the code, inside is this little snippet:

if(aCardsFlipped.IndexOf(card) < 0 )

{

aCardsFlipped.Add(card);

}

I've been trying to figure out these 4 lines of code for maybe 3 hours and I still don't understand what's going on, even though my book explains it, it's still not clicking with me.

This if statement confuses me so much!!

I'll be specific, my book added this if statement because if you click the same card twice it would flip back over anyway, somehow adding this if statement prevents that from happening, but I don't know why.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by rutter · Aug 30, 2014 at 02:20 AM

I'll be honest with you: that's a lot of code to dig through, and it's a nice Friday night and I just got off work. So I'm going to skip all of that and get right to your bottom question. This code:

 if(aCardsFlipped.IndexOf(card) < 0 )
 {
     aCardsFlipped.Add(card);
 }

First off, you'll need to understand how if statements work, and the < (less than) operator, and the fact that IndexOf is returning an int that's being compared against zero. If any of that confuses you, find tutorials that explain logic and branching. Important stuff.

Second, we know that IndexOf returns an int value, but let's talk about how it picks a value. Your list is sort of like an array, right? It's a series of values, each of which has a numbered "index". IndexOf starts in box 0, and works up from there, checking for an item that's equal to whatever you're asking about. In this case, it's looking for a particular card.

If IndexOf finds your card, it tells you which index holds that card. If you happen to have the same card in the list more than once, it only tells you about the first one it finds. If you don't have that card in your list at all, it returns -1. Why? Because your list index numbering starts counting from 0, so -1 is a special value indicating that there's no such item found.

So, getting back to the question, let's explain with a comment:

 //if list does not contain this card, add it to the list
 if(aCardsFlipped.IndexOf(card) < 0 )
 {
     aCardsFlipped.Add(card);
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zero147 · Aug 30, 2014 at 03:32 PM 0
Share

This is the kind of stuff that I won't learn in books, awesome!

But why does IndexOf only tell me about the first one that I find?

When I debug this code and keep clicking the same card it will always report back 0, it's at index 0 but does this mean IndexOf will only check for any new cards and won't compare to the current one twice? am I understanding that correctly?

avatar image
0

Answer by TheDarkVoid · Aug 30, 2014 at 02:19 AM

That if statement basically checks the list of flipped cards to see if the current card is in that list, if it is then it's index will be 0 or greater, if it's not in that list the IndexOf function returns -1 which passes the if statement and allows the card to be added to the list of flipped cards.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by AyAMrau · Aug 30, 2014 at 02:23 AM

IndexOf returns -1 if the item was not found, so if the selected card is not in the aCardsFlipped array (IndexOf returned -1 which is less than 0), then add it (flip it) otherwise ignore it.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make an array of buttons open corresponding images on click? 1 Answer

Implicit Downcast warning from "List.IndexOf" ...Bad? Unavoidable? 1 Answer

Foreach's char loop not functioning as expected 1 Answer

For loop & Foreach don't complete inside IEnumerator when using IndexOf 2 Answers

Search Game Inventory Button Object Text Strings 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges