using System.Linq; // Убедитесь, что эта строка есть в самом верху файла
// ... остальной код класса Window1 ...
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());
if (Parol != null) Parol.Text = secretCode;
// Заполняем поле звездочками по длине пароля вместо "-"
if (Otgadal != null) Otgadal.Text = new string('*', length);
// Настраиваем и обнуляем прогресс-бар
if (Progress != null)
{
Progress.Maximum = length;
Progress.Value = 0;
}
if (Status != null) Status.Text = $"Загадано число из {length} цифр";
if (Vvod != null) Vvod.Text = "";
}
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;
// Берем текущее состояние поля "отгадано", чтобы уже открытые цифры сохранялись между попытками
char[] openedChars = (Otgadal.Text != null && Otgadal.Text.Length == secretCode.Length)
? Otgadal.Text.ToCharArray()
: new string('*', secretCode.Length).ToCharArray();
for (int i = 0; i < secretCode.Length; i++)
{
if (guess[i] == secretCode[i])
{
bulls++;
openedChars[i] = secretCode[i]; // Меняем звездочку на угаданный символ
}
else if (secretCode.Contains(guess[i]))
{
cows++;
}
}
// Выводим обновленную маску со звездочками и открытыми цифрами
Otgadal.Text = new string(openedChars);
// Обновляем прогресс-бар по количеству открытых символов (не звездочек)
if (Progress != null)
{
Progress.Value = openedChars.Count(c => c != '*');
}
if (bulls == secretCode.Length)
{
Status.Text = "Поздравляем! Вы угадали пароль!";
}
else
{
Status.Text = $"Попытка для '{guess}': {bulls} Б, {cows} К";
}
Vvod.Text = "";
}