-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameController.cs
260 lines (223 loc) · 9.77 KB
/
GameController.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using DAL;
using Domain;
using GameEngine;
namespace UnoConsoleUI;
public class GameController
{
private readonly UnoGameEngine _engine;
private readonly IGameRepository _repository;
private string _continueAnswer = "";
public GameController(UnoGameEngine engine, IGameRepository repository)
{
_engine = engine;
_repository = repository;
}
public void Run()
{
//only show when next player move
Console.Clear();
while (!_engine.IsGameOver() && _continueAnswer != "N")
{
Console.Clear();
// _engine.ClearMovesHistory();
ConsoleVisualization.DrawPlayerInfo(_engine.State);
if (_engine.GetActivePlayer().PlayerType is EPlayerType.AI or EPlayerType.Random)
{
Console.WriteLine(_engine.GetActivePlayer().PlayerType == EPlayerType.AI
? $"AI player {_engine.GetActivePlayer().NickName} turn..."
: $"Random player {_engine.GetActivePlayer().NickName} turn...");
Thread.Sleep(_engine.GetAisPeed());
}
else
{
Console.WriteLine(
"Your turn, make sure you are alone looking at the screen! Press enter to continue...");
Console.ReadLine();
}
DrawGameInfo();
}
if (_engine.IsGameOver())
{
ConsoleVisualization.DrawWinner(_engine.GetWinner()!, _engine.State, _engine.GetPlayersInOrder());
}
}
private void DrawGameInfo()
{
var activePlayer = _engine.GetActivePlayer();
while (true)
{
Console.Clear();
if (activePlayer.PlayerSkipTurn)
{
ConsoleVisualization.DrawPlayerSkipTurn(activePlayer, _engine.GetNextPlayer(),
_engine.GetPreviousPlayer(), _engine.State, _engine.ValidateMove, _engine.GetTopCard(), _engine.GetAisPeed());
_engine.NextPlayerTurn();
break;
}
ConsoleVisualization.DrawPreviousPlayerMoves(_engine.State, _engine.GetPreviousPlayer());
ConsoleVisualization.DrawPlayerInfo(_engine.State);
ConsoleVisualization.DrawDesk(_engine.State, _engine.GetPlayersInOrder());
ConsoleVisualization.DrawPlayerHand(activePlayer, _engine.ValidateMove, _engine.GetTopCard(),
_engine.HaveCardOfSameColor(activePlayer, _engine.GetTopCard()));
ConsoleVisualization.DrawChoices(_engine.State, _engine.GetAvailableChoices());
if (_engine.GetActivePlayer().PlayerType == EPlayerType.AI)
{
_engine.AiTurn();
ConsoleVisualization.DrawAiMoves(_engine.State, _engine.GetPreviousPlayer(), _engine.GetAisPeed());
break;
}
if (_engine.GetActivePlayer().PlayerType == EPlayerType.Random)
{
_engine.AiTurn(true);
ConsoleVisualization.DrawAiMoves(_engine.State, _engine.GetPreviousPlayer(), _engine.GetAisPeed());
break;
}
var choiceKey = Console.ReadLine()!.ToUpper().Trim();
if (!string.IsNullOrWhiteSpace(choiceKey))
{
if (ProcessPlayerChoice(choiceKey) == ETurnResult.NextPlayerTurn)
{
break;
}
}
}
}
private ETurnResult ProcessPlayerChoice(string choiceKey)
{
var selectedChoice = _engine.GetSelectedChoice(choiceKey);
var activePlayer = _engine.GetActivePlayer();
switch (selectedChoice)
{
case EPlayerAction.PlayCard:
var checkUnoCall = _engine.CheckUnoCall();
var playCard = ProcessPlayCardChoice();
if (!checkUnoCall)
{
ConsoleVisualization.DrawForgotToSayUno();
}
return playCard;
case EPlayerAction.EndTurn:
if (!_engine.CheckUnoCall())
{
ConsoleVisualization.DrawForgotToSayUno();
return ETurnResult.ActivePlayerTurnContinues;
}
_engine.NextPlayerTurn();
var move2 = new GameMove
{
Player = activePlayer,
Card = null,
EffectMessage = $"{activePlayer.NickName} ended their turn"
};
_engine.State.PreviousPlayerMoves.Add(move2);
return ETurnResult.NextPlayerTurn;
case EPlayerAction.DrawCard:
if (!_engine.CheckUnoCall())
{
ConsoleVisualization.DrawForgotToSayUno();
}
_engine.TakeCards(_engine.GetActivePlayer(), 1);
_engine.SetPlayerHasDrawnCard();
var move = new GameMove
{
Player = _engine.GetActivePlayer(),
Card = null,
EffectMessage = $"{_engine.GetActivePlayer().NickName} drew a card"
};
_engine.State.PreviousPlayerMoves.Add(move);
return ETurnResult.ActivePlayerTurnContinues;
case EPlayerAction.SaveGame:
_repository.Save(_engine.State.Id, _engine.State);
Console.Write("State saved. Continue (Y/N)[Y]?");
_continueAnswer = Console.ReadLine()!.Trim().ToUpper();
return _continueAnswer == "N" ? ETurnResult.NextPlayerTurn : ETurnResult.ActivePlayerTurnContinues;
case EPlayerAction.CallUno:
Console.WriteLine(_engine.CallUno()
? "Uno called at the right time!"
: "Uno called at the wrong time, 2 penalty cards!");
Console.ReadLine();
return ETurnResult.ActivePlayerTurnContinues;
case EPlayerAction.SkipTurnWd4:
var moveWd4 = new GameMove
{
Player =activePlayer,
Card = null,
EffectMessage = $"{activePlayer.NickName} draw four cards"
};
_engine.State.PreviousPlayerMoves.Add(moveWd4);
_engine.SkipTurnWd4();
return ETurnResult.NextPlayerTurn;
case EPlayerAction.ChallengeWd4:
var moveCwd4 = new GameMove
{
Player = activePlayer,
Card = null,
EffectMessage =
$"{activePlayer.NickName} challenged the use of the Wild Draw Four"
};
_engine.State.PreviousPlayerMoves.Add(moveCwd4);
Console.WriteLine(
$"Top card was: {ConsoleVisualization.VisualizeGameCard(_engine.State.DiscardPile[^2])}");
ConsoleVisualization.DrawPlayerHand(_engine.GetPreviousPlayer(), _engine.ValidateMove,
_engine.State.DiscardPile[^2]);
if (_engine.ChallengeWd4())
{
Console.WriteLine("Challenge fails, you must draw six cards and miss your turn");
Console.ReadLine();
return ETurnResult.NextPlayerTurn;
}
Console.WriteLine(
$"Challenge is successful, {_engine.GetPreviousPlayer().NickName} must draw four cards");
Console.WriteLine($"{_engine.GetActivePlayer().NickName}'s turn continues..");
Console.ReadLine();
return ETurnResult.ActivePlayerTurnContinues;
case EPlayerAction.Undefined:
Console.WriteLine($"Invalid choice {choiceKey}. Please try again.");
Console.ReadLine();
return ETurnResult.ActivePlayerTurnContinues;
default:
return ETurnResult.ActivePlayerTurnContinues;
}
}
private ETurnResult ProcessPlayCardChoice()
{
Console.Write("Select a card to play: ");
var cardShortcut = Console.ReadLine()?.ToUpper().Trim();
if (string.IsNullOrWhiteSpace(cardShortcut)) return ETurnResult.ActivePlayerTurnContinues;
var playedCard = _engine.GetSelectedCard(cardShortcut);
if (playedCard != null && _engine.ValidateMove(playedCard, _engine.GetTopCard()))
{
return playedCard.CardValue switch
{
ECardValue.Wild => ProcessWildCard(playedCard),
ECardValue.WildDrawFour => ProcessWildCard(playedCard),
ECardValue.Skip => _engine.PlayCard(playedCard),
ECardValue.Reverse => _engine.PlayCard(playedCard),
ECardValue.DrawTwo => _engine.PlayCard(playedCard),
_ => _engine.PlayCard(playedCard)
};
}
Console.WriteLine($"Invalid card {cardShortcut}. Please try again.");
Console.ReadLine();
return ETurnResult.ActivePlayerTurnContinues;
}
private ETurnResult ProcessWildCard(GameCard playedCard)
{
_engine.PlayCard(playedCard);
ConsoleVisualization.DrawColorChoices(_engine.GetAllColors);
while (true)
{
Console.Write("Your choice: ");
var colorKey = Console.ReadLine()?.ToUpper().Trim();
if (!string.IsNullOrWhiteSpace(colorKey))
{
var selectedColor = _engine.GetSelectedColor(colorKey);
if (selectedColor != ECardSuit.Undefined)
{
return _engine.SetColorForTopCard(selectedColor);
}
}
Console.WriteLine($"Wrong color key {colorKey}");
}
}
}