A Flash Developer Resource Site

# Thread: Coding problem is an array the way?

1. ## Coding problem is an array the way?

I'm not sure how to do this.

How can I set an array and push "RED" into it and when it get's to 'x' amount of RED's say RED,RED it traces something but if it's interupted and it goes RED,RED,BLACK it doesn't.

It's a casino betting sytem basically once there is 7 RED straight I want to make a bet \$5 on Black and if the RED comes out again then double the bet to \$10 etc on and on but once it wins then change it back until another straight seven off any color comes out straight again.

I have the random number selecting 0 through to 36 and a ZERO what it does is trace("RED"); or trace("BLACK");

I'm so far thinking if I put something like this in there? But i haven't got anywhere near the making betting code yet. Any help would be great for a start. It's only to simulate making the bets using this betting theory to see the results it's not to make a game just testing the code so that it runs and traces results money count etc for a few hundred spins or enter frames in this case
.

display = new Array();
display.push("RED");
if(display.Array == RED,RED){
trace("two reds");
}

2. Put zip and fla up here!

http://www.jaffasoft.com/cassino.zip

3. Sorry this is messy, but i just hashed it out quick and it seems to work. This is a function which will count how many consecutive reds you have in an array.

Code:
```myArray=new Array("red", "red", "red", "red", "red","black", "red", "black");

function countConsecutiveReds(theArray){
reds=0;
maxReds=0;
for(i=0; i<theArray.length; i++){
if(theArray[i]=="red"){
reds++;
if(reds>maxReds){
maxReds=reds;
}
} else {
reds=0;
}
}
return maxReds;
}

foo=countConsecutiveReds(myArray);
trace(foo);```
In this case it will trace 5

4. Originally Posted by richblend
Sorry this is messy
That's alright. That was great thanks.

I'm hacking a way at this too. Not finished yet but got a lot further using your code. I have updated the zip in that link in above post.

I'm getting a bit stuck on how to do the betting now to keep track of it.

Please offer any suggestions if you get time ( rem its a work in progress) so things won't look right in the fla quiet yet!

5. Can this be coded so that it only makes a cosecutive count (beggining with the first in the array).

For example like above:
//this would trace 5
myArray=new Array("red", "red", "red", "red", "red","black", "red", "black");

//but now when I unshift("black"); into the myArray (which is effectively a new random roll!!) and the last "black" is pop("black"); off the end of the myArray. Now I would need to trace nothing!
myArray=new Array("black,"red", "red", "red", "red", "red","black", "red");

I only want it to trace when there are consecutive runs of either color 'red' or 'black' from the beggining in the array.

6. the quickest way is just to slightly change the function call....

Code:
```if(myArray[0]=="red"){
foo=countConsecutiveReds(myArray);
trace(foo);
}```

7. //that works fine for say this one
myArray=new Array("red", "red", "red", "red", "red","black", "red", "black");

//and this one
myArray=new Array("black,"red", "red", "red", "red", "red","black", "red");

//if the array comes out as this it traces 5 still??
myArray=new Array("red","black,"red", "red", "red", "red", "red","black");

8. Here, let me give this a shot, I don't know if this is what you want, I wasn't able to open the flash file, even though I have MX 2004. So basically, you just want it to return the longest consecutive chain or reds, correct?

Here's my try, exscuse the while loops. I belive it does everything you wanted it too, and I tested it and it behaves correctly for what I was making it do.

Here's my function

Code:
```function countReds(anArray)
{
var longestChain
var tempChain
var a
tempChain = 0
longestChain = 0
a = 0
//Sorry, you'll have to exscuse the while loops, I'm yet to break the habit.

//First we'll check every element in the array to see if it's either red or black.
while(a < anArray.length)
{
if(anArray[a] == "red")
{
tempChain++
//Add one to our temp chain
}
if(anArray[a] == "black")
{
//We've broken the pattern
if(tempChain >= longestChain)
{
longestChain = tempChain
//If it's longer then our current record setter, then replace the current one.
}
tempChain = 0
//We broke the chain so we're going to set it back to zero.
}
a++
}
trace("the longest chain or reds is " + longestChain)
}```

9. That's a pretty wild go at it .

Actually that does what the first function did.

What it needs to do is count the most of one color, we'll stick to the example of "reds" because black doesn't matter yet I can change it to do black once we know how to do "reds" first!

So if this array was tested using the function it should see that there is only one 'red' in the beggining of the array so if it was going to trace anything it should trace 1.

I will give you four examples of what the function should trace.

//should trace 1 becasue there is only one consecutive red from the beggining of the array. The first 'reds' have already been counted because they have now been broken by a black being rolled.
myArray=new Array("red","black,"red", "red", "red", "red", "red","black");

//this should trace 3 because there are now three reds straight drawn
notice there is actually four 'reds' in this now but it only needs to find the longest consecutive reds from the beggining of the array from the left side of the array.
myArray=new Array("red","red","red", "black", "red", "red", "red","red");

//this should trace 5 because there are now five reds straight drawn
myArray=new Array("red","red","red", "red", "red", "black", "red","black");

//this should trace 0 or nothing because there are no reds drawn from the left hand side of the array
myArray=new Array("black","red","red","red", "red", "red", "red", "red","red");

10. Deleted:

11. Hi
If i understand you correctly, you need to keep track of any consecutive colors added to a list until there chain is broken by a different addition?
Maybe you can use this example . . . i've included a FLA
code:
```
//variables acting for random roll
randNum = 0;
count = 0;
randColor = "";
//create array to hold colors chosen randomly
display = new Array();
//
myButton.onPress = function() {
count++;
randNum = Math.floor(Math.random(0)*2);
roll();
display.push(randColor);
displayArray.text = display;
countConsecutiveReds(display);
countConsecutiveBlacks(display);
//Once there are 7 colors added to the array  . . .
//the first entry in the array (left hand side) gets removed
//each time you click the button
if (count>7) {
display.shift();
}
trace(display);
showOutcome();
};
//use variables to act as string
roll = function () {
if (randNum == 0) {
randColor = "black";
} else if (randNum == 1) {
randColor = "red";
}
};
// see if there are consecutive REDS
function countConsecutiveReds(display) {
reds = 0;
maxReds = 0;
for (i=0; i<display.length; i++) {
if (display[i] == "red") {
reds++;
if (reds>maxReds) {
maxReds = reds;
}
} else {
reds = 0;
}
}
return maxReds;
}
//// see if there are consecutive BLACKS
function countConsecutiveBlacks(display) {
blacks = 0;
maxBlacks = 0;
for (i=0; i<display.length; i++) {
if (display[i] == "black") {
blacks++;
if (blacks>maxBlacks) {
maxBlacks = blacks;
}
} else {
blacks = 0;
}
}
return maxBlacks;
}
showOutcome = function () {
trace("CONSECUTIVE BLACKS ="+blacks);
trace("CONSECUTIVE REDS ="+reds);
//do something
};
```

Here is accompanying FLA to demonstrate :

12. You've understood it correctly well done.

That appears to be right...in fact it...is right I know it is .

Thanks for going to the trouble to make that fla up.

Thanks to those that helped get us there.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap