Notifications
Clear all

[Closed] structs as properties of structs within arrays

I’m writing a texas holdem poker simulator in maxscript. (2 things that I’m trying hard to get better at)

This is the first time I’ve used struct and I’ve found myself in a bit of a problem with struct properties that are a part of a struct that in turn belongs to an array.

So I have created a struct called card. This struct has 2 integer properties that are to be assigned, one for suit (1-4) and one for val (1-13)
and 2 derived string properties(derived by functions inside the card struct) for when the card values need to make more sense on screen: suitString=spades, hearts etc. and valString=six,ten,ace, etc.

Then I’ve created a struct called hand which has 7 properties. The first 5 are obviously going to be the cards,
and the next 2 properties are:hand rank (integer values like 9 = royal flush, 8 = straight flush…1= high card etc.) and the hand score (used to determine which hand wins if the hand ranking is equal)
Both of these are to be figured out by the main hand testing functions.

I created an array called cardDeck with copies of 52 card structs. I assigned the correct values to the structs with 2 nested loops to create a whole deck of cards.

I then pick 7 random values between 1 and 52

I then calculate the 21 combinations of 5 card hands from the 7 possible cards to use and store tham in a 2d array.

I then make an array of 21 Hand structs and use the hand combination values to assign the corresponding card structs to the 5 card values of the hand struct.

So now I have an array of 21 hand structs (21 combinations of the 7 cards) that have 5 card structs as properties in each of them. And I have no idea how to access any of the data in them using iterative loops.

I know all the right information is in there cause if I print the combination array, it prints all the data in all levels of the contained structs.

This is also the first time I’ve used multidimentional arrays.

Can anyone help me? I’ve created a monster and I don’t know how to control it.

Cheers,

Cg.

2 Replies

presuming these structs:


Struct card (
  someValue = 5
)

Struct hand (
  cards = #()
)

And one collection array:


-- where each 'hand()' is an instance of the hand struct
-- and each hand struct is filled with instances of the card truct
colArray = #(hand(), hand(), ...)

Then you would access as follows:


colArray[N] -- returns the Nth hand in the collection array
colArray[N].cards -- returns the array of cards in the hand
colArray[N].cards[i] -- returns the Ith card
colArray[N].cards[i].someValue -- returns the value of that card

for curHand in colArray do ( -- loops over all of the hands in the collection array
  for curCard in curHand.cards do ( -- loops over all cards in the current loop's hand
	curCard.someValue -- access its value
  )
)

-- another loop, based on indices instead
-- dumps out each hand's cards and their values
for i = 1 to colArray.count do (
  curHand = colArray[i]
  format "hand #%
" i
  for j = 1 to curHand.cards.count do (
	curCard = curHand.cards[j]
	format "	card #%: %
" j curCard.value
  )
)

Perfect, thank you.

So instead of having 5 different properties in the hand struct, each containing a single card each, I should instead make a single “cards” property containing an array of the 5 cards to make it easy to iterate thru all the cards in the hand. That makes perfect sense now. I really appreciate such a well explained solution.

Cheers,

Cg.