- Home /
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);
}
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);
}
}
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
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!