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! :frowning: You can see my initial question here: Trying to create an class array with two properties - Unity Answers

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);
}

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);
    }
}