I'm currently developing a visual card game and facing the challenge of organizing the seating arrangement for players around the table. The priority is to assign the player's ID, with the boolean "pov" set to "true," to the bottom container within the "_seats" structure. Subsequently, the remaining player IDs should be positioned clockwise around the table.
Furthermore, the game supports 2 to 4 players, leading to variations in the number of seats and table configurations:
For 2 Players: Seats - Bottom, Left
For 3 Players: Seats - Bottom, Left, Right
For 4 Players: Seats - Bottom, Left, Top, Right
I have devised a functional solution that achieves this objective; however, the code is lengthy and overly reliant on nested statements. I am seeking guidance on how to optimize and streamline this process effectively. Thank you in advance!
Below is an example of the array containing player information passed into the function:
let players =
[{ name: "Player2", id: 1, admin: false, pov: false, curDealer: false },
{ name: "Player3", id: 2, admin: false, pov: false, curDealer: false },
{ name: "Player4", id: 3, admin: false, pov: false, curDealer: false },
{ name: "Player1", id: 4, admin: true, pov: true, curDealer: false }]
The object below outlines the final positions assigned to player IDs:
_seats = {
bottom: 0,
left: 0,
top: 0,
right: 0
};
This snippet illustrates my current implementation:
renderPlayersAtTable(arr) {
var seatCount = arr.length;
arr.forEach(player => {
if (player.pov === true) {
this._seats.bottom = player.id
if (seatCount === 4) {
switch (player.id) {
case 4:
this._seats.right = 3;
this._seats.top = 2;
this._seats.left = 1;
break;
case 3:
this._seats.right = 2;
this._seats.top = 1;
this._seats.left = 4;
break;
case 2:
this._seats.right = 1;
this._seats.top = 4;
this._seats.left = 3;
break;
case 1:
this._seats.right = 4;
this._seats.top = 3;
this._seats.left = 2;
break;
}
}
if (seatCount === 3) {
switch (player.id) {
case 3:
this._seats.right = 2;
this._seats.left = 1;
break;
case 2:
this._seats.right = 1;
this._seats.left = 3;
break;
case 1:
this._seats.right = 3;
this._seats.left = 2;
break;
}
}
if (seatCount === 2) {
switch (player.id) {
case 2:
this._seats.left = 1
break;
case 1:
this._seats.left = 2
break;
}
}
}
})
}