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 supra411 · Nov 09, 2013 at 06:40 PM · javascriptarrayclasses

Trying to create an array within an array (Round 2)

Hi, Everybody:

Ok! Round 2! I'm trying to create a poker game which the cards are shuffled to the players. I tried an program initially, but I fell flat on my face! :( You can see my initial question here: http://answers.unity3d.com/questions/571709/trying-to-create-an-class-array-with-two-propertie.html

So I'm here again, trying to attack this problem another way. This time the program creates two array (cardContent and cardList), adds two items to cardContent, then adds the array cardContent to cardList. It compiles without errors, but it's not giving me what I need. The results are:

p1.cardList: 10,D,10,D,10,D,10,D,10,D,10,D,10,D,10,D,10,D,10,D,10,D,10,D,10,D,10,D,10,D,10,D,10,D

p2.cardList: 10,S,10,S,10,S,10,S,10,S,10,S,10,S,10,S,10,S,10,S,10,S,10,S,10,S,10,S,10,S,10,S,10,S,10,S,10,S,10,S,10,S,10,S,10,S,10,S

It's suppose to give me something like: 1,D,1,H,2,D,3,H, ...

The coding is listed below. The cardContent seems to work, because Debug.Log on cardContent shows the right items, but it doesn't seem to work when I try to add cardContent to cardList (i.e., cardList.Add(cardContent). Please help!

Thanks!

Supra411

 #pragma strict

 class Card {
 var cardContent = new Array ();
 var cardList = new Array ();
 
     function AddCard (i : int, j : String) {
         cardContent.Clear();
         cardContent.Add(i);
         cardContent.Add(j);
         //Debug to see what's in cardContent.
         Debug.Log(cardContent);
         cardList.Add(cardContent);
     }
 }
     
 //Set's array for suits when shuffling.
 var suitKind = Array ("D", "C", "H", "S");
 
 var p1 : Card;
 var p2 : Card;
 
 function PassCard (i : int, suit : String) {
     if (Random.value <= .5) {
         p1.AddCard(i, suit);
     }
     else {
         p2.AddCard(i, suit);
     }
 }
 
 function Start () {
     for (var i=0; i<10; i++) {
         for (var j=0; j<4; j++) {
             var suit : String = suitKind[j];
             PassCard (i+1, suit);
         }
     }
     Debug.Log (p1.cardList);
     Debug.Log (p1.cardList.length);
     Debug.Log (p2.cardList);
     Debug.Log (p2.cardList.length);
 }
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

1 Reply

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

Answer by Peter G · Nov 09, 2013 at 07:22 PM

I'm not totally sure that the design of your program makes sense.

Your card class is currently working as both the container class for a card, and the enumeration of all the cards in the player's hand. Those are two distinctly different parts and should be treated as such. There's a number of ways you can handle this, but I would have Card represent each individual class then use a container class to hold all the cards. This is really an ideal place to use a List<>.

You should almost never use js style arrays. They're weakly typed so all the contents have to cast and uncast when you add them to the array. Also you don't need to use them the way you are to store the contents of the card. Just add two properties to the class itself.

Here are the tweaks I would make:

 #pragma strict
 import System.Collections.Generic;
 
 enum Suit {
     Heart = 0 , Diamond = 1 , Club = 2 , Spade = 3
 }
 
 class Card {
 //Maybe make it a struct.
     var suit : Suit;
     var value : int;
     
     function Card (s : Suit , v : int) {
     //Constructor simply takes suit and value.
         suit = s;
         value = v;
     }
 }
 
 var player1 :  List.<Card>;
 var player2 :  List.<Card>;
 //store all the cards each player has.
  
 function PassCard (card :Card) {
     if (Random.value <= .5) {
         player1.Add(card);
     }
     else {
          player2.Add(card);
     }
 }
  
 function Start () {
     for (var i=1; i<11; i++) {
        for (var j=0; j<4; j++) {
              var card = new Card( j , i );
              //There is an implicit cast here from int to Suit.
              //I shifted the limits of i 1 so that you didn't need to add it every time.
          
                PassCard (card);
        }
     }
     print("Player 1 has the following cards");
     for ( var c in player1 ) {
         print( c.suit + " " + c.value);
     } 
     print("Player 2 has the following cards");
     for ( var c in player2 ) {
         print( c.suit + " " + c.value);
     }
 }
Comment
Add comment · Show 2 · 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 supra411 · Nov 09, 2013 at 07:48 PM 0
Share

Thanks! The structure seems to work for my purposes. Is this a Generic List? I read through: http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?. Is there another tutorial/guide I can read? I'm new to coding, but am very excited and driven about learning how to code for fun and for designing games!

Thanks again, I'll try this tonight!

supra411

avatar image supra411 · Nov 10, 2013 at 01:42 AM 0
Share

It's works! $$anonymous$$y $$anonymous$$m decided that they wanted to use the 2D fixed array, but I'll definitely keep this in my $$anonymous$$d the next time I encounter another similar problem!

Thanks again!

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Array of custom class objects all return the same value? 1 Answer

How to use Javascript Array with classes? 1 Answer

Accessing variable from a class 1 Answer

Make an array containting multiple classes 1 Answer

Access a variable of a class stored in an array. 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