Arrays?!

I’m a beginner at programmer and especially flash and my basic knowledge on to do something is to use if statements to death.

I’m making a sort of card game where on press of a button 1 card out of the deck is randomly selected and A picture of a card with a text box reveals what card it is. Then on press of the button another different card is shown so on and so on until a certain number is reached. The only problem is that I’ve been doing this to check the random card:


function checkcard() {
randnum = random(52)+1
	
if (randnum == 1) {
// the textbox
		_root.numbervalue = "Ace";
//the score
		_root.cardtotal1.player1total = Number(_root.cardtotal1.player1total) + Number( a);	
	
}

	if (randnum == 2) {
// the textbox
		_root.numbervalue = "2";
//the score
		_root.cardtotal1.player1total = Number(_root.cardtotal1.player1total) + Number(two);

…This has only 2 done when I need 52, which would take a lot of time and space. I realise that this can probably be done easier with the use of an array but this is out of my reach.

Any help is appreciated;)

O and I know that the array question probably been asked many times before but I’ve had a quick search through the forums and tutorials and havn’t found much. If theres any good posts or tutorials pls help.

ok, i’ll give this a shot for ya:

//first make your cardArray
cardArray= new Array();
//populate it (i’m too lazy to type, so i wrote a for loop to populate it
for (i=0; i<52; i++){
cardArray*=*;
}
//if you want other values, you can add them manually

//say your function randNum returns 13:

//array function:
cardArray.splice(13,1);
//splice removes 1 member at position 13 in array

ok, this will remove the number from the array, so you have a problem in that your array will become less than 52, thus making your randNum function less than perfect, so you should rewrite your randNum function like so:

//set array length variable:
aLen=cardArray.length;
function randNum(){
randNum=random(aLen);
}
this means that your randNum will always be taken from a range that equals the number of entries in your array.

good luck, let me know if it doesn’t make sense or doesn’t work…
cheers,
-mojo

Thanks Ive made the array fine and and the splice works but
how do I pick one element out at random and then depending on which element print the corrsespnding number. Ive had a go but im not sure what to do. Do I still have to use all my If statements?

the randNum function should be returning the proper member of the array, no? your variable is also called randNum and you should be able to use it to accomplish what you want–please restate what you want to do with this v ariable–i think there’s a better way than a massive if/else statement, but i need to know what you want to do…
-mojo

Sorry mojo if i didnt make this clear but ill give it another go…
to make life easier Ill have an array length of 13, each array having a different value from ACE, 1 to 10 then jack, queen and king . I can do this fine so far.
Then depending on what random number is generated I want a textbox to display the corresponding array.

So For example if randnum = 4 , then I want whats inside cardarray[4], which is 4, to be displayed in a text box and then add the number to the total score.

I dont know if this helps or just sounds worse… but again any help is appriciated :slight_smile:

oops I forgot Also the harder part would be say If Randnum = 12 then some how the contents Cardarray[12] would appear in a textbox, which is Queen. But Jack, Queen and King is “worth” 10 points so you would add 10 to the total score

here you go, stim–i guess i’ve got nothing better to do on a cold sunday, so i worked this out for you:

it’s better to create an array of all 52 cards–it makes tracking them easier than using suits, where you’d have to do lots of checking when creating the card combos (this method would be good if you didn’t have to track a deck of 52)


//first make your cardArray
cardArray= ["2 of Hearts", "3 of Hearts","4 of Hearts", "5 of Hearts", "6 of Hearts", "7 of Hearts", "8 of Hearts", "9 of Hearts", "10 of Hearts", "Jack of Hearts", "Queen of Hearts", "King of Hearts", "Ace of Hearts","2 of Diamonds", "3 of Diamonds","4 of Diamonds", "5 of Diamonds", "6 of Diamonds", "7 of Diamonds", "8 of Diamonds", "9 of Diamonds", "10 of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds", "Ace of Diamonds","2 of Spades", "3 of Spades","4 of Spades", "5 of Spades", "6 of Spades", "7 of Spades", "8 of Spades", "9 of Spades", "10 of Spades", "Jack of Spades", "Queen of Spades", "King of Spades", "Ace of Spades","2 of Clubs", "3 of Clubs","4 of Clubs", "5 of Clubs", "6 of Clubs", "7 of Clubs", "8 of Clubs", "9 of Clubs", "10 10 of Clubs", "Jack of Clubs", "Queen of Clubs", "King of Clubs", "Ace of Clubs"];
//set card var to array length
card=cardArray.length;
score=0;
//populate it 
//uncomment below to trace all members of cardArray
//trace(cardArray.toString());


//function to take random member from cardArray 
function selectCard(){
//rand select
	card=cardArray.length;
	//trace(card);
	randCard=random(card);
	randMember=cardArray[randCard];
	//trace(randMember);
	//remove member from cardArray
	cardArray.splice(randCard,1);
//uncomment below to trace all members of cardArray
//trace(cardArray.toString());
//call scoreCard function
	scoreCard();
}
function scoreCard(){
//get first member of string, check to see if its a number
	char=randMember.charAt(0);
	char=Number(char);
	if(char==NaN){
		trace("Hi");
		score+=10;
		}else //trace(char);
		trace("Lo");
		score+=char;
	//trace(score);
}

check out the attached .fla to see it in action–
the only thing that doesn’t work yet is the score function if statement–what i’m trying to do is check the first char of the string and if it’s a number, add the number to the score, but if it returns ‘NaN’ (not a number) then it will just add 10 to the score–so even tho NaN is defined as a variable in flash, somehow my if statement ‘char==NaN’ isn’t working…

guess we’ll need some help from the rest of the board on this one, eh?
:wink: -mojo

oops, forgot the .fla…

here’s the code–lostinbeta came thru w. the right syntax for the NaN eval (thanks l.i.b.!)


function scoreCard(){
//get first member of string, check to see if its a number
	char=randMember.charAt(0);
	char=Number(char);
	if(isNaN(char)){
		score+=10;
		//exception, first char of 10 =1, and there's no 1 card, so set 1=10
		}else if(char==1){
			trace("10");
			score+=10;
		}else score+=char;
}
also note that i added another if statement--i forgot to account for the 10 card--since there's no one card, if charAt returns a 1, it means it's a 10, so i just added that to the statement--just take this code and replace the old function in the .fla and you'll be good to go...
-mojo

wow your my hero mojo sorry to waste your sunday:( but I really apprieciate this, thanks

no worries, mate? (that’s your saying,right?)–not a waste of time at all, maybe i’m getting weird, but it’s kinda fun(-:
have fun w. the code and let me know if you have any questions…
regards,
-mojo