# How to check if there's an entry in an array from its index number without stopping execution ?

Attention Topic was automatically imported from the old Question2Answer platform.

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

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.

``````if (n+1 < number_selected.size()) :
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]):
pair_number += 1
pair.append(int(number_selected[n]))
number_selected_toggle = true
else:
number_selected_toggle = false
``````

and

``````if n>=0:
if (number_selected[n-1] == number_selected[n]) and number_selected_toggle == false:
pair_number += 1
pair.append(int(number_selected[n]))
number_selected_toggle = true
pair_verification.append(true)
else:
number_selected_toggle = false
pair_verification.append(false)
``````

I don’t know if it’s right but it’s working and I thought since I use ask godot a lot when I’m writing code, that I should give the answer here.