-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameManager.cs
More file actions
299 lines (261 loc) · 11.8 KB
/
GameManager.cs
File metadata and controls
299 lines (261 loc) · 11.8 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ex02.ConsoleUtils;
using static MemoryGame.Player;
namespace MemoryGame
{
/// <summary>
/// Orchestrates the Memory game flow. Handles player setup (human vs computer),
/// board configuration, the main game loop, and end-of-game logic.
/// </summary>
public class GameManager
{
private Board m_BoardGame;
private Player[] m_Players;
private int m_CurrentPlayerIndex;
/// <summary>
/// Starts the Memory game. Prompts for player names, board size, then runs
/// the game until the board is complete or a player exits.
/// </summary>
public void StartGame()
{
setPlayers();
setBoard();
playGame();
}
private void setPlayers()
{
Console.WriteLine("Welcome to the Memory Game!!!");
Console.WriteLine("Player 1, enter your name (up to 20 characters, no spaces):");
string firstPlayerName = GameRules.GetThePlayerName();
m_Players = new Player[2];
m_Players[0] = new Player(firstPlayerName, eWhoPlay.Person);
Console.WriteLine("To play against the computer enter C," +
" to play against another player enter P (another character will not be accepted).");
bool secondPlayerIsComputer = GameRules.GetTheSecondPlayerType() == eWhoPlay.Computer;
if (secondPlayerIsComputer)
{
m_Players[1] = new Player("Computer", eWhoPlay.Computer);
}
else
{
Console.WriteLine("Player 2, enter your name (up to 20 characters, no spaces):");
string secondPlayerName = GameRules.GetThePlayerName();
m_Players[1] = new Player(secondPlayerName, eWhoPlay.Person);
}
}
private void setBoard()
{
bool checkSizeBoard = true;
while (checkSizeBoard)
{
Console.WriteLine("Write the number of rows of the game board " +
"(can be a number between 4 and 6, the number of cells on the board must be even):");
int numberOfRows = GameRules.GetNumberOfRowsAndColumns();
Console.WriteLine("Write the number of columns of the game board " +
"(can be a number between 4 and 6, the number of cells on the board must be even):");
int numberOfColumns = GameRules.GetNumberOfRowsAndColumns();
if (GameRules.ValidSizeBoard(numberOfRows, numberOfColumns))
{
checkSizeBoard = false;
m_BoardGame = new Board(numberOfRows, numberOfColumns);
}
else
{
Console.WriteLine("The number of cells must be even. Please enter different numbers for rows and columns.");
}
}
}
private void playGame()
{
m_CurrentPlayerIndex = 0;
for (int i = 0; i < m_Players.Length; i++)
{
m_Players[i].Score = 0;
}
bool gameRunning = true;
while (gameRunning && !m_BoardGame.CheckIfTheBoardComplete())
{
displayBoard(m_BoardGame);
int numberRowFirstCard;
int numberColumnFirstCard;
bool thePlayerIsComputer = m_Players[m_CurrentPlayerIndex].WhoPlay == "Computer";
if (thePlayerIsComputer)
{
Console.WriteLine("\n{0} now is your turn, please write the first cell you want to open:",
m_Players[m_CurrentPlayerIndex].Name);
computerTurn(out numberRowFirstCard, out numberColumnFirstCard);
}
else
{
Console.WriteLine("\n{0} now is your turn, please enter the first cell you want to open or Q to exit:",
m_Players[m_CurrentPlayerIndex].Name);
bool firstMove = playerTurn(out numberRowFirstCard, out numberColumnFirstCard);
if (!firstMove)
{
gameRunning = !gameRunning;
Console.WriteLine("\n{0} chose to exit the game so game over, Goodbye!", m_Players[m_CurrentPlayerIndex].Name);
continue;
}
}
Screen.Clear();
m_BoardGame.OpenCard(numberRowFirstCard, numberColumnFirstCard);
displayBoard(m_BoardGame);
int numberRowSecondCard;
int numberColumnSecondCard;
if (thePlayerIsComputer)
{
Console.WriteLine("\n{0} now is your turn, please enter the second cell you want to open:",
m_Players[m_CurrentPlayerIndex].Name);
computerTurn(out numberRowSecondCard, out numberColumnSecondCard);
}
else
{
Console.WriteLine("\n{0} now is your turn, please enter the second cell you want to open or Q to exit:",
m_Players[m_CurrentPlayerIndex].Name);
bool secondMove = playerTurn(out numberRowSecondCard, out numberColumnSecondCard);
if (!secondMove)
{
gameRunning = !gameRunning;
Console.WriteLine("\n{0} chose to exit the game so game over, Goodbye!", m_Players[m_CurrentPlayerIndex].Name);
continue;
}
}
m_BoardGame.OpenCard(numberRowSecondCard, numberColumnSecondCard);
if (GameRules.MatchCards(m_BoardGame.GetCard(numberRowFirstCard, numberColumnFirstCard),
m_BoardGame.GetCard(numberRowSecondCard, numberColumnSecondCard)))
{
m_Players[m_CurrentPlayerIndex].Score++;
Screen.Clear();
displayBoard(m_BoardGame);
Console.WriteLine("\nYou found match cards :)");
System.Threading.Thread.Sleep(1000);
}
else
{
Screen.Clear();
displayBoard(m_BoardGame);
Console.WriteLine("\nYou didn't find match cards :(");
System.Threading.Thread.Sleep(2000);
m_BoardGame.CloseCard(numberRowFirstCard, numberColumnFirstCard);
m_BoardGame.CloseCard(numberRowSecondCard, numberColumnSecondCard);
m_CurrentPlayerIndex = (m_CurrentPlayerIndex + 1) % 2;
}
Screen.Clear();
}
if (gameRunning)
{
printTheWinner(m_Players[0], m_Players[1]);
playAgain();
}
}
private bool playerTurn(out int o_NumberRow, out int o_NumberColumn)
{
bool stopTurn = true;
bool checkInput = true;
o_NumberRow = -1;
o_NumberColumn = -1;
while (checkInput)
{
string playerAnswer = GameRules.GetAnswerFromPlayer(m_BoardGame.NumberOfRows, m_BoardGame.NumberOfColumns);
bool playerExit = playerAnswer == "Q";
if (playerExit)
{
stopTurn = !stopTurn;
checkInput = !checkInput;
}
else
{
o_NumberRow = GameRules.GetNumberRow(playerAnswer[1]);
o_NumberColumn = GameRules.GetNumberColumn(playerAnswer[0]);
if (m_BoardGame.GetCard(o_NumberRow, o_NumberColumn).IsCardRevealed)
{
Console.WriteLine("The card in the cell is already exposed," +
" please choose another cell in which you want to reveal the card (write the cell you want to open or Q to exit):");
}
else
{
checkInput = !checkInput;
}
}
}
return stopTurn;
}
private void printTheWinner(Player i_FirstPlayer, Player i_SecondPlayer)
{
bool firstPlayerWin = i_FirstPlayer.Score > i_SecondPlayer.Score;
bool secondPlayerWin = i_FirstPlayer.Score < i_SecondPlayer.Score;
if (firstPlayerWin)
{
Console.WriteLine("Congratulations to {0} the winner of the memory game.", i_FirstPlayer.Name);
}
else if (secondPlayerWin)
{
Console.WriteLine("Congratulations to {0} the winner of the memory game.", i_SecondPlayer.Name);
}
else
{
Console.WriteLine("There is no winner for the memory game, there is a tie between the players!");
}
Console.WriteLine("{0} received {1} points.", i_FirstPlayer.Name, i_FirstPlayer.Score);
Console.WriteLine("{0} received {1} points.", i_SecondPlayer.Name, i_SecondPlayer.Score);
}
private void playAgain()
{
Console.WriteLine("Please write Y to play again or N to exit (other character will not be accepted)");
string playerAnswer = GameRules.GetAnswerForAgain();
bool playerWantAgain = playerAnswer == "Y";
if (playerWantAgain)
{
setBoard();
playGame();
}
else
{
Console.WriteLine("You chose to exit, it was nice game, Goodbye!");
}
}
private void computerTurn(out int o_NumberOfRow, out int o_NumberOfColumn)
{
string computerAnswer = GameRules.GetAnswerFromComputer(m_BoardGame, m_BoardGame.NumberOfRows, m_BoardGame.NumberOfColumns);
Console.WriteLine(computerAnswer);
System.Threading.Thread.Sleep(1000);
o_NumberOfRow = GameRules.GetNumberRow(computerAnswer[1]);
o_NumberOfColumn = GameRules.GetNumberColumn(computerAnswer[0]);
}
private static void displayBoard(Board i_GameBoard)
{
int numberOfRows = i_GameBoard.NumberOfRows;
int numberOfColumns = i_GameBoard.NumberOfColumns;
Console.Write(" ");
for (char column = 'A'; column < 'A' + numberOfColumns; column++)
{
Console.Write($" {column} ");
}
Console.WriteLine();
Console.Write(" ");
Console.WriteLine(new string('=', numberOfColumns * 4 + 1));
for (int i = 0; i < numberOfRows; i++)
{
Console.Write((i + 1) + " |");
for (int j = 0; j < numberOfColumns; j++)
{
if (i_GameBoard.GetCard(i, j).IsCardRevealed)
{
Console.Write(" " + i_GameBoard.GetCard(i, j).CardSign + " |");
}
else
{
Console.Write(" |");
}
}
Console.WriteLine();
Console.Write(" ");
Console.WriteLine(new string('=', numberOfColumns * 4 + 1));
}
}
}
}