Trying to make a way to check certain conditions for a win

I have a pseudo code layout of what I am aiming for, but I have no idea how to actually code it with making each condition ridiculously long.
if(3 match)
{
win back bet
}
else if(4 in a row of increasing value)
{
win back bet + 50%
}
else if(3 match && 2 match)
{
win 2X bet
}
else if(4 match)
{
win 4X bet
}
else if(5 in a row of increasing value)
{
win 5X bet
}
else if(5 match)
{
win Jackpot
}
else
{
win nothing
}

Full project here, this will hopefully help clear up things if pseudo code is confusing.
https://playcanvas.com/editor/scene/581391

One option could be to implement each condition inside a function that will return true/false.
Then, if you also want to avoid a long if/else list, you could assign all the functions to an array and loop on it until one function returns true.

This is kind of what I had in mind, I’m just not entirely sure how to check if, for example, 3 of 5 objects return the same value without going into the long if/else statements. The only way I can think of off the top of my head is basically: if object1 = object2 =object3 or object2 = object3 = object4, etc.and this is what I would prefer to avoid because then the code gets obnoxious and hard to read.

Oh, I understand now.
I would probably store the results inside an array, sort them, and then count the values that repeat:

var results = [1, 3, 2, 3, 4, 5];
var bestCount = 0;
var bestValue = null;
var currentCount = 0;
var currentValue = null;
var i;

results.sort();
for (i = 0; i < results.length; i++) {
    if (results[i] !== currentValue) {
      currentCount = 1;
      currentValue = results[i];
   } else {
      currentCount++;  
   }
   if (currentCount > bestCount) {
      bestCount = currentCount;
      bestValue = currentValue;
   }
}

// bestCount will have the best longest set of the same bestValue.

Of course, if you also want to count if there are combinations of 2 + 3 numbers or 2 + 2 is a bit more complex.
A simpler approach could be to have the code above inside a function that accepts a number to exclude, and you repeat the research more time. So the first time you get that there is a triplet of 4, on next research you exclude the number 4, and you could get that there is also a couple of 2.

Let me know if you can get it working :slight_smile:

Sorry for my misunderstanding here, this is the only line I don’t quite get. Are you naming the function sort or is sort a separate function?

is a method of Array class:

I think I found a way to incorporate the “full house” (3+2) condition.
just add two variables:

var secondCount = 0;
var secondValue = null;

and set them to equal the current best count and value before setting the best count and value to the current count and value:

if(currentCount > bestCount)
        {
            secondCount = bestCount;
            secondValue = bestValue;
            bestCount = currentCount;
            bestValue = currentValue;
        }

Then, compare:

if(bestCount = 3 && secondCount = 2)
{
  win 2X bet
}

Mmhh… I don’t think it will work. It will give you a 3+2 also when ther is just a set of 3, because it will increase the count for the same numbers.

Right, my bad. After I posted that code I made a change to it that should take care of that issue.

if(results[i] !== currentValue)
        {
            currentCount = 1;
            currentValue = results[i];
        }
        else
        {
            currentCount++;
        }
        if(currentCount > bestCount)
        {
            if(bestCount !== 0)
            {
                secondCount = bestCount;
                secondValue = bestValue;
            }
            bestCount = currentCount;
            bestValue = currentValue;
        }

Unless I’m reading it wrong, the way this should work it should check each dice for its value (1-6), then it will check how many dice equal a certain value. So lets say I rolled two 4’s, and three 6’s. The code, in theory, should count up the fours first, then start the sixes. Once the count for sixes exceeds 2, the fours become second count and value, and the sixes become best count and value.

Yes, should work.
Now your next exercise will be to search for a sequence of increasing values :slight_smile:
I think should be easy. Just increase the counter when the new value is equal to previous value + 1.

eeeeeeeeeeeeeeeehhhhhhhhhhhhhhhhhhhhhhhh[quote=“Ryan, post:3, topic:5849, full:true”]

This is kind of what I had in mind, I’m just not entirely sure how to check if, for example, 3 of 5 objects return the same value without going into the long if/else statements. The only way I can think of off the top of my head is basically: if object1 = object2 =object3 or object2 = object3 = object4, etc.and this is what I would prefer to avoid because then the code gets obnoxious and hard to read.
[/quote]

:slightly_frowning_face::expressionless::expressionless::expressionless:hhhhuu3y83

Good news is, this works. Now I just have to figure out how to get the straights conditions to work.

Just had an idea to check for the straights win conditions. Is there a way I can check if the results array contains the numbers required to trigger that condition?
Something like:

If(array contains 1, 2, 3, 4 or 2, 3, 4, 5 or etc.)

Forgetting about my “if array contains” idea, I gave something like this a try and it works like a charm. Here’s a link to the full code for checking the win, plus the script for cashing out based on the result.
Check result: https://playcanvas.com/editor/code/529780?tabs=10964296,10861152,10827801,10800992,10830385
Cashout: https://playcanvas.com/editor/code/529780?tabs=10964296,10861152,10827801,10800992,10830385

Cool. I’m happy it worked :slight_smile:

Pretty well, although the overall code is having a weird looping issue (made another post about it Certain parts of script being called 6 times) and it’s gotten me completely stuck.

I did just find a small bug with this method, it seems if you roll something like 1,2,4,5,6 it will return a small straight when it shouldn’t.

var results = [1, 2, 1, 4, 5, 6];
var bestCount = 0;
var bestValue = null;
var currentCount = 0;
var currentValue = null;
var i;

results.sort();
for (i = 0; i < results.length; i++) {
    if (currentValue === null || results[i] != currentValue + 1) {
      currentCount = 1;
   } else {
      currentCount++; 
   }
   currentValue = results[i];
   if (currentCount > bestCount) {
      bestCount = currentCount;
      bestValue = currentValue;
   }
}

console.log(bestCount);
console.log(bestValue);

This returns 3 as bestCount and 6 as bestValue.

This is how I managed to solve it. It includes code to check for the straight conditions, and gets around counting a straight when there isn’t one.

CheckRoll.prototype.getResults = function()
{
    //Uncomment next line and change values to test different win conditions
    //this.results = [1,2,3,4,5];
    
    //Sort results from lowest to highest
    this.results.sort();
    console.log("Results: " + this.results);
    for(i=0;i<this.results.length;i++)
    {
        if(i >= 1)
        {
            j = i - 1;
        }
        else
        {
            j = null;
        }
        //Check if the current result is the same as the last result
        if(this.results[i] !== this.currentValue)
        {
            //Make the current result the last result if it isn't
            //and start counting how many results match this one
            this.currentCount = 1;
            this.currentValue = this.results[i];
        }
        else
        {
            //Increment count if current result is same as last result
            this.currentCount++;
        }
        
        if(this.results[j] === this.results[i] - 1)
        {
            this.straightCount++;
            console.log("Result= " + this.results[i] + " Straight= " + this.straightCount);
        }
        //Minor fix here. If results = 1,2,3,4,6, straightCount = 1 before prize check and would count as loss
        else if(this.results[j] !== this.results[i] && this.straightCount < 4 || this.straightCount > 5) 
        {
            this.straightCount = 1;       
        }
        
        if(this.currentCount > this.bestCount)
        {
            if(this.currentValue !== this.bestValue)
            {
                //Set the previous best count and value to second place for later use
                this.secondCount = this.bestCount;
                this.secondValue = this.bestValue;
            }
            //Set the current best count and value
            this.bestCount = this.currentCount;
            this.bestValue = this.currentValue;
        }
        //If best value is lower than current value,
        //make it second value, and its count second count
        else if(this.currentValue > this.bestValue)
        {
            this.secondCount = this.currentCount;
            this.secondValue = this.currentValue;
        }
    }
    this.manager.script.winCheck.payTable();
};

Still can’t figure out why this gets run 6 times on the initial start call though.

Found it. For some reason on the initial start call only, this function gets run 6 times:

CheckRoll.prototype.check = function()
{
    //Makes sure each dice has stopped by comparing its current position
    //to its last position
    for(i=0;i<dice.length;i++)
    {
        if(dice[i].stopped !== true)
        {
            var diePos = dice[i].getLocalPosition();
        
            currPos = new pc.Vec3(this.toDecimal(diePos.x), this.toDecimal(diePos.y), this.toDecimal(diePos.z));
            if(currPos.x === lastPos.x && currPos.y === lastPos.y && currPos.z === lastPos.z)
            {
                dice[i].stopped = true;
                this.stopped = true;
            }
            else
            {
                lastPos = currPos;
                this.check();
            }
        }
    }
    this.stopCheck();
};

I understand it has something to do with calling check() in the else statement, but the overall function doesn’t work at all if it’s not there.