2 Replies - 3119 Views - Last Post: 20 January 2010 - 08:00 AM

#1 Novice_panda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-January 10

Flash card game shuffle problem

Posted 14 January 2010 - 09:57 PM

hi I need a little help on my school project. i'm a newbie at action scripting and i can't seem to get rid of the "same card" problem I always get after a few rounds in the game. could someone explain to me what i can do to at least fix this? I all ready placed "if" and "else if" conditions but its still not working

heres the code

on(release)
{
	
	num1 = Math.round(Math.random()*51 + 2);
	num2 = Math.round(Math.random()*51 + 2);
	num3 = Math.round(Math.random()*51 + 2);
	num4 = Math.round(Math.random()*51 + 2);
	num5 = Math.round(Math.random()*51 + 2);
	num6 = Math.round(Math.random()*51 + 2);
	num7 = Math.round(Math.random()*51 + 2);
	num8 = Math.round(Math.random()*51 + 2);
	num9 = Math.round(Math.random()*51 + 2);
	num10 = Math.round(Math.random()*51 + 2);
	num11 = Math.round(Math.random()*51 + 2);
	num12 = Math.round(Math.random()*51 + 2);
	
	
	if(num2 == num1){
	num2 = Math.round(num2 + 1);
	}
	
		
	if(num3 == num1){
	num3 = Math.round(num3 + 1);
	}
	else if(num3 == num2){
	num3 = Math.round(num3 + 1);
	}
	

	if(num4 == num1){
	num4 = Math.round(num4 + 1);
	}
	else if (num4 == num2){
	num4 = Math.round(num4 + 1);
	}
	else if(num4 == num3){
	num4 = Math.round(num4 + 1);
	}
	

	if(num5 == num1){
	num5 = Math.round(num5 + 1);
	}
	else if (num5 == num2){
	num5 = Math.round(num5 + 1);
	}
	else if(num5 == num3){
	num5 = Math.round(num5 + 1);
	}
	else if(num5 == num4){
	num5 = Math.round(num5 + 1);
	}
	
		
	if(num6 == num1){
	num6 = Math.round(num6 + 1);
	}
	else if (num6 == num2){
	num6 = Math.round(num6 + 1);
	}	
	else if(num6 == num3){
	num6 = Math.round(num6 + 1);
	}
	else if(num6 == num4){
	num6 = Math.round(num6 + 1);
	}
	else if (num6 == num5){
	num6 = Math.round(num6 + 1);
	}
	

	if(num7 == num1){
	num7 = Math.round(num7 + 1);
	}
	else if (num7 == num2){
	num7 = Math.round(num7 + 1);
	}
	else if(num7 == num3){
	num7 = Math.round(num7 + 1);
	}
	else if(num7 == num4){
	num7 = Math.round(num7 + 1);
	}
	else if (num7 == num5){
	num7 = Math.round(num7 + 1);
	}
	else if(num7 == num6){
	num7 = Math.round(num7 + 1);
	}
	else if (num7 == num8){
	num7 = Math.round(num7 + 1);
	}
	else if(num7 == num9){
	num7 = Math.round(num7 + 1);
	}
	else if (num7 == num10){
	num7 = Math.round(num7 + 1);
	}
	else if(num7 == num11){
	num7 = Math.round(num7 + 1);
	}
	else if (num7 == num12){
	num7 = Math.round(num7 + 1);
	}
	
	if(num8 == num1){
	num8 = Math.round(num8 + 1);
	}
	else if (num9 == num2){
	num9 = Math.round(num9 + 1);
	}
	else if(num8 == num3){
	num8 = Math.round(num8 + 1);
	}
	else if(num8 == num4){
	num8 = Math.round(num8 + 1);
	}
	else if (num8 == num5){
	num8 = Math.round(num8 + 1);
	}
	else if(num8 == num6){
	num8 = Math.round(num8 + 1);
	}
	else if (num8 == num7){
	num8 = Math.round(num8 + 1);
	}
	
	
	if(num9 == num1){
	num9 = Math.round(num9 + 1);
	}
	else if (num9 == num2){
	num9 = Math.round(num9 + 1);
	}
	else if(num9 == num3){
	num9 = Math.round(num9 + 1);
	}
	else if(num9 == num4){
	num9 = Math.round(num9 + 1);
	}
	else if (num9 == num5){
	num9 = Math.round(num9 + 1);
	}
	else if(num9 == num6){
	num9 = Math.round(num9 + 1);
	}
	else if (num9 == num7){
	num9 = Math.round(num9 + 1);
	}
	else if(num9 == num8){
	num9 = Math.round(num9 + 1);
	}
	
	
	if(num10 == num1){
	num10 = Math.round(num10 + 1);
	}
	else if (num10 == num2){
	num10 = Math.round(num10 + 1);
	}
	else if(num10 == num3){
	num10 = Math.round(num10 + 1);
	}
	else if(num10 == num4){
	num10 = Math.round(num10 + 1);
	}
	else if (num10 == num5){
	num10 = Math.round(num10 + 1);
	}
	else if(num10 == num6){
	num10 = Math.round(num10 + 1);
	}
	else if (num10 == num7){
	num10 = Math.round(num10 + 1);
	}
	else if(num10 == num8){
	num10 = Math.round(num10 + 1);
	}
	else if (num10 == num9){
	num10 = Math.round(num10 + 1);
	}
	
	
	if(num11 == num1){
	num11 = Math.round(num11 + 1);
	}
	else if (num11 == num2){
	num11 = Math.round(num11 + 1);
	}
	else if(num11 == num3){
	num11 = Math.round(num11 + 1);
	}
	else if(num11 == num4){
	num11 = Math.round(num11 + 1);
	}
	else if (num11 == num5){
	num11 = Math.round(num11 + 1);
	}
	else if(num11 == num6){
	num11 = Math.round(num11 + 1);
	}
	else if (num11 == num7){
	num11 = Math.round(num11 + 1);
	}
	else if (num11 == num8){
	num11 = Math.round(num11 + 1);
	}
	else if(num11 == num9){
	num11 = Math.round(num11 + 1);
	}
	else if(num11 == num10){
	num11 = Math.round(num11 + 1);
	}
	
	
	if(num12 == num1){
	num12 = Math.round(num12 + 1);
	}
	else if (num12 == num2){
	num12 = Math.round(num12 + 1);
	}
	else if(num12 == num3){
	num12 = Math.round(num12 + 1);
	}
	else if(num12 == num4){
	num12 = Math.round(num12 + 1);
	}
	else if (num12 == num5){
	num12 = Math.round(num12 + 1);
	}
	else if(num12 == num6){
	num12 = Math.round(num12 + 1);
	}
	else if (num12 == num7){
	num12 = Math.round(num12 + 1);
	}
	else if (num12 == num8){
	num12 = Math.round(num12 + 1);
	}
	else if(num12 == num9){
	num12 = Math.round(num12 + 1);
	}
	else if (num12 == num10){
	num12 = Math.round(num12 + 1);
	}
	else if(num12 == num11){
	num12 = Math.round(num12 + 1);
	}


	card1.gotoAndStop(num1);
	card2.gotoAndStop(num2);
	card3.gotoAndStop(num3);
	card4.gotoAndStop(num4);
	card5.gotoAndStop(num5);
	card6.gotoAndStop(num6);
	card7.gotoAndStop(num7);
	card8.gotoAndStop(num8);
	card9.gotoAndStop(num9);
	card10.gotoAndStop(num10);
	card11.gotoAndStop(num11);
	card12.gotoAndStop(num12);
	}



the whole code is inside a flash button hope you could help me

the only problem is that when I click the button with that code in it the cards will shuffle and show what card it is but sometimes around 4 out of 10 tries 2 or 3 cards end up being the same.

This post has been edited by Novice_panda: 14 January 2010 - 09:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Flash card game shuffle problem

#2 4D1  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 225
  • Joined: 22-October 09

Re: Flash card game shuffle problem

Posted 19 January 2010 - 02:41 PM

If you used an array instead of num1-12 and a for loop it would get rid of alot of code and make it easier to error check. Also with your check on num12 for example you are saying:

if(num12==num1)
{
/*change num12*/
}

//But what if num12 is changed to something that is then 
//equal to a number before it, you need to use some loops 

As an example for you:

int num[12];

for(int a=0; a<12; a++)
{
	  num[a]=Math.round(Math.random()*51 + 2);
}

while(int flag!=0)
{

flag=0;

for(int a=0; a<12; a++)
{
	 for(int b=0; b<12; b++)
	 {
		  if(num[a]==num[b])
		  {
			   num[a]=Math.round(num12 + 1);
												   flag++;
		  }
	 }
}
}



Hopefully that will work in Actionscript, I haven't used Action Script before only C++, hopefully you can see how good loops can be, this version will do the same thing as yours but with much less code. This version will also keep checking until it has passed through the array with no changes made.

This post has been edited by 4D1: 19 January 2010 - 03:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 longbar  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 12
  • Joined: 24-November 09

Re: Flash card game shuffle problem

Posted 20 January 2010 - 08:00 AM

I think the essential problem with your logic is this: you're checking whether each card is a duplicate of one other card. But after you adjust a card, you don't go back and check again.

What if we ended up with this:
num2 = 22
num3 = 21
num4 = 30
num5 = 21

So you go through and check -- num 2 and num3 are fine on the first check. Then num4 is fine. Then num5 is the same as num3, so you increment it up to be 22 -- but now it's a duplicate of num2, but you don't go back and check against num2, so now you have a duplicate.

Make sense?

As 4D1 points out, simplifying your code with some loops will make it a lot easier for you to do what you're trying to do. (Although the sample code there isn't ActionScript code, as 4D1 says.)

Have you worked with for loops and/or arrays in your class? If so, give them a try with this code and let us know if you still have problems now that you know where the flaw is in the logic of the code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1