Player position algorithm for cards game

121 Views Asked by At

So, in a classic 4 player cards game, which is multiplayer. I want to show each player sitting in the central position on their own respective screen. Opposite sitting players are partners.

      p2
  p1      p3
      p0

this configuration will work for first player.

For second player

      p3
  p2      p0
      p1

Is there a transformation (mathematical) that I can use to automatically find position. Or should I just make a hard coded lookup table, with manual entries.

Im hoping for a cool way to solve this. Matrices or something.

2

There are 2 best solutions below

4
On BEST ANSWER

If you already know the coordinates of the four locations where the players can be displayed, and just need to determine which player to show in which location, number the locations $0, 1, 2, 3$ around the circle, starting from the location where each player should appear on their own screen.

On the screen for player $n$, show player $m$ at position number (m - n + 4)%4.

(The + 4 is to avoid applying % to a negative number in programming languages such as C++ or Java. It is not needed if you're programming in Python.)


[Initially-posted answer follows.]

If you wanted to compute the coordinates of each position directly, one possibility is that the view of player $n$ shows player $m$ at the coordinates \begin{align} x &= x_0 - r \sin \left(\frac\pi2(m - n)\right) \\ y &= y_0 - r \cos \left(\frac\pi2(m - n)\right), \\ \end{align} where $(x_0,y_0)$ is the center of the pattern and $r$ is the distance from the center to each player. This assumes $x$ is increasing to the right and $y$ is increasing upward; if $y$ increases downward (as it does in some computer graphics) then change the first $-$ to $+$ in the formula for $y$.

The matrix answer that you've already received is doing essentially the same thing, if the scale of the picture is such that $x_0 = y_0 = 0$ and $r = 1$.

1
On

Set $A_0=[0\ -1]^T$, $A_1=[-1\ \ 0]^T$, $A_2=[0\ \ 1]^T$, and $A_3=[1\ \ 0]^T$. Then from player $n$'s perspective, the position of player $m$ should be $\left[ \begin{matrix}0 & -1 \\ 1 & 0 \end{matrix}\right]^nA_m$.