-
Notifications
You must be signed in to change notification settings - Fork 0
/
17.ts
executable file
·39 lines (35 loc) · 1.52 KB
/
17.ts
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
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-17
// Rock, Paper, Scissors
// It's Sunday and there's one week to go before the big day (Christmas Eve) when the elfs' work for the year will finally be complete. For the last 20 years the only game the elves have had to play together is StarCraft. They're looking for a fresh game to play.
// So, they get the idea to try a Rock, Paper, Scissors tournament.
// But the elves are sorta nerdy so they want to accomplish this using TypeScript types. The WhoWins should type to correctly determine the winner in a Rock-Paper-Scissors game. The first argument is the opponent and the second argument is you!
// What's Rock, Paper, Scissors?
// In case you haven't played it before, basically:
// it's a two player game where each player picks one of three options: Rock (👊🏻), Paper (🖐🏾), and Scissors (✌🏽)
// game rules:
// Rock crushes Scissors (Rock wins)
// Scissors cuts Paper (Scissors wins)
// Paper covers Rock (Paper wins)
// otherwise, a draw
type Rock = '👊🏻'
type Paper = '🖐🏾'
type Scissors = '✌🏽'
type RockPaperScissors = Rock | Paper | Scissors;
type WhoWins<Opponent extends RockPaperScissors, Me extends RockPaperScissors> =
Opponent extends Rock
? Me extends Rock
? 'draw'
: Me extends Paper
? 'win'
: 'lose'
: Opponent extends Paper
? Me extends Rock
? 'lose'
: Me extends Paper
? 'draw'
: 'win'
: Me extends Rock
? 'win'
: Me extends Paper
? 'lose'
: 'draw'