Загрузка данных


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
            Vvod.AddHandler(TextInputEvent, (s, e) =>
            {
                if (!string.IsNullOrEmpty(e.Text) && !e.Text.All(char.IsDigit))
                {
                    e.Handled = true;
                }
            }, RoutingStrategies.Tunnel);
        }

        private void GenerateCode()
        {
            if (Slojnost == null) return;

            // Легкая (0) -> 3, Средняя (1) -> 6, Тяжелая (2) -> 9
            int length = (Slojnost.SelectedIndex + 1) * 3;

            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 = "";

            // Проверка, что введены только цифры (на случай вставки текста через Ctrl+V)
            if (string.IsNullOrEmpty(popitka) || !popitka.All(char.IsDigit))
            {
                Status.Text = "Ошибка: вводить можно только цифры!";
                return;
            }

            if (popitka.Length != parol.Length)
            {
                Status.Text = $"Введите ровно {parol.Length} цифр!";
                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} К";
            }
        }
    }
}