Search Unity

c# listing items in if statement.

Discussion in 'Scripting' started by klausbous, Sep 11, 2017.

  1. klausbous

    klausbous

    Joined:
    Sep 7, 2017
    Posts:
    16
    I am looking for a way to list items in a if statement. Right now i have this:
    Code (CSharp):
    1. int hand1 = Random.Range(0, 52);
    2.  
    3. if(hand1 == 4  && hand2 == 0 && hand3 == 1 && hand4 == 2 && hand5 == 3  ||
    4.             hand1 == 4  && hand2 == 3 && hand3 == 2 && hand4 == 1 && hand5 == 0  ||
    5.             hand1 == 4  && hand2 == 2 && hand3 == 0 && hand4 == 3 && hand5 == 1  ||
    6.             hand1 == 4  && hand2 == 1 && hand3 == 3 && hand4 == 0 && hand5 == 2 ){
    7.             Debug.Log("works!");
    8.         }
    but i am wondering if there is a easier way for example...

    Code (CSharp):
    1. if (hand1 == 0  ||  1  ||  2  ||  3  ||  4   && hand2 == 0  ||  1  ||  2  ||  3  ||  4   && hand3 == 0  ||  1  ||  2  ||  3  ||  4   && hand4 == 0  ||  1  ||  2  ||  3  ||  4   && hand5 == 0  ||  1  ||  2  ||  3  ||  4 ) {
    2. Debug.Log("Works!");
    3. }
    If someone knows how to do it then please share! :)
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Start with learning to use arrays. You should absolutely be using an array to represent your hand.

    Also check the for loop tutorial, as that goes hand in hand with array.

    When you have that, you can then also use < and > operators to check for whole ranges of values.
    Code (csharp):
    1. int[] hand = new int[]{1,2,3,4,5};
    2. bool foundOneOutsideRange = false;
    3. for (int h=0; h<hand.Length; h++) {
    4. if (hand[h] <= 4 && hand[h] >= 0) {
    5. foundOneOutsideRange = true;
    6. }
    7. }
    8.  
    9. if (!foundOneOutsideRange) {
    10. //all items in your hand are within the specified range
    11. }
    Should be enough to get you started.
     
  3. unit_nick

    unit_nick

    Joined:
    Jul 15, 2017
    Posts:
    23
    Code (csharp):
    1.  
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Test : MonoBehaviour
    6. {
    7.     public int maxHands = 2;
    8.  
    9.     private List<int> hands;    
    10.  
    11.     private void Start()
    12.     {
    13.         hands = new List<int>();
    14.  
    15.         // initialize 10 items with random number of hands
    16.         for(int i = 0; i < 10; i++)
    17.             hands.Add(Random.Range(0, 52));
    18.  
    19.         /*
    20.          *
    21.          * "I am looking for a way to list items in an if statement"
    22.          *
    23.          */
    24.         if (hands.TrueForAll(numHands => numHands <= maxHands))
    25.         {
    26.             Debug.Log(string.Format("No items have too many hands"));
    27.         }
    28.         else
    29.             Debug.Log(string.Format("Some items have too many hands"));
    30.     }
    31. }
    32.