-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSudokuGame.cs
More file actions
424 lines (348 loc) · 13.2 KB
/
Copy pathSudokuGame.cs
File metadata and controls
424 lines (348 loc) · 13.2 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
namespace Sudoku.Domain.Entities;
public class SudokuGame : AggregateRoot
{
private readonly List<Cell> _cells;
private readonly List<MoveHistory> _moveHistory;
public GameId Id { get; private set; }
public PlayerAlias PlayerAlias { get; private set; }
public GameDifficulty Difficulty { get; private set; }
public GameStatusEnum Status { get; private set; }
public GameStatistics Statistics { get; private set; }
public DateTime CreatedAt { get; private set; }
public DateTime? StartedAt { get; private set; }
public DateTime? CompletedAt { get; private set; }
public DateTime? PausedAt { get; private set; }
public List<MoveHistory> MoveHistory => _moveHistory;
private SudokuGame()
{
_cells = [];
_moveHistory = [];
}
public static SudokuGame Create(PlayerAlias playerAlias, GameDifficulty difficulty, IEnumerable<Cell> initialCells)
{
var game = new SudokuGame
{
Id = GameId.New(),
PlayerAlias = playerAlias,
Difficulty = difficulty,
Status = GameStatusEnum.NotStarted,
Statistics = GameStatistics.Create(),
CreatedAt = DateTime.UtcNow
};
game._cells.AddRange(initialCells);
game.AddDomainEvent(new GameCreatedEvent(game.Id, playerAlias, difficulty));
return game;
}
public static SudokuGame Reconstitute(
GameId id,
PlayerAlias playerAlias,
GameDifficulty difficulty,
GameStatusEnum statusEnum,
GameStatistics statistics,
IEnumerable<Cell> cells,
IEnumerable<MoveHistory> moveHistory,
DateTime createdAt,
DateTime? startedAt,
DateTime? completedAt,
DateTime? pausedAt)
{
var game = new SudokuGame
{
Id = id,
PlayerAlias = playerAlias,
Difficulty = difficulty,
Status = statusEnum,
Statistics = statistics,
CreatedAt = createdAt,
StartedAt = startedAt,
CompletedAt = completedAt,
PausedAt = pausedAt
};
game._cells.AddRange(cells);
game._moveHistory.AddRange(moveHistory);
return game;
}
public void StartGame()
{
if (Status != GameStatusEnum.NotStarted)
{
throw new GameNotInStartStateException($"Cannot start game in {Status} state");
}
Status = GameStatusEnum.InProgress;
StartedAt = DateTime.UtcNow;
AddDomainEvent(new GameStartedEvent(Id));
}
public void MakeMove(int row, int column, int? value)
{
if (Status != GameStatusEnum.InProgress)
{
throw new GameNotInProgressException($"Cannot make move in {Status} state");
}
var cell = GetCell(row, column);
if (cell.IsFixed)
{
throw new CellIsFixedException($"Cannot modify fixed cell at position ({row}, {column})");
}
// Check if this is a valid move (only when setting a value, not clearing)
var isValid = !value.HasValue || IsValidMove(row, column, value.Value);
// Record move history before making the change
var previousValue = cell.Value;
_moveHistory.Add(new MoveHistory(row, column, previousValue, value));
cell.SetValue(value);
Statistics.RecordMove(isValid);
AddDomainEvent(new MoveMadeEvent(Id, row, column, value, Statistics));
if (IsGameComplete())
{
CompleteGame();
}
}
public void AddPossibleValue(int row, int column, int value)
{
if (Status != GameStatusEnum.InProgress)
{
throw new GameNotInProgressException($"Cannot add possible value in {Status} state");
}
var cell = GetCell(row, column);
if (cell.IsFixed)
{
throw new CellIsFixedException($"Cannot modify fixed cell at position ({row}, {column})");
}
if (cell.HasValue)
{
throw new CellAlreadyHasValueException($"Cannot add possible values to cell with a definite value at position ({row}, {column})");
}
cell.AddPossibleValue(value);
AddDomainEvent(new PossibleValueAddedEvent(Id, row, column, value));
}
public void RemovePossibleValue(int row, int column, int value)
{
if (Status != GameStatusEnum.InProgress)
{
throw new GameNotInProgressException($"Cannot remove possible value in {Status} state");
}
var cell = GetCell(row, column);
if (cell.IsFixed)
{
throw new CellIsFixedException($"Cannot modify fixed cell at position ({row}, {column})");
}
cell.RemovePossibleValue(value);
AddDomainEvent(new PossibleValueRemovedEvent(Id, row, column, value));
}
public void ClearPossibleValues(int row, int column)
{
if (Status != GameStatusEnum.InProgress)
{
throw new GameNotInProgressException($"Cannot clear possible values in {Status} state");
}
var cell = GetCell(row, column);
if (cell.IsFixed)
{
throw new CellIsFixedException($"Cannot modify fixed cell at position ({row}, {column})");
}
cell.ClearPossibleValues();
AddDomainEvent(new PossibleValuesClearedEvent(Id, row, column));
}
public void UndoLastMove()
{
if (Status != GameStatusEnum.InProgress)
{
throw new GameNotInProgressException($"Cannot undo move in {Status} state");
}
if (_moveHistory.Count == 0)
{
throw new NoMoveHistoryException();
}
var lastMove = _moveHistory[^1];
_moveHistory.RemoveAt(_moveHistory.Count - 1);
var cell = GetCell(lastMove.Row, lastMove.Column);
cell.SetValue(lastMove.PreviousValue);
// Decrement the move count in statistics
Statistics.UndoMove();
AddDomainEvent(new MoveUndoneEvent(Id, lastMove.Row, lastMove.Column, lastMove.PreviousValue));
}
public void ResetGame()
{
if (Status == GameStatusEnum.NotStarted)
{
throw new GameNotInStartStateException("Game is already in its initial state");
}
// Reset all non-fixed cells
foreach (var cell in _cells.Where(c => !c.IsFixed))
{
cell.SetValue(null);
cell.ClearPossibleValues();
}
// Clear move history
_moveHistory.Clear();
// Reset statistics but keep the original timestamp
Statistics = GameStatistics.Create();
// Set the game state back to InProgress if it was completed or abandoned
if (Status == GameStatusEnum.Completed || Status == GameStatusEnum.Abandoned)
{
Status = GameStatusEnum.InProgress;
CompletedAt = null;
}
AddDomainEvent(new GameResetEvent(Id));
}
public ValidationResult ValidateGame()
{
var errors = new List<string>();
var isValid = true;
// Validate rows
for (int row = 0; row < 9; row++)
{
var rowValues = _cells.Where(c => c.Row == row && c.HasValue)
.Select(c => c.Value!.Value)
.ToList();
if (rowValues.Count != rowValues.Distinct().Count())
{
errors.Add($"Row {row + 1} contains duplicate values");
isValid = false;
}
}
// Validate columns
for (int column = 0; column < 9; column++)
{
var columnValues = _cells.Where(c => c.Column == column && c.HasValue)
.Select(c => c.Value!.Value)
.ToList();
if (columnValues.Count != columnValues.Distinct().Count())
{
errors.Add($"Column {column + 1} contains duplicate values");
isValid = false;
}
}
// Validate 3x3 boxes
for (int boxRow = 0; boxRow < 9; boxRow += 3)
{
for (int boxColumn = 0; boxColumn < 9; boxColumn += 3)
{
var boxValues = _cells.Where(c => c.Row >= boxRow && c.Row < boxRow + 3 &&
c.Column >= boxColumn && c.Column < boxColumn + 3 &&
c.HasValue)
.Select(c => c.Value!.Value)
.ToList();
if (boxValues.Count != boxValues.Distinct().Count())
{
errors.Add($"Box at position ({boxRow / 3 + 1}, {boxColumn / 3 + 1}) contains duplicate values");
isValid = false;
}
}
}
return new ValidationResult(isValid, errors);
}
public void PauseGame()
{
if (Status != GameStatusEnum.InProgress)
{
throw new GameNotInProgressException($"Cannot pause game in {Status} state");
}
Status = GameStatusEnum.Paused;
PausedAt = DateTime.UtcNow;
AddDomainEvent(new GamePausedEvent(Id));
}
public void ResumeGame()
{
if (Status != GameStatusEnum.Paused && Status != GameStatusEnum.NotStarted)
{
throw new GameNotPausedException($"Cannot resume game in {Status} state. Game must be NotStarted or Paused.");
}
// If starting from NotStarted, set the StartedAt timestamp
if (Status == GameStatusEnum.NotStarted)
{
StartedAt = DateTime.UtcNow;
}
Status = GameStatusEnum.InProgress;
PausedAt = null;
AddDomainEvent(new GameResumedEvent(Id));
}
public void AbandonGame()
{
if (Status == GameStatusEnum.Completed)
{
throw new GameAlreadyCompletedException("Cannot abandon completed game");
}
Status = GameStatusEnum.Abandoned;
AddDomainEvent(new GameAbandonedEvent(Id));
}
public void UpdatePlayDuration(TimeSpan duration)
{
Statistics.UpdatePlayDuration(duration);
}
public IReadOnlyList<Cell> GetCells() => _cells.AsReadOnly();
public Cell GetCell(int row, int column)
{
return _cells.FirstOrDefault(c => c.Row == row && c.Column == column) ?? throw new CellNotFoundException($"Cell not found at position ({row}, {column})");
}
public bool IsGameComplete()
{
return _cells.All(c => c.HasValue) && IsValidPuzzle();
}
private bool IsValidMove(int row, int column, int value)
{
return IsValidForRow(row, value) &&
IsValidForColumn(column, value) &&
IsValidForBox(row, column, value);
}
private bool IsValidForRow(int row, int value)
{
return _cells.Where(c => c.Row == row && c.HasValue).All(c => c.Value != value);
}
private bool IsValidForColumn(int column, int value)
{
return _cells.Where(c => c.Column == column && c.HasValue).All(c => c.Value != value);
}
private bool IsValidForBox(int row, int column, int value)
{
var boxRow = row / 3 * 3;
var boxColumn = column / 3 * 3;
return _cells.Where(c => c.Row >= boxRow && c.Row < boxRow + 3 && c.Column >= boxColumn && c.Column < boxColumn + 3 && c.HasValue).All(c => c.Value != value);
}
private bool IsValidPuzzle()
{
for (int row = 0; row < 9; row++)
{
var rowValues = _cells.Where(c => c.Row == row && c.HasValue)
.Select(c => c.Value!.Value)
.ToList();
if (rowValues.Count != 9 || rowValues.Distinct().Count() != 9)
{
return false;
}
}
for (int column = 0; column < 9; column++)
{
var columnValues = _cells.Where(c => c.Column == column && c.HasValue)
.Select(c => c.Value!.Value)
.ToList();
if (columnValues.Count != 9 || columnValues.Distinct().Count() != 9)
{
return false;
}
}
for (int boxRow = 0; boxRow < 9; boxRow += 3)
{
for (int boxColumn = 0; boxColumn < 9; boxColumn += 3)
{
var boxValues = _cells.Where(c => c.Row >= boxRow && c.Row < boxRow + 3 &&
c.Column >= boxColumn && c.Column < boxColumn + 3 &&
c.HasValue)
.Select(c => c.Value!.Value)
.ToList();
if (boxValues.Count != 9 || boxValues.Distinct().Count() != 9)
{
return false;
}
}
}
return true;
}
public void CompleteGame()
{
Status = GameStatusEnum.Completed;
CompletedAt = DateTime.UtcNow;
AddDomainEvent(new GameCompletedEvent(Id, Statistics));
}
}
public record MoveHistory(int Row, int Column, int? PreviousValue, int? NewValue);
public record ValidationResult(bool IsValid, List<string> Errors);