I am building a Texas Hold'em Poker web application using ReactJS. In order to keep track of the dealer button, I am using the following formula...
let currentPlayers = [
{name: "Foo", position: 1},
{name: "Bar", position: 2},
{name: "Baz", position: 3}
]
let dealerButtonPos = 1
dealerButtonPos = ((dealerButtonPos + currentPlayers.length ) % currentPlayers.length + 1 )
What I am trying to do here is make sure that the dealer button starts at position 1 and is always on the correct position at the start of every round. The dealer button, in this case, should always fall on position 1, then 2, then 3, then 1 again.
The function with the formula will be called at the end of every round. This should rotate the dealer button correctly for up to 8 players. Will I always be on the correct player position? Am I implementing this correctly? I had a hard time conceptualizing and getting to this point. Can someone explain the mod operator for clarity?
Be nice for iterator pattern
let currentPlayers = [
{name: "Foo", position: 1},
{name: "Bar", position: 2},
{name: "Baz", position: 3}
]
function* currentPlayersIterator(players) {
let index = 0;
while(true)
if (index < players.length) {
yield players[index++]
} else {
index = 0
yield players[index++]
}
}
let i = currentPlayersIterator(currentPlayers)
console.log(i.next().value)
console.log(i.next().value)
console.log(i.next().value)
console.log(i.next().value)
console.log(i.next().value)