/
anstyfil
/
Minesweeper
Обзор
Документация
Войти
/
anstyfil
/
Minesweeper
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
UI/InputValidator.cs
128 строк
4 KB
anstyfil
reorganize code into 3 abstraction levels (UI, Game, Storage)
12 дек 2025, 01:10
12 дек 2025, 01:10
99b8539
Код
Авторство
О чём код?
using System; using Minesweeper.Game; namespace Minesweeper.UI { public struct CommandResult { public string Command { get; set; } public int X { get; set; } public int Y { get; set; } public string? FilePath { get; set; } } public static class InputValidator { private static string? lastParseError; public static string? GetLastParseError() { return lastParseError; } public static CommandResult? ParseCommand(string input) { lastParseError = null; if (string.IsNullOrWhiteSpace(input)) { lastParseError = "UnknownCommand: use \"reveal x y\" or \"save filename\""; return null; } string[] parts = input.Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length < 2) { lastParseError = "UnknownCommand: use \"reveal x y\" or \"save filename\""; return null; } string command = parts[0].ToLower(); // Обработка команды save if (command == "save") { if (parts.Length < 2) { lastParseError = "ParseError: укажите путь к файлу для сохранения"; return null; } // Объединяем все части после "save" в путь к файлу (на случай пробелов в пути) string filePath = string.Join(" ", parts, 1, parts.Length - 1).Trim(); if (string.IsNullOrWhiteSpace(filePath)) { lastParseError = "ParseError: укажите путь к файлу для сохранения"; return null; } return new CommandResult { Command = command, FilePath = filePath }; } // Обработка команды reveal if (command == "reveal") { if (parts.Length != 3) { lastParseError = "UnknownCommand: use \"reveal x y\""; return null; } if (!int.TryParse(parts[1], out int x)) { lastParseError = "ParseError: x and y must be integers"; return null; } if (!int.TryParse(parts[2], out int y)) { lastParseError = "ParseError: x and y must be integers"; return null; } // Преобразуем координаты из 1-based (пользовательский ввод) в 0-based (внутренняя логика) return new CommandResult { Command = command, X = x - 1, Y = y - 1 }; } // Неизвестная команда lastParseError = "UnknownCommand: use \"reveal x y\" or \"save filename\""; return null; } public static string? Validate(BoardState board, int x, int y) { if (board.GameOver) { return "GameOver: no actions allowed"; } if (!board.Geometry.IsInside(x, y)) { return $"OutOfBounds: координаты должны быть от 1 до {board.Geometry.Width} по X и от 1 до {board.Geometry.Height} по Y"; } var cell = board.At(x, y); if (cell.IsRevealed) { return "InvalidStatus: cell is not closed"; } return null; } } }