-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerRing.php
More file actions
95 lines (83 loc) · 2.48 KB
/
Copy pathPlayerRing.php
File metadata and controls
95 lines (83 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
declare(strict_types=1);
namespace Likewinter\CardDeck;
/**
* Turn order for a game: a rotating pointer over a fixed list of players.
*
* Most card games use clockwise turn order with an optional 'skip' or
* 'reverse' modifier. This primitive captures the common case and lets
* games query 'whose turn is it?' and 'advance to the next player'.
*
* Players are identified by integer index (0-based). Games map these to
* their own player representation (names, Stack objects, etc.).
*/
final class PlayerRing
{
/**
* @param int $numPlayers Number of players (>= 2).
* @param int $startingPlayer Index of the player who acts first.
*/
public function __construct(
public readonly int $numPlayers,
public readonly int $startingPlayer = 0,
) {
if ($numPlayers < 2) {
throw new \InvalidArgumentException('PlayerRing requires at least 2 players');
}
if ($startingPlayer < 0 || $startingPlayer >= $numPlayers) {
throw new \InvalidArgumentException('Starting player index out of range');
}
$this->current = $startingPlayer;
}
private int $current;
/**
* Returns the index of the player whose turn it is.
*/
public function current(): int
{
return $this->current;
}
/**
* Advance to the next player and return their index.
*/
public function next(): int
{
$this->current = ($this->current + 1) % $this->numPlayers;
return $this->current;
}
/**
* Returns the index of the next player (without moving the pointer).
*/
public function peekNext(): int
{
return ($this->current + 1) % $this->numPlayers;
}
/**
* Reset to the starting player.
*/
public function reset(): void
{
$this->current = $this->startingPlayer;
}
/**
* Set the current player to a specific index.
*/
public function setCurrent(int $player): void
{
if ($player < 0 || $player >= $this->numPlayers) {
throw new \InvalidArgumentException('Player index out of range');
}
$this->current = $player;
}
/**
* Advance N steps around the ring.
*/
public function advance(int $steps): int
{
if ($steps < 0) {
throw new \InvalidArgumentException('Steps must be non-negative');
}
$this->current = ($this->current + $steps) % $this->numPlayers;
return $this->current;
}
}