2

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?

mega0319
  • 25
  • 5
  • 1
    `(a + b) % b` is the same as `a % b` – ASDFGerte Jul 07 '17 at 16:18
  • 2
    `+ currentPlayers.length`??? `+1`! – LS_ᴅᴇᴠ Jul 07 '17 at 16:19
  • 2
    You can easily keep it in bounds of the array with `dealerButtonPos = (dealerButtonPos + 1) % currentPlayers.length`. However, be careful of situations where a player stands up or is eliminated, leaving a "dead" spot which nevertheless has to deal cards. The dealer button is slightly more complicated than simply rotating around regardless of other conditions. – jered Jul 07 '17 at 16:22
  • 1
    Yes, if you have virtual "seats" that players may enter and leave at times (as well as by busting out), then you must implement the full button-and-blind rules of the game, including the head-up special case. – Lee Daniel Crocker Jul 07 '17 at 17:25
  • @jered Good point. I have taken into account active players and non-active players so far. – mega0319 Jul 09 '17 at 16:31
  • @LeeDanielCrocker I haven't implemented blinds yet, as I just finished implementing player positioning and dealer button positioning but I will definitely get to it eventually. – mega0319 Jul 09 '17 at 16:32

2 Answers2

2

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)
Krzysztof Safjanowski
  • 7,292
  • 3
  • 35
  • 47
1

I would do it this way.

let currentPlayers = [
    {name: "Foo", position: 1}, 
    {name: "Bar", position: 2},
    {name: "Baz", position: 3} 
]
let dealerButtonPos = 1
var index = dealerButtonPos % currentPlayers.length
dealerButtonPos = (index == 0) ? index + currentPlayers.length : index
VIX
  • 605
  • 4
  • 15