0 votes

Hi, so I'm making a card game in godot. I want to check for suites and pairs, and basically my code for checking kind of works, but since I'm checking values in a for loop of n+1 and n-1, I need to make sure there's something in the array for n-1 or n+1 and if there's nothing, then do nothing. So I tried with :

number_selected[n+1] != null

but it stops the game, because it's looking for something that doesn't exist in the if function. Is there a function that does that? I tried with back() but no luck since it's the n+1 value I'm looking for. Here's my code :

    for n in range (number_selected.size()) :
    if number_selected[n+1] != null and n >= 0:
        print(number_selected)
        if(number_selected[n] == str(int(number_selected[n+1])-int('1'))):
            suite_up.append(true)
        else:
            suite_up.append(false)

        if(number_selected[n] == str(int(number_selected[n+1])+int('1'))):
            suite_down.append(true)
        else:
            suite_down.append(false)
        if(number_selected[n] == number_selected[n+1]) or (number_selected[n-1] == number_selected[n]):

            pair_number.append(number_selected[n])

Thanks!!

(I also tried with

if n+1 < number_selected.size() and n >= 0:

but it skips the last entry...)

Godot version 3.5.1
in Engine by (17 points)

1 Answer

+1 vote

I don't understand why you are checking n-1. It isn't necessary.
Separate suit checks from pairs checks. If this is poker you should have a hand checking function for each of the poker hands all the way from royal flush down to high card.

func get_flush(): 
   ...
func get_pair(): 
   ...

(If you check poker hands in order from best to worst you can simplify the checks greatly.)
In any case
I assume you sort the array first (otherwise the problem becomes exponentially more difficult).

Say the hand is A 5 6 A 2
After sorting it is 2 5 6 A A
Change your for loop to

for n in range (number_selected.size() - 1) :   

So that you are looping from element 0 to the second last element.
Now forward checks (n + 1) will find the pair .
For suits you will again sort the hand but this time on suits.
For example:
H S H C D becomes:
C D H H S
Then again looping and forward checking finds the matching suits.

by (372 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.