-
-
Notifications
You must be signed in to change notification settings - Fork 424
/
Copy pathGame.cs
134 lines (120 loc) · 3.92 KB
/
Game.cs
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using LanguageExt;
using static LanguageExt.Prelude;
namespace CardGame;
/// <summary>
/// Pontoon / Vingt-Un / 21
/// </summary>
public partial class Game
{
/// <summary>
/// Play the game!
/// </summary>
public static Game<Unit> play =>
Display.askPlayerNames >>
enterPlayerNames >>
Display.introduction >>
Deck.shuffle >>
playHands;
/// <summary>
/// Ask the users to enter their names until `enterPlayerName` returns `false`
/// </summary>
static Game<Unit> enterPlayerNames =>
when(enterPlayerName, lazy(() => enterPlayerNames)).As();
/// <summary>
/// Wait for the user to enter the name of a player, then add them to the game
/// </summary>
static Game<bool> enterPlayerName =>
from name in Console.readLine
from _ in when(notEmpty(name), addPlayer(name))
select notEmpty(name);
/// <summary>
/// Play many hands until the players decide to quit
/// </summary>
static Game<Unit> playHands =>
from _ in initPlayers >>
playHand >>
Display.askPlayAgain
from key in Console.readKey
from __ in when(key.Key == ConsoleKey.Y, playHands)
select unit;
/// <summary>
/// Play a single hand
/// </summary>
static Game<Unit> playHand =>
dealHands >>
playRound >>
gameOver >>
Deck.cardsRemaining.Bind(Display.cardsRemaining);
/// <summary>
/// Deal the initial cards to the players
/// </summary>
static Game<Unit> dealHands =>
Players.with(players, dealHand);
/// <summary>
/// Deal the two initial cards to a player
/// </summary>
static Game<Unit> dealHand =>
from cs in dealCard >> dealCard
from player in Player.current
from state in Player.state
from _ in Display.playerState(player, state)
select unit;
/// <summary>
/// Deal a single card
/// </summary>
static Game<Unit> dealCard =>
from card in Deck.deal
from _ in Player.addCard(card)
select unit;
/// <summary>
/// For each active player check if they want to stick or twist
/// Keep looping until the game ends
/// </summary>
static Game<Unit> playRound =>
when(isGameActive,
from _ in Players.with(activePlayers, stickOrTwist)
from r in playRound
select r)
.As();
/// <summary>
/// Ask the player if they want to stick or twist, then follow their instruction
/// </summary>
static Game<Unit> stickOrTwist =>
when(isGameActive,
from player in Player.current
from _ in Display.askStickOrTwist(player) >>
Player.showCards
from key in Console.readKey
from __ in key.Key switch
{
ConsoleKey.S => Player.stick,
ConsoleKey.T => twist,
_ => stickOrTwistBerate
}
select unit)
.As();
/// <summary>
/// Player wants to twist
/// </summary>
static Game<Unit> twist =>
from card in Deck.deal
from _ in Player.addCard(card) >>
Display.showCard(card) >>
when(Player.isBust, Display.bust)
select unit;
/// <summary>
/// Berate the user for not following instructions!
/// </summary>
static Game<Unit> stickOrTwistBerate =>
Display.stickOrTwistBerate >>
stickOrTwist;
/// <summary>
/// Show the game over summary
/// </summary>
static Game<Unit> gameOver =>
from ws in winners
from ps in playersState
from _ in Display.winners(ws) >>
Display.playerStates(ps)
select unit;
}