[blockquote]I thought it would be a good idea to start posting up some of the problems I run into while developing the game, along with the solution I manage to eventually come up with. It might come in handy for anyone reading, and at the very least it’s useful for me to write this stuff down for future reference.[/blockquote]

 

I’ve been working on the scripting of the game logic for beginnings and ends of rounds, and I came across a bit of a puzzle. At the end of a round, I need to be able to use the player’s scores (stored in an array) to find the winner of the round. It’s a simple enough task, but the solution took me a surprising amount of effort to arrive at. The solution I came up with was so simple I’m wondering why I didn’t come up with it to begin with.

The problem I ran into arose when I realised that in some cases, the end of a round would result in a tie. My original idea to use the convenient “Max of Int Array” node (added in version 4.3) to look up the owner of the highest score wouldn’t work when there was more than one owner of said highest score. I had to figure out a way to detect a tie situation

After two failed attempts (one involving a bizarre custom “reverse for-loop”) I arrived at a setup which seems to work.

I ended up still making use of the “Max of Int Array” node; my solution begins by saving the result value of this node to a variable, which is then used to compare against the score of each player (in a for each loop). When a player’s score matches the max value we stored in the variable earlier, that player’s player index is stored in a new array. When the loop is finished, the number of entries in the new array is counted. At this point I know for certain that there will be at least one player in the array (there has to be at least one player with the highest score value we found earlier), so a simple test is performed to see if the array length is more than one. If it is, we know that there’s a tiebreaker situation; and if it’s not, then we know that there is a clear winner.

By zero51

Leave a Reply

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