using System;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Interactivity;
namespace mishdimAAA
{
public partial class Window1 : Window
{
private Random random = new();
private string secretCode = "";
private char[] openedMask = Array.Empty<char>();
public Window1()
{
InitializeComponent();
GenerateCode();
}
private void GenerateCode()
{
if (Slojnost == null) return;
int length = Slojnost.SelectedIndex + 3;
char[] digits = "0123456789".ToCharArray();
random.Shuffle(digits);
secretCode = new string(digits.Take(length).ToArray());
// Создаем массив звездочек по длине пароля
openedMask = new string('*', length).ToCharArray();
if (Parol != null) Parol.Text = secretCode;
if (Otgadal != null) Otgadal.Text = new string(openedMask);
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 rulesWindow = new Window2();
rulesWindow.ShowDialog(this);
}
private void Proverka(object? sender, RoutedEventArgs e)
{
if (Vvod == null || Status == null || Otgadal == null) return;
string guess = Vvod.Text?.Trim() ?? "";
if (guess.Length != secretCode.Length)
{
Status.Text = $"Введите ровно {secretCode.Length} цифр!";
return;
}
int bulls = 0;
int cows = 0;
for (int i = 0; i < secretCode.Length; i++)
{
if (guess[i] == secretCode[i])
{
bulls++;
openedMask[i] = secretCode[i]; // Открываем цифру, если угадано её точное место
}
else if (secretCode.Contains(guess[i]))
{
cows++;
}
}
// Обновляем поле со звездочками
Otgadal.Text = new string(openedMask);
// Считаем, сколько символов уже открыто (не равно '*'), и двигаем прогресс-бар
int openedCount = 0;
for (int i = 0; i < openedMask.Length; i++)
{
if (openedMask[i] != '*') openedCount++;
}
if (Progress != null)
{
Progress.Value = openedCount;
}
if (bulls == secretCode.Length)
{
Status.Text = "Поздравляем! Вы угадали пароль!";
}
else
{
Status.Text = $"Попытка для '{guess}': {bulls} Б, {cows} К";
}
Vvod.Text = "";
}
}
}