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.;
var player2 : List.;
//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);
}
}
↧