Part 11: Setting up the game board

Initiating a game will require us to have all the potential card and location positions for the playspace defined (for each player) as well as slots for both players.

This is roughly the outcome weโ€™re looking for:

Screenshot 2023 11 13 at 23 14 36

Weโ€™ll also need to track the playerโ€™s hand content, as well as their deck contents, so letโ€™s break this down a bit.

ย 

Setting up the playspace:

We need to set the players up – we have a โ€œGame Instanceโ€ table which has the following fields we can set:

Current turn = letโ€™s zero index for our โ€œpre-launchโ€ turn – we can do any pre-turn operations during this time. ย Iโ€™ll set the default to 0.

Location 1-3 = Letโ€™s shuffle all potential locations and deal them into these slots.

Number of turns = default is 6, but this can change based on cards played.

Current Active Player = weโ€™ll probably do a coin toss and make a reference to a Player Session record.

Player 1 & Player 2 = References to the player session

ย 

Weโ€™ll need the following subflows:

1 – Create the Game Instance Record

2 – Shuffle cards – it should take an input of โ€œTypeโ€ (location or player deck) and if Location, itโ€™ll need to shuffle all active location cards.

3 – Deal Locations – once weโ€™ve shuffled, we should deal out the 3 locations – weโ€™ll create the โ€œLocation instanceโ€ record and return the reference to the Game instance.

4 – Add Players – Allocate the Players and do a coin-toss for who goes first.

ย 

The Game Instance flow was easy – create record from a matchmaking instance. ย I also added a reference to the matchmaking record just so we have tracablilty when debugging and I set the game instance as the output of this subflow (so other subflows can use that)

The Shuffle Flow was a little interestingโ€ฆ I want to set up a type so I can shuffle different classes of cards. ย The Location choice is based on the full card setโ€ฆ but the Player Deck comes from the Player Deck Card records, and the Live Player Deck comes from the Card Instances that are โ€œIn Deckโ€ status.

Screenshot 2023 11 13 at 18 32 08

This means that the operation to create or update records will be different for each. ย I probably actually need 2 subflows – one which is a generic โ€œshuffle an array of numbersโ€ and another which is the โ€œbuild an array of numbersโ€. ย Getting the Array shouldnโ€™t be TOO intensive – the Player deck has a maximum of 12 cards, and the Locations are a few dozen – maybe no more than 60-70 location variations. ย I donโ€™t have a huge problem with that size of database operation.

Letโ€™s do the array shuffle flow firstโ€ฆ. we might need to do some code here somewhere. ย I donโ€™t think any of the math transforms or array transforms in Flow Designer can handle a randomiser or anything like that, so I think weโ€™ll have to resort to some code to shuffle our array of items. ย GIven weโ€™re writing code anyway, maybe we should only return x number of IDs (corresponding with how many cards we want to โ€œdealโ€). Iโ€™ll create a Flow Action for this:

Screenshot 2023 11 14 at 00 15 41

This constitutes the first lines of code in this entire solution (not counting CSS remember?). ย If I wanted to be really strict, I might have been able to find an API that would shuffle an array for me and call that, but it seems like a lot of effort to avoid these this code, and shuffling is a bit tricky (and also I donโ€™t want to rely too heavily on stuff outside of ServiceNow).

This action is both shuffling and limiting the number of records I need to subsequently look up – this should make the full flow logic a little quicker.

The next thing I need to do is get the array of IDs to send to the Action. ย Unfortunately thereโ€™s no โ€œarrayโ€ Flow Variable type (yet..?) so thatโ€™s why my action will work with JSON. ย What Iโ€™m going to do instead is use a String Flow Variable and construct the JSON of the IDs using another Subflow:

Screenshot 2023 11 14 at 00 23 24

This subflow looks up all the active Locations (this gives us a record count as well as a record, so we can use that sneaky iterator flow variable to check whether weโ€™re at the end of the objects to close our JSON string)

We start the JSON with an array [, and then we iterate through all our records adding {โ€œID:<the id field>}.

Screenshot 2023 11 14 at 00 26 43 2x

When we get to the end of the record list (and our iterator matches) then we close the JSON string with a ].

This isnโ€™t really โ€˜codeโ€™ but weโ€™re getting the flow to write code, so I donโ€™t think it counts :).

At this point weโ€™ve got the records coming back with a JSON array of IDs, and weโ€™ve got an action that can return a limited list of shuffled IDs. ย We can use this list to do a second Look up record based on the IDs we want to return and stitch it all together in a master subflow:

Screenshot 2023 11 14 at 00 28 28 2x

ย 

That gives us shuffled locations with a limit. ย Thatโ€™ll do for tonight. ย We can add our Location Instance record creator next, then start looking at our Player card shuffle and deal system.

ย 

Hereโ€™s roughly what that might entail:

ย Pre-loading the deck/hand:

Instantiate a copy of the playerโ€™s deck as an โ€œunshuffledโ€ deck so we know what cards weโ€™ve got.

Some cards have an ability that happens prior to shuffle (such as automatically adding the card to the playerโ€™s hand), so weโ€™ll first need to search for those and execute the appropriate flows. ย Once these treatments have occur then, letโ€™s shuffle the remaining cards and deal the first hand of three cards.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *