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
}
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.
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.
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
I think should be easy. Just increase the counter when the new value is equal to previous value + 1.
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]
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: PlayCanvas | HTML5 Game Engine
Cashout: PlayCanvas | HTML5 Game Engine
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.
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.