#Poker/Card Game Logic Help

1 messages · Page 1 of 1 (latest)

unkempt sphinx
#

Hello, im pretty new to Godot (2nd project) and Im trying to make a balatro type game but with farkle which is a dice game and it uses similar combos to poker like straights, 3 of a kind, 4 of a kind etc. I thought this wouldn't be too difficult as its just dice but I underestimated how complicated (at least in my opinion) poker type game logic would be to create. I feel like this topic isn't talked upon much because when searching for a tutorial I can't find anything and I tried to use Chat GPT to help me which just led to more problems and confusion. If anybody could lead me to a tutorial or lead me in the right direction I would be very thankful!

wicked gulch
#

Balatro mentioned

#

Anyway

#

I think the the easiest to understand way of detecting hands is to just iterate over every card in the hand and compare it to a list of possible hands

#

But that's definitely not the most efficient way

#

I think a way to make it take less comparisons is to sort the cards by rank

#

That way for a straight, you can start at the beginning of the array and look for lower ranks

#

And you don't have to worry about there being higher ranks

#

Uhh what other hands does farkle have?

unkempt sphinx
#

depends some people include different combos but the ones im planning to include are straights, 3 pairs, 2 three of a kinds. Also in farkle you can play ones and fives with any combo or just alone for standard points.

wicked gulch
#

Okay I found the Wikipedia article on farkle

#

Okay so if you take the dice array and sort it in ascending order, then I think you can get all the combos after one iteration

#

So every one and five is its own combo

unkempt sphinx
wicked gulch
#

For the three of a kind maybe it's easier if I just write out some code

#

brb

unkempt sphinx
#

also this isn't necessary and I could try to figure this out on my own later as long as I get the fundamentals down first but in this game called kingdom come deliverence 2 you can play farkle and the 3 of a kind combo multiplys with each additional value afterwards. I wanted to implement this too but it isn't urgent.

#

I do have this right now in a script called score manager and it adds and erases the scores to an array when the dice are selected I just dont have a function to calculate the score yet

wicked gulch
#

okay so here's my logic for detecting three of a kind

#
    var dice := [1, 1, 1, 3, 4, 5]
    dice.sort()
    var combos := []
    var current_combo_count := 0
    var current_combo_number := 0
    for die in dice:
        if current_combo_number == 0 or not die == current_combo_number:
            current_combo_number = die
            current_combo_count = 1
        elif die == current_combo_number:
            current_combo_count += 1
            if current_combo_count == 3:
                combos.append("three of a kind %s" % die)
#

detecting straights would be similar

#

except you only increment the combo count if the current die is one greater than the previous die

unkempt sphinx
#

I see

wicked gulch
#

maybe there's a more visually pleasing way to write the code? let me see

unkempt sphinx
wicked gulch
#

okay i think this looks better (it also includes straights)

#
extends Node

func _ready() -> void:
    var dice := [1, 2, 2, 3, 4, 5]
    dice.sort()
    var combos := []
    var previous_die := 0
    var combo_count := 1
    var straight_count := 1
    for die in dice:
        if die == previous_die:
            combo_count += 1
            if combo_count == 3:
                combos.append("three of a kind %s" % die)
        elif die - 1 == previous_die:
            combo_count = 1
            straight_count += 1
            if straight_count == 5:
                combos.append("straight five")
        else:
            combo_count = 1
            straight_count = 1
        previous_die = die
    print(combos)
unkempt sphinx
#

okay thank you very much! 😊

#

I managed to use the array I made previously with the script you made and it detected the three of a kind which is great progress

wicked gulch
#

yay

unkempt sphinx
# wicked gulch yay

sorry to bother if you could help me with one last thing regarding the ones and fives (totally understand if you don't want to).

wicked gulch
#

ok

unkempt sphinx
#

I added some lines which add the required amount of points if there is also a one or a five selected but say if its a 3 of a kind of ones instead of giving me only 1000 points it gives me 1,300 because it also runs those few lines of code

#

the dice shouldnt be able to be scored if they are in a combo I tried to come up with some solutions but wasn't able to fix the issue

wicked gulch
#

you can use the continue keyword to skip the rest of the code and go to the next iteration of the for loop

#

so like after you append a combo, type continue

#

on the next line