Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GameEngineViewModel : ViewModelBase
- {
- public string? _gameText;
- public string? _operation;
- public string? _winLoseText = "";
- public string? _scoreInfo;
- public int? _numberOne;
- public int? _numberTwo;
- public int? _score = 0;
- public double _solution;
- public double _gameSolution;
- public bool _isCorrect;
- public bool _isVisible = false;
- public bool _questionFinished = false;
- public List<string> operations = new() { "➕", "➖", "✖️", "➗" };
- public GameEngineViewModel(Game game)
- {
- GameText = $"Game: {game.GameType} - Difficulty: {game.Difficulty}";
- ScoreInfo = $"Score: {Score} / {game.TotalQuestions}";
- NumberOne = GetNumber(game.Difficulty);
- NumberTwo = GetNumber(game.Difficulty);
- Operation = GetOperation(game.GameType);
- GameSolution = GetSolution(game.GameType);
- WinLoseText = IsCorrect
- ? "Correct Answer! Great Job!"
- : $"Incorrect Answer! Expected: {GameSolution}";
- }
- public int GetNumber(string diff)
- {
- Random rnd = new();
- return diff switch
- {
- "Easy" => rnd.Next(0, 300),
- "Medium" => rnd.Next(0, 600),
- "Hard" => rnd.Next(0, 900),
- _ => throw new Exception("Error Getting Random Number")
- };
- }
- public string GetOperation(string gameType)
- {
- Random rnd = new();
- int rndNum = rnd.Next(0, operations.Count);
- return gameType switch
- {
- "Addition" => "➕",
- "Subtraction" => "➖",
- "Multiplication" => "✖️",
- "Division" => "➗",
- "Random" => operations[rndNum],
- _ => throw new Exception("Error Getting Operation")
- };
- }
- public void CheckSolution()
- {
- if (Solution == GameSolution)
- {
- Score++;
- WinLoseText = "Correct Answer! Great Job!";
- }
- else
- {
- WinLoseText = $"Incorrect Answer! Expected: {GameSolution}";
- }
- IsVisible = true;
- }
- public double GetSolution(string gameType)
- {
- return gameType switch
- {
- "Addition" => GameSolution = Convert.ToDouble(NumberOne + NumberTwo),
- "Subtraction" => GameSolution = Convert.ToDouble(NumberOne - NumberTwo),
- "Multiplication" => GameSolution = Convert.ToDouble(NumberOne * NumberTwo),
- "Division" => GameSolution = Convert.ToDouble(NumberOne / NumberTwo),
- _ => throw new Exception("Error Obtaining Solution")
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment