-
Notifications
You must be signed in to change notification settings - Fork 0
/
Engine.cs
246 lines (204 loc) · 8.71 KB
/
Engine.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
// <copyright file="Engine.cs" company="Telerik Academy">
// Telerik Academy - High Quality Code Team Project. Team Rhenium.
// </copyright>
namespace Minesweeper
{
using System;
using System.Linq;
using System.Collections.Generic;
/// <summary>
/// Initializes a new instance of the <seealso cref="Engine"/> class.
/// </summary>
public class Engine
{
public static List<Player> ScoreBoardTopPlayers = new List<Player>();
public static bool IsNewGame = true;
private int personalScore = 0;
private int rowToCheckForBomb;
private int colToCheckForBomb;
/// <summary>
/// Parses and returns the input command.
/// </summary>
/// <param name="inputCommand">The command to parse.</param>
/// <param name="fieldWithQuestionmarks">The field to validate before the command is parsed.</param>
/// <returns>Returns the parsed input command.</returns>
public string ParseInputCommand(string inputCommand, char[,] fieldWithQuestionmarks)
{
string command = inputCommand.Trim();
if (command.Length == 3)
{
//Checks if the input rows and cols are in the bounds of the playing field
//Checks if the second char is 'x' => it's a turn command, else it's another or wrong command
if (int.TryParse(command[0].ToString(), out this.rowToCheckForBomb) &&
int.TryParse(command[2].ToString(), out this.colToCheckForBomb) &&
this.rowToCheckForBomb <= fieldWithQuestionmarks.GetLength(0) &&
this.colToCheckForBomb <= fieldWithQuestionmarks.GetLength(1) &&
command[1] == 'x')
{
command = "turn";
}
}
return command;
}
/// <summary>
/// Executes a parsed command.
/// </summary>
/// <param name="inputCommand">The parsed command to execute.</param>
/// <param name="gameField">The game field dimensions.</param>
/// <param name="fieldWithQuestionmarks">The unrevealed game field.(with question marks)</param>
/// <param name="fieldWithBombs">The bombs field.(the places of the bombs)</param>
/// <param name="maxScore">The max score formula.
/// Calculated by the formula (gameField.FieldCols * gameField.FieldCols) - (gameField.FieldCols + gameField.FieldCols)
/// </param>
public void ExecuteCommand(string inputCommand, GameField gameField, char[,] fieldWithQuestionmarks, char[,] fieldWithBombs, int maxScore)
{
switch (inputCommand)
{
case "top":
Draw.ScoreBoard(ScoreBoardTopPlayers);
break;
case "restart":
fieldWithQuestionmarks = gameField.Create();
fieldWithBombs = gameField.PlaceBombs();
Draw.PlayingField(fieldWithQuestionmarks);
IsNewGame = true;
break;
case "exit":
Console.WriteLine("Good bye.");
break;
case "turn":
if (fieldWithBombs[this.rowToCheckForBomb, this.colToCheckForBomb] != '*')
{
IsNewGame = false;
this.SetSurroundingBombsCount(fieldWithQuestionmarks, fieldWithBombs, this.rowToCheckForBomb, this.colToCheckForBomb);
this.personalScore++;
if (maxScore == this.personalScore)
{
Console.WriteLine("Congrats! You won the game!");
Draw.PlayingField(fieldWithBombs);
this.EnterScoreToScoreBoard();
Draw.ScoreBoard(ScoreBoardTopPlayers);
fieldWithQuestionmarks = gameField.Create();
fieldWithBombs = gameField.PlaceBombs();
this.personalScore = 0;
IsNewGame = true;
}
else
{
Draw.PlayingField(fieldWithQuestionmarks);
}
}
else
{
Draw.PlayingField(fieldWithBombs);
Console.WriteLine("You just hit a bomb. Sorry.");
EnterScoreToScoreBoard();
Draw.ScoreBoard(ScoreBoardTopPlayers);
fieldWithQuestionmarks = gameField.Create();
fieldWithBombs = gameField.PlaceBombs();
this.personalScore = 0;
IsNewGame = true;
}
break;
default:
Console.WriteLine("Wrong command: {0}", inputCommand);
break;
}
}
/// <summary>
/// Sets the number of the surrounding bombs near a non-bomb cell which is picked.
/// </summary>
/// <param name="fieldWithQuestionmarks">The unrevealed game field.(with question marks)</param>
/// <param name="fieldWithBombs">The bombs field.(the places of the bombs)</param>
/// <param name="bombFieldRow">The current row to set the number.</param>
/// <param name="bombFieldCol">The current column to set the number.</param>
private void SetSurroundingBombsCount(char[,] fieldWithQuestionmarks, char[,] fieldWithBombs, int bombFieldRow, int bombFieldCol)
{
char surroundingBombs = this.GetSurroundingBombsCount(fieldWithBombs, bombFieldRow, bombFieldCol);
fieldWithBombs[bombFieldRow, bombFieldCol] = surroundingBombs;
fieldWithQuestionmarks[bombFieldRow, bombFieldCol] = surroundingBombs;
}
/// <summary>
/// Gets the number of the surrounding bombs.
/// </summary>
/// <param name="bombField">The field with placed bombs.</param>
/// <param name="row">The current row to get the number.</param>
/// <param name="col">The current column to get the number.</param>
/// <returns>The number of surrounding bombs near the cell.</returns>
private char GetSurroundingBombsCount(char[,] bombField, int row, int col)
{
int bombsCount = 0;
int rowsCount = bombField.GetLength(0);
int colsCount = bombField.GetLength(1);
if (row - 1 >= 0)
{
if (bombField[row - 1, col] == '*')
{
bombsCount++;
}
}
if (row + 1 < rowsCount)
{
if (bombField[row + 1, col] == '*')
{
bombsCount++;
}
}
if (col - 1 >= 0)
{
if (bombField[row, col - 1] == '*')
{
bombsCount++;
}
}
if (col + 1 < colsCount)
{
if (bombField[row, col + 1] == '*')
{
bombsCount++;
}
}
if ((row - 1 >= 0) && (col - 1 >= 0))
{
if (bombField[row - 1, col - 1] == '*')
{
bombsCount++;
}
}
if ((row - 1 >= 0) && (col + 1 < colsCount))
{
if (bombField[row - 1, col + 1] == '*')
{
bombsCount++;
}
}
if ((row + 1 < rowsCount) && (col - 1 >= 0))
{
if (bombField[row + 1, col - 1] == '*')
{
bombsCount++;
}
}
if ((row + 1 < rowsCount) && (col + 1 < colsCount))
{
if (bombField[row + 1, col + 1] == '*')
{
bombsCount++;
}
}
return char.Parse(bombsCount.ToString());
}
/// <summary>
/// Enter the score to the scoreboard when a round is finished.
/// </summary>
private void EnterScoreToScoreBoard()
{
Console.WriteLine("Enter your nickname for the score board: ");
string playerNickname = Console.ReadLine();
Player playerCurrentScore = new Player(playerNickname, this.personalScore);
ScoreBoardTopPlayers.Add(playerCurrentScore);
this.personalScore = 0;
IsNewGame = true;
}
}
}