Загрузка данных
using System;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
namespace mishdimAAA
{
public partial class Window1 : Window
{
private Random random = new();
private string parol = "";
private char[] otkrito = Array.Empty<char>();
public Window1()
{
InitializeComponent();
GenerateCode();
// Запрещаем вводить буквы и повторяющиеся цифры прямо с клавиатуры
Vvod.AddHandler(TextInputEvent, (s, e) =>
{
if (string.IsNullOrEmpty(e.Text)) return;
string currentText = Vvod.Text ?? "";
// Если вводится не цифра ИЛИ такая цифра уже написана в поле ИЛИ в самом вводе дубликаты
if (!e.Text.All(char.IsDigit) ||
e.Text.Any(c => currentText.Contains(c)) ||
e.Text.Distinct().Count() != e.Text.Length)
{
e.Handled = true; // Блокируем ввод символа
}
}, RoutingStrategies.Tunnel);
}
private void GenerateCode()
{
if (Slojnost == null) return;
// Легкая (0) -> 3, Средняя (1) -> 6, Тяжелая (2) -> 9
int length = (Slojnost.SelectedIndex + 1) * 3;
// Генерация пароля из уникальных цифр (0-9 перемешиваются без повторов)
char[] digits = "0123456789".ToCharArray();
random.Shuffle(digits);
parol = new string(digits.Take(length).ToArray());
otkrito = new string('*', length).ToCharArray();
if (Parol != null) Parol.Text = parol;
if (Otgadal != null) Otgadal.Text = new string(otkrito);
if (Status != null) Status.Text = $"Загадано число из {length} уникальных цифр";
if (Vvod != null) Vvod.Text = "";
if (Progress != null)
{
Progress.Maximum = length;
Progress.Value = 0;
}
}
private void SmenaSlojnosti(object? sender, SelectionChangedEventArgs e)
{
GenerateCode();
}
private void Pravila(object? sender, RoutedEventArgs e)
{
var oknopravil = new Window2();
oknopravil.ShowDialog(this);
}
private void Proverka(object? sender, RoutedEventArgs e)
{
if (Vvod == null || Status == null || Otgadal == null) return;
string popitka = Vvod.Text?.Trim() ?? "";
// Очищаем поле ввода сразу после попытки
Vvod.Text = "";
// 1. Проверка, что введены только цифры
if (string.IsNullOrEmpty(popitka) || !popitka.All(char.IsDigit))
{
Status.Text = "Ошибка: вводить можно только цифры!";
return;
}
// 2. Проверка длины
if (popitka.Length != parol.Length)
{
Status.Text = $"Введите ровно {parol.Length} цифр!";
return;
}
// 3. Проверка на уникальность цифр (без повторов)
if (popitka.Distinct().Count() != popitka.Length)
{
Status.Text = "Ошибка: цифры не должны повторяться!";
return;
}
int bulls = 0;
int cows = 0;
for (int i = 0; i < parol.Length; i++)
{
if (popitka[i] == parol[i])
{
bulls++;
otkrito[i] = parol[i];
}
else if (parol.Contains(popitka[i]))
{
cows++;
}
}
Otgadal.Text = new string(otkrito);
int otkrCount = 0;
for (int i = 0; i < otkrito.Length; i++)
{
if (otkrito[i] != '*') otkrCount++;
}
if (Progress != null)
{
Progress.Value = otkrCount;
}
if (bulls == parol.Length)
{
Status.Text = "Поздравляем! Вы угадали пароль!";
}
else
{
Status.Text = $"Попытка для '{popitka}': {bulls} Б, {cows} К";
}
}
}
}