using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
namespace GuessGameApp.ViewModels
{
public class MainViewModel : INotifyPropertyChanged
{
private static readonly Random _random = new();
public List<int> DifficultyLevels { get; } = new() { 3, 4, 5, 6 };
private int _selectedDifficulty = 3;
public int SelectedDifficulty
{
get => _selectedDifficulty;
set
{
if (_selectedDifficulty != value)
{
_selectedDifficulty = value;
OnPropertyChanged();
GenerateNewCode();
}
}
}
private string _secretCode = string.Empty;
public string SecretCode
{
get => _secretCode;
private set
{
_secretCode = value;
OnPropertyChanged();
}
}
private string _userInput = string.Empty;
public string UserInput
{
get => _userInput;
set
{
_userInput = value;
OnPropertyChanged();
}
}
private string _statusMessage = string.Empty;
public string StatusMessage
{
get => _statusMessage;
private set
{
_statusMessage = value;
OnPropertyChanged();
}
}
public ICommand CheckGuessCommand { get; }
public MainViewModel()
{
CheckGuessCommand = new RelayCommand(CheckGuess);
GenerateNewCode();
}
private void GenerateNewCode()
{
char[] digits = new char[SelectedDifficulty];
for (int i = 0; i < SelectedDifficulty; i++)
{
digits[i] = (char)('0' + _random.Next(0, 10));
}
SecretCode = new string(digits);
StatusMessage = $"Загадано число из {SelectedDifficulty} цифр";
UserInput = string.Empty;
}
private void CheckGuess()
{
if (string.IsNullOrWhiteSpace(UserInput))
{
StatusMessage = "Введите число!";
return;
}
if (UserInput == SecretCode)
{
StatusMessage = "Угадано! Новая игра...";
GenerateNewCode();
}
else
{
StatusMessage = "Неверно, попробуйте еще раз!";
}
}
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string? name = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
// Простая реализация ICommand для кнопки
public class RelayCommand : ICommand
{
private readonly Action _execute;
public RelayCommand(Action execute) => _execute = execute;
public bool CanExecute(object? parameter) => true;
public void Execute(object? parameter) => _execute();
public event EventHandler? CanExecuteChanged { add { } remove { } }
}
}