Search Unity

Deck of cards?

Discussion in 'Scripting' started by Jait2603, Feb 26, 2018.

  1. Jait2603

    Jait2603

    Joined:
    Feb 26, 2018
    Posts:
    3
    So, I am a complete beginner who wants to make this simple game using 30 cards from normal deck of cards. So can you tell me how to get started with it’s instantiation and it’s shuffle mechanics. I have basic knowledge in scripting but have less clue in its terminology. So even small tips will help. Thank you
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That question is pretty vague. There is a post that was recently bumped in this section about shuffling cards that could help you. As for the instantiating - what do you mean/are you looking to know?
    Edit: btw, this is thread to which I was referring (it's not only about shuffling, but includes it) : https://forum.unity.com/threads/c-poker-hands.493740/

    Beyond that, I would suggest that you do some beginner tutorials from Unity ... make a small game following an example, and read some scripting tutorials.
    Then, "advance" by taking what you've learned and trying to modify and/or expand upon the idea, so you can become comfortable with Unity, scripting, and making a game.

    Come back to your 30-card game from time to time, as you learn, and work with it :)

    Here are some links to what I mentioned, if you're interested:
    https://unity3d.com/learn/tutorials/s/scripting
    https://unity3d.com/learn/tutorials/topics/interface-essentials
    https://unity3d.com/learn/tutorials
    https://unity3d.com/learn/tutorials/s/roll-ball-tutorial
    https://unity3d.com/learn/tutorials/s/space-shooter-tutorial
     
    Last edited: Feb 26, 2018
    Jait2603 likes this.
  3. Jait2603

    Jait2603

    Joined:
    Feb 26, 2018
    Posts:
    3
    So as I said I’m not very familiar with the terminology. It’s a pretty simple game with simple rules mechanics which I used to play when I was younger. I have figured out all the logic in my head I just can’t totally put it down on code if that makes any sense. So for example from parsing through google , unity forums and c# forums(also the post you mentioned helped a lot too) . I have done a program which makes a list of all the cards and game objects for them but how do I link the GOs to each specific object on the list? (If that makes any sense). But I’m sure I have made lots of progress according to my level today.
    Thanks for the advice that might help me too


    Edit: nope I didn’t see that thread before. Will look through it thanks.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm not sure: what is the game object vs the object on the list? :)
     
  5. Jait2603

    Jait2603

    Joined:
    Feb 26, 2018
    Posts:
    3
    Code (CSharp):
    1.  
    2. public struct Card {
    3.    public enum suit_enum {hearts=0,diamond,club,spade}
    4.    public enum face_enum {seven=0,eight,nine,ten,j,q,k,a}
    5.    public suit_enum suit;
    6.    public face_enum face;
    7.  
    8.  
    9.  
    10. };
    11.  
    12. public class Deck {
    13.    public List<Card> deck ;
    14.  
    15.    public Deck(){
    16.        deck = new List<Card> ();
    17.    }
    18.  
    19.    public void Generate(){
    20.        Card newCard = new Card ();
    21.        int RanNum1 = Random.Range (0, 4);
    22.        int RanNum2 = RanNum1;
    23.        while(RanNum2 == RanNum1){
    24.            RanNum2 = Random.Range (0,4);
    25.        }
    26.  
    27.        for (int i=0; i != 4; i++) {
    28.            for (int j =0; j!=8; j++) {
    29.                newCard.suit = (Card.suit_enum)i;
    30.                newCard.face = (Card.face_enum)j;
    31.                this.deck.Add (newCard);
    32.                Debug.Log ("Card '" + newCard.face + "' of '" + newCard.suit + "' added.");
    33.                GameObject newcardobject = new GameObject("Card_" + newCard.suit + "_" + newCard.face +"");
    34.                if (j == 0 && (i == RanNum1 || i == RanNum2)) {
    35.                    deck.Remove (newCard);
    36.                    Debug.Log ("Card '" + newCard.face + "' of '" + newCard.suit + "' removed.");
    37.                    GameObject.Destroy(newcardobject);
    38.                }
    39.                
    40.            }
    41.        }
    42.  
    43.    }
    44. }
    45.  
    so this is the code i have so far. Still have to implement shuffle and draw.
    But hopefully this clears up my former question.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You could have it so there's a script on each game object. That script could be the card (data), or reference a card object.
    Then, your list of cards would be scripts on game objects, and therefore you could access '.gameObject' on (any) Card.
    Hope that helps.