How to check if array has elements that can be in the same order has another array

Godot Version

4.2.2

Question

Hello!
Let’s say I have an array:
numberSort = [1, 2, 3, 4, 5, 6, 7, 8, 9, “T”, “J”, “Q”, “K”, “A”]
And I want to check if the elements of an array can be in the same order is that but not necessarily are.
Like if we had an array [7, 9, 8, J, T] that would return true.
Can anybody help me here?

I have an idea!
convert array to string and use in:

var numberSort = [1, 2, 3, 4, 5, 6, 7, 8, 9, “T”, “J”, “Q”, “K”, “A”]
var array = [7, 9, 8, "J", "T"]

func _ready():
  print(Check(numberSort, array))

func Check(array1, array2):
  var SortStr = str(array1)
  var ArrayStr = str(array2)
if ArrayStr in SortStr:
  return true

I haven’t tried it, maybe it works.

Asking for clarification, should one of the two arrays follow the ordering of another? Or should it count as long as they somewhat contain the same values within both of their arrays? (Or possibly both of my questions combined?)

If you ask me, I would have made use of the Array.all() method which returns true if all elements within the array return true upon checking.

In practice, it would go like this

var result: bool = array.all(func(element): return numberSort.find(element) != -1)
if result == true:
    # all elements in array can be found in numberSort
else:
    # there are some elements in array that cannot be found in numberSort

Referencing:

I’m having an issue here, you see, I made all the cards be something along the lines of “J_H” for Jack Of Hearts or “3_C” for 3 of Clubs, I’m having trouble figuring out where to use the “.begins_with” on this script.
Any help is greatly appreciated.

I’m having trouble figuring out where to use the “.begins_with” on this script.

If you need to check for something, you would be required to go through a loop and then check the cards one by one.
Something like this:

for card in array:
    if card is String and card.begins_with("SomePattern"):
        # I fulfill the conditions

Or do you want to filter out all cards of your hand for those who begin with a specific string?

The option to filter out all the cards of the hand that begin with a specific string

var all_cards = ["text", "card", "test", "load", "post", "top"]

func use_filter():
  print(filter_cards(all_cards, "t")) #-> ["text", "test", "top"]

func filter_cards(all, filter):
  var filtered_cards = []
  for i in all:
    if i.begins_with(filter):
      filtered_cards.append(i)
  return filtered_cards

If you want to remove filtered cards:

var all_cards = ["text", "card", "test", "load", "post", "top"]

func use_filter():
  all_cards = filter_cards(all_cards, "t")) #all_cards = ["card", "load", "post"]

func filter_cards(all, filter):
  var filtered_cards = []
  for i in all:
    if not i.begins_with(filter):
      filtered_cards.append(i)
  return filtered_cards

If you want to keep only filtered cards:

var all_cards = ["text", "card", "test", "load", "post", "top"]

func use_filter():
  all_cards = filter_cards(all_cards, "t")) #all_cards = ["text", "test", "top"]

func filter_cards(all, filter):
  var filtered_cards = []
  for i in all:
    if i.begins_with(filter):
      filtered_cards.append(i)
  return filtered_cards

i feel like people are misunderstanding something or I’M the one misunderstanding things, but i want to filter the first number/letter of every element in the array and see if that fits within numberSort

#have an array and a filtered_array that only has elements that begins with “J”
var array = [7, 9, 8, “J_b”, “J_a”]
var filtered_array = array.filter(func(element): return element is String and element.begins_with(“J”))
print(filtered_array)

tbh the topic started rather lightweight with a simple array filter operation but right now I just want to know what kind of game you want to make and the context that requires you to filter out the alphabetical cards while discarding all numbered ones. We are kinda thumbling in the dark :sweat_smile:

In order to not confuse you all any longer, I will leave it to the others

its meant to be a simple custom variant of poker, my goal here was to program straights lol, i just have it so that the array that contains all the cards is formated such as (Rank) + _ + (Suit) so a full deck would be:

Global.cardsInDeck = [
	"2_H", "3_H", "4_H", "5_H", "6_H", "7_H", "8_H", "9_H", "T_H", "J_H", "Q_H", "K_H", "A_H",
	"2_S", "3_S", "4_S", "5_S", "6_S", "7_S", "8_S", "9_S", "T_S", "J_S", "Q_S", "K_S", "A_S",
	"2_D", "3_D", "4_D", "5_D", "6_D", "7_D", "8_D", "9_D", "T_D", "J_D", "Q_D", "K_D", "A_D",
	"2_C", "3_C", "4_C", "5_C", "6_C", "7_C", "8_C", "9_C", "T_C", "J_C", "Q_C", "K_C", "A_C"
	]

I need the number sort so i can make it so Tens (T), Jacks (J), Queens (Q), Kings (K) and Aces (A) work

For the hand the person is playing I have another array: Global.currentSelectedCards

Im trying to get check if this array has a hand like [2_H, 3_C, 4_S, 5_D, 6_C] so I can mark it true and use an If statement to determine that that hand is in fact a Straight.

Now we have enough explanations!

var sort: Array = ["2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"]

func check_straight(array):
	var numbers = []
	for i in array:
		numbers.append(i.erase(1,2))
	var numbers_str = str(numbers)
	var sort_str = str(sort)
	if numbers_str in sort_str:
		return true
	else:
		return false

I think this function is useful for you, you can use it like this:

if check_straight(%your_array%):
	#If it is straight, it is executed

OMG YES!! This is almost there!! I just need it to also work when the cards are out of order, like a [3_C, 2_H, 5_D, 4_S, 6_C] should also work!

Here it is!!

func sorter(a, b):
	if sort.find(a) < sort.find(b):
		return true
	return false

func check_straight(array):
	var numbers = []
	for i in array:
		numbers.append(i.erase(1,2))
	numbers.sort()
	numbers.sort_custom(sorter)
	var numbers_str = str(numbers)
	var sort_str = str(sort)
	numbers_str = numbers_str.erase(numbers_str.length() - 1)
	numbers_str = numbers_str.erase(0)
	if numbers_str in sort_str:
		return true
	else:
		return false

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.