Note: The JavaScript code has been implemented based on the answer provided by ajrwhite. Hopefully, it proves helpful to someone.
Link: http://codepen.io/eMineiro/pen/EKrNBe
Be sure to open the CodePen console to see the examples in action.
When playing poker, player positions are defined in relation to the dealer, like this:
https://i.sstatic.net/QlktT.jpg
Blue: Small Blind and Big Blind positions
Green: Late and Dealer/Late positions
Yellow: Middle positions
Pink: Early positions
Given the following arrays:
players:[1,2,3,4,5,6,7,8,9,10];
positions:["bb","sb","btn","late","medium","medium","medium","early","early","early"];
For instance, "player1" is the "Big Blind", "player2" is the "Small Blind", and so on...
The goal is to sort the players array when changePositions(dealer) is called. For example:
changePosition(10); //indicating that "player10" is now the new Dealer
The resulting arrays should be:
players:[2,1,10,9,8,7,6,5,4,3];
positions:["bb","sb","btn","late","medium","medium","medium","early","early","early"];
If players are eliminated during the game, the "last position" in the positions array needs to be excluded along with the player. Subsequently, changePosition(X) should be called again, with X being the next non-eliminated player to the left of "player10" (current dealer).
For instance, if "player1" is eliminated, the new arrays would be:
players:[2,10,9,8,7,6,5,4,3];
positions:["bb","sb","btn","late","medium","medium","medium","early","early"];
Subsequently, changePosition(X) needs to be called again to determine the new positions. In this scenario, X=2, since "player2" is to the left of the current dealer "player10".
changePosition(2);
The resulting arrays should then be:
players:[4,3,2,10,9,8,7,6,5];
positions:["bb","sb","btn","late","medium","medium","medium","early","early"];
How can the new dealer be determined when a player gets eliminated?
Note: To address this, a function named changeNextDealer() was created. Dealing with negative indexes was not an issue since the next dealer is in a clockwise direction. Refer to the code pen link for details.
dealerArrayPosition-1; //However, when the Big Blind and Small Blind are eliminated simultaneously, a negative position is obtained.
Is there a quick way to map a negative index like -1 to the last position, or -2 to the LastPosition-1?
Note: While this specific question remains unanswered, it is not the primary focus of this discussion. It may be addressed in a separate post.
How should the changePosition(dealer) function be implemented?
Despite numerous attempts, figuring out how to accomplish this has proven challenging.
Note: A function called changePosition() has been created. It can be found in the CodePen link provided.