Your cart is currently empty!
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:
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.
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:
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:
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>}.
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:
ย
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.
Leave a Reply