Program.cs
6.87 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
using System;
using System.Collections.Generic;
using ConsoleUserInt.ConsoleUI;
using Engine;
using Game;
using GameEngine;
using MenuSystem;
using static System.Console;
namespace IDoMinesweeper
{
class Program
{
private static GameSettings _settings;
static void Main(string[] args)
{
Clear();
WriteLine("Welcome to Minesweeper");
_settings = GameConfigHandler.LoadConfig();
var gameMenu = new Menu(1)
{
Title = "New Game",
MenuItemsDictionary = new Dictionary<string, MenuItem>()
{
{
"1", new MenuItem()
{
Title = "Level 1",
CommandToExecute = TestGame
}
},
{
"2", new MenuItem()
{
Title = "Level 2",
CommandToExecute = OldTestGame
}
},
{
"3", new MenuItem()
{
Title = "Customize Your Board size",
CommandToExecute = SaveSettings
}
},
}
};
var menu0 = new Menu(0) //main menu
{
Title = "Main Menu",
MenuItemsDictionary = new Dictionary<string, MenuItem>()
{
{
"S", new MenuItem()
{
Title = "Start game",
CommandToExecute = gameMenu.Run
}
},
{
"J", new MenuItem()
{
Title = "Set defaults for game (save to JSON)",
CommandToExecute = SaveSettings
}
},
{
"D", new MenuItem()
{
Title = "Donate",
CommandToExecute = null
}
}
}
};
menu0.Run();
}
static string SaveSettings()
{
Console.Clear();
var boardWidth = 0;
var boardHeight = 0;
var userCanceled = false;
(boardWidth, userCanceled) = GetUserIntInput("Enter board width", 8, 30, 0);
if (userCanceled) return "";
(boardHeight, userCanceled) = GetUserIntInput("Enter board height", 8, 30, 0);
if (userCanceled) return "";
_settings.BoardHeight = boardHeight;
_settings.BoardWidth = boardWidth;
GameConfigHandler.SaveConfig(_settings);
return "";
}
static string TestGame()
{
var game = new BoardDim(_settings);
GameUI.PrintBoard(game);
Mines.MinesSetter(_settings.BoardHeight, _settings.BoardWidth);
Console.WriteLine();
return "GAME OVER!!";
}
static string OldTestGame()
{
Console.Clear();
WriteLine("Board height (Min. 8): ");
//WriteLine(">");
var checkHeight = ReadLine();
int height = 0;
if (!int.TryParse(checkHeight, out height))
{
WriteLine("Not an integer");
}
WriteLine("Board width (Min. 8): ");
//WriteLine(">");
var checkWidth = ReadLine();
int width = 0;
if (!int.TryParse(checkWidth, out width))
{
Console.WriteLine("Not an integer");
}
width = Convert.ToInt32(checkWidth);
height = Convert.ToInt32(checkHeight);
var game2 = new BoardDimOld(height, width);
var doIt = false;
do
{
if (height < 8 || width < 8)
{
Console.WriteLine("Too small. Minimal board size is 8x8");
}
else if (height > 30 || width > 30)
{
Console.WriteLine("Too big. Maximum board size is 30x30");
}
} while (!doIt);
return "GAME OVER!!";
}
static (int result, bool wasCanceled) GetUserIntInput(string prompt, int min, int max,
int? cancelIntValue = null, string cancelStrValue = "")
{
do
{
Console.WriteLine(prompt);
if (cancelIntValue.HasValue || !string.IsNullOrWhiteSpace(cancelStrValue))
{
Console.WriteLine($"To cancel input enter: {cancelIntValue}" +
$"{(cancelIntValue.HasValue && !string.IsNullOrWhiteSpace(cancelStrValue) ? " or " : "")}" +
$"{cancelStrValue}");
}
Console.Write(">");
var consoleLine = Console.ReadLine();
if (consoleLine == cancelStrValue) return (0, true);
if (int.TryParse(consoleLine, out var userInt))
{
return userInt == cancelIntValue ? (userInt, true) : (userInt, false);
}
Console.WriteLine($"'{consoleLine}' cant be converted to int value!");
} while (true);
}
static void getBoardDim()
{
/*_settings.Height = boardHeight;
_settings.Width = boardWidth;
GameConfigHandler.SaveConfig(_settings);
*/
}
}
internal class BoardDimOld
{
private static CellState[,] Board { get; set; }
public int BoardWidth { get; }
public int BoardHeight { get; }
public BoardDimOld(int boardHeight, int boardWidth)
{
if (boardHeight > 7 || boardWidth > 7)
{
BoardHeight = boardHeight;
BoardWidth = boardWidth;
// initialize the board
Board = new CellState[boardHeight, boardWidth];
}
else
{
Console.WriteLine($"{boardHeight} & {boardWidth} is too small! Minimum 8x8");
throw new ArgumentException("Board size has to be at least 8x8!");
}
}
public CellState[,] GetBoard()
{
var result = new CellState[BoardHeight, BoardWidth];
Array.Copy(Board, result, Board.Length);
return result;
}
}
}