mekasu0124

Untitled

Oct 15th, 2023
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. public class GameEngineViewModel : ViewModelBase
  2. {
  3.     public string? _gameText;
  4.     public string? _operation;
  5.     public string? _winLoseText = "";
  6.     public string? _scoreInfo;
  7.     public int? _numberOne;
  8.     public int? _numberTwo;
  9.     public int? _score = 0;
  10.     public double _solution;
  11.     public double _gameSolution;
  12.     public bool _isCorrect;
  13.     public bool _isVisible = false;
  14.     public bool _questionFinished = false;
  15.     public List<string> operations = new() { "➕", "➖", "✖️", "➗" };
  16.  
  17.     public GameEngineViewModel(Game game)
  18.     {
  19.         GameText = $"Game: {game.GameType} - Difficulty: {game.Difficulty}";
  20.         ScoreInfo = $"Score: {Score} / {game.TotalQuestions}";
  21.         NumberOne = GetNumber(game.Difficulty);
  22.         NumberTwo = GetNumber(game.Difficulty);
  23.         Operation = GetOperation(game.GameType);
  24.         GameSolution = GetSolution(game.GameType);
  25.  
  26.         WinLoseText = IsCorrect
  27.             ? "Correct Answer! Great Job!"
  28.             : $"Incorrect Answer! Expected: {GameSolution}";
  29.     }
  30.  
  31.     public int GetNumber(string diff)
  32.     {
  33.         Random rnd = new();
  34.  
  35.         return diff switch
  36.         {
  37.             "Easy" => rnd.Next(0, 300),
  38.             "Medium" => rnd.Next(0, 600),
  39.             "Hard" => rnd.Next(0, 900),
  40.             _ => throw new Exception("Error Getting Random Number")
  41.         };
  42.     }
  43.  
  44.     public string GetOperation(string gameType)
  45.     {
  46.         Random rnd = new();
  47.         int rndNum = rnd.Next(0, operations.Count);
  48.  
  49.         return gameType switch
  50.         {
  51.             "Addition" => "➕",
  52.             "Subtraction" => "➖",
  53.             "Multiplication" => "✖️",
  54.             "Division" => "➗",
  55.             "Random" => operations[rndNum],
  56.             _ => throw new Exception("Error Getting Operation")
  57.         };
  58.     }
  59.  
  60.     public void CheckSolution()
  61.     {
  62.         if (Solution == GameSolution)
  63.         {
  64.             Score++;
  65.             WinLoseText = "Correct Answer! Great Job!";
  66.         }
  67.         else
  68.         {
  69.             WinLoseText = $"Incorrect Answer! Expected: {GameSolution}";
  70.         }
  71.  
  72.         IsVisible = true;
  73.     }
  74.  
  75.     public double GetSolution(string gameType)
  76.     {
  77.         return gameType switch
  78.         {
  79.             "Addition" => GameSolution = Convert.ToDouble(NumberOne + NumberTwo),
  80.             "Subtraction" => GameSolution = Convert.ToDouble(NumberOne - NumberTwo),
  81.             "Multiplication" => GameSolution = Convert.ToDouble(NumberOne * NumberTwo),
  82.             "Division" => GameSolution = Convert.ToDouble(NumberOne / NumberTwo),
  83.             _ => throw new Exception("Error Obtaining Solution")
  84.         };
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment