I just finished redoing my entire poker draw using this tutorial as a baseline and now I just need the script evaluations to determine hand values to match against. Any ideas on how I should code this?
MV [RESOLVED] Event scripts for poker hand calculations
Javascript/Plugin SupportDo you mean how you'd do the part that goes "These cards form this hand"?
Yeah, like if you have two pair or flush or whatever.
The way the tutorial is storing the cards, I'd use .split(-) to get an array of the suit and the value.
I'll have to have a think about how I'd approach poker hand logic. There are some that would be fairly trivial if you were storing cards the way I outlined in the tutorial post, but this way it's a bit more complicated. I'll come back to it.
Okay, so first things first (assuming the same variable IDs from the tutorial, adjust as needed):
Royal Flush:
Straight flush:
Four of a kind:
Full house:
Flush:
Straight:
Three of a kind:
Two pair:
Pair:
I'll have to have a think about how I'd approach poker hand logic. There are some that would be fairly trivial if you were storing cards the way I outlined in the tutorial post, but this way it's a bit more complicated. I'll come back to it.
Okay, so first things first (assuming the same variable IDs from the tutorial, adjust as needed):
JavaScript:
var splitHand = $gameVariables.value(9).map(card => card.split("-"));
var suits = splitHand.map(card => card[0]);
var values = splitHand.map(card => Number(card[1])).sort((a, b) => a - b);
var counts = {};
for (var num of values) {
counts[num] = counts[num] ? counts[num] + 1 : 1;
}
Royal Flush:
JavaScript:
values.includes(1) && values.includes(10) && values.includes(11) && values.includes(12) && values.includes(13) && suits.every(suit => suit === suits[0])
Straight flush:
JavaScript:
suits.every(suit => suit === suits[0]) && values.every((value, index) => index === 0 || value === values[index - 1] + 1)
Four of a kind:
JavaScript:
Object.values(counts).some(count => count === 4)
Full house:
JavaScript:
Object.values(counts).some(count => count === 3) && Object.values(counts).some(count => count === 2)
Flush:
JavaScript:
suits.every(suit => suit === suits[0])
Straight:
JavaScript:
values.every((value, index) => index === 0 || value === values[index - 1] + 1)
Three of a kind:
JavaScript:
Object.values(counts).some(count => count === 3)
Two pair:
JavaScript:
Object.values(counts).filter(count => count === 2).length === 2
Pair:
JavaScript:
Object.values(counts).some(count => count === 2)
I think that'll do it! Let me run everything and I'll confirm.
EDIT: Good to go. All I need now is to set the casino logic to verify against the available tokens and I can retire the old version. Thanks again!
EDIT 2: And here's the completed script (put it in main.js and change the variable numbers to match your setup):
Then when you have the hand decided, call it from a script eval like this:
I'll have a full tutorial later this weekend.
EDIT: Good to go. All I need now is to set the casino logic to verify against the available tokens and I can retire the old version. Thanks again!
EDIT 2: And here's the completed script (put it in main.js and change the variable numbers to match your setup):
JavaScript:
cardEval = function () {
var splitHand = $gameVariables.value(240).map(card => card.split("-"));
var suits = splitHand.map(card => card[0]);
var values = splitHand.map(card => Number(card[1])).sort((a, b) => a - b);
var counts = {};
for (var num of values) {
counts[num] = counts[num] ? counts[num] + 1 : 1; }
if (values.includes(1) && values.includes(10) && values.includes(11) && values.includes(12) && values.includes(13) && suits.every(suit => suit === suits[0])) {
$gameVariables.setValue(229, "Royal Flush!");
$gameVariables.setValue(228, 649739); }
else if (suits.every(suit => suit === suits[0]) && values.every((value, index) => index === 0 || value === values[index - 1] + 1)) {
$gameVariables.setValue(229, "Straight flush!");
$gameVariables.setValue(228, 72192); }
else if (Object.values(counts).some(count => count === 4)) {
$gameVariables.setValue(229, "Four of a kind!");
$gameVariables.setValue(228, 4164); }
else if (Object.values(counts).some(count => count === 3) && Object.values(counts).some(count => count === 2)) {
$gameVariables.setValue(229, "Full house!");
$gameVariables.setValue(228, 693); }
else if (suits.every(suit => suit === suits[0])) {
$gameVariables.setValue(229, "Flush!");
$gameVariables.setValue(228, 508); }
else if (values.every((value, index) => index === 0 || value === values[index - 1] + 1)) {
$gameVariables.setValue(229, "Straight!");
$gameVariables.setValue(228, 254); }
else if (Object.values(counts).some(count => count === 3)) {
$gameVariables.setValue(229, "Three of a kind!");
$gameVariables.setValue(228, 46); }
else if (Object.values(counts).filter(count => count === 2).length === 2) {
$gameVariables.setValue(229, "Two pair!");
$gameVariables.setValue(228, 20); }
else if (Object.values(counts).some(count => count === 2)) {
$gameVariables.setValue(229, "Pair!");
$gameVariables.setValue(228, 2); }
else {
$gameVariables.setValue(229, "Ouch.");
$gameVariables.setValue(228, 0); }
if ($gameVariables.value(228) != 0) {
$gameVariables.setValue(227, $gameVariables.value(228) * $gameVariables.value(225));
$gameVariables.setValue(226, "You won " + $gameVariables.value(228) + " tokens.");
} else {
$gameVariables.setValue(226, "Better luck next time!"); }
};
Then when you have the hand decided, call it from a script eval like this:
JavaScript:
cardEval();
I'll have a full tutorial later this weekend.
This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.
Original thread: https://forums.rpgmakerweb.com/threads/resolved-event-scripts-for-poker-hand-calculations.141626/