-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCardInPlay.php
More file actions
111 lines (98 loc) · 2.72 KB
/
Copy pathCardInPlay.php
File metadata and controls
111 lines (98 loc) · 2.72 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
declare(strict_types=1);
namespace Likewinter\CardDeck;
/**
* A card with a face-up or face-down orientation, for games with partial
* information (Solitaire, War, any game where some cards are hidden).
*
* Card itself is immutable and orientation-agnostic. CardInPlay wraps a
* Card and carries a Face state. Flipping returns a new instance rather
* than mutating, preserving immutability of the underlying card.
*
* Games that don't need face-down state can use Card directly and ignore
* this class entirely.
*/
final readonly class CardInPlay implements PlayableCard
{
/**
* @param Card $card The wrapped card.
* @param Face $face Orientation; defaults to face-up.
*/
public function __construct(
public Card $card,
public Face $face = Face::Up,
) {}
/**
* Create a face-up card in play.
*/
public static function up(Card $card): self
{
return new self($card, Face::Up);
}
/**
* Create a face-down card in play.
*/
public static function down(Card $card): self
{
return new self($card, Face::Down);
}
/**
* Returns a new instance with the opposite face. Does not mutate.
*/
#[\NoDiscard]
public function flip(): self
{
return new self($this->card, $this->face === Face::Up ? Face::Down : Face::Up);
}
/**
* Returns a new instance face-up. No-op if already up.
*/
#[\NoDiscard]
public function reveal(): self
{
return $this->face === Face::Up ? $this : new self($this->card, Face::Up);
}
/**
* Returns a new instance face-down. No-op if already down.
*/
#[\NoDiscard]
public function hide(): self
{
return $this->face === Face::Down ? $this : new self($this->card, Face::Down);
}
/**
* Whether the card is currently face-up.
*/
public function isFaceUp(): bool
{
return $this->face->isUp();
}
/**
* Whether the card is currently face-down.
*/
public function isFaceDown(): bool
{
return $this->face->isDown();
}
/**
* Renders "██" when face-down, otherwise the wrapped card's string form.
*/
public function __toString(): string
{
return $this->face === Face::Down ? '██' : (string) $this->card;
}
/**
* Returns the wrapped card, regardless of face state.
*/
public function underlyingCard(): Card
{
return $this->card;
}
/**
* Equal when both the wrapped card and the face state match.
*/
public function equals(PlayableCard $other): bool
{
return $other instanceof self && $this->card->equals($other->card) && $this->face === $other->face;
}
}