I'm implementing the card game Hearts in JavaScript, and one of the core elements of the game is that you can pass your cards to other players. My game has strictly 4 players, no more, no less.
The passing order goes left, right, straight ahead, then no passing. Therefore, P1 would pass to P2, P4, P3, and then pass to nobody. The cycle loops until the game is over.
I am trying to implement this logic via arrow functions, however, it is not working. What I am trying to do is print out the player, and the player that they should pass to, based on a given index.
Here is my code, I hope it is clear what I am trying to do.
const players = [1, 2, 3, 4];
const passingOrder = 2;
const passCards = [
i => (i + 1) % 4, //pass left
i => (i - 1 + 4) % 4, //pass right
i => (i + 2) % 4, //pass straight
i => i //pass to ones self
];
players.forEach((player, index) => {
console.log(player + "passes to " + passCards[passingOrder](index))
})
passingOrder = 2;i => (i + 2) % 4and gives output 2, 3, 0, 1. I don't get why you think this is wrong.