Topic was automatically imported from the old Question2Answer platform.

Asked By

Ombre

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])

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.

Hi, so I should have said it but it’s a Baten Kaitos combat system clone, so the cards have to be sent in order which is why I wanted to check for n+1 and n-1. I solved it by having two ifs, one for n+1 and one for n-1 and a boolean to make sure it doesn’t check twice on pair.