mekasu0124

Untitled

Oct 16th, 2023
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.67 KB | None | 0 0
  1. using MeksMathGame.Models;
  2. using ReactiveUI;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Reactive;
  6.  
  7. namespace MeksMathGame.ViewModels;
  8.  
  9. public class GameEngineViewModel : ViewModelBase
  10. {
  11.     public string? _gameText;
  12.     public string? _operation;
  13.     public string? _winLoseText = "";
  14.     public string? _scoreInfo;
  15.     public int? _numberOne;
  16.     public int? _numberTwo;
  17.     public int? _score = 0;
  18.     public double _solution;
  19.     public double _gameSolution;
  20.     public bool _isCorrect;
  21.     public bool _isVisible = false;
  22.     public List<string> operations = new() { "➕", "➖", "✖️", "➗" };
  23.  
  24.     public GameEngineViewModel(Game game)
  25.     {
  26.         GameText = $"Game: {game.GameType} - Difficulty: {game.Difficulty}";
  27.         ScoreInfo = $"Score: {Score} / {game.TotalQuestions}";
  28.         NumberOne = GetNumber(game.Difficulty);
  29.         NumberTwo = GetNumber(game.Difficulty);
  30.         Operation = GetOperation(game.GameType);
  31.         GameSolution = GetSolution(game.GameType);
  32.  
  33.         WinLoseText = IsCorrect
  34.             ? "Correct Answer! Great Job!"
  35.             : $"Incorrect Answer! Expected: {GameSolution}";
  36.  
  37.         IObservable<bool> entryOk = this.WhenAnyValue(
  38.             x => x.Solution,
  39.             x => x >= 0);
  40.  
  41.         Submit = ReactiveCommand.Create(
  42.             CheckSolution,
  43.             entryOk);
  44.  
  45.         GoHome = ReactiveCommand.Create(() => { });
  46.     }
  47.  
  48.     public int GetNumber(string diff)
  49.     {
  50.         Random rnd = new();
  51.  
  52.         return diff switch
  53.         {
  54.             "Easy" => rnd.Next(0, 300),
  55.             "Medium" => rnd.Next(0, 600),
  56.             "Hard" => rnd.Next(0, 900),
  57.             _ => throw new Exception("Error Getting Random Number")
  58.         };
  59.     }
  60.  
  61.     public string GetOperation(string gameType)
  62.     {
  63.         Random rnd = new();
  64.         int rndNum = rnd.Next(0, operations.Count);
  65.  
  66.         return gameType switch
  67.         {
  68.             "Addition" => "➕",
  69.             "Subtraction" => "➖",
  70.             "Multiplication" => "✖️",
  71.             "Division" => "➗",
  72.             "Random" => operations[rndNum],
  73.             _ => throw new Exception("Error Getting Operation")
  74.         };
  75.     }
  76.  
  77.     public void CheckSolution()
  78.     {
  79.         if (Solution == GameSolution)
  80.         {
  81.             Score++;
  82.             WinLoseText = "Correct Answer! Great Job!";
  83.         }
  84.         else
  85.         {
  86.             WinLoseText = $"Incorrect Answer! Expected: {GameSolution}";
  87.         }
  88.  
  89.         IsVisible = true;
  90.     }
  91.  
  92.     public double GetSolution(string gameType)
  93.     {
  94.         return gameType switch
  95.         {
  96.             "Addition" => GameSolution = Convert.ToDouble(NumberOne + NumberTwo),
  97.             "Subtraction" => GameSolution = Convert.ToDouble(NumberOne - NumberTwo),
  98.             "Multiplication" => GameSolution = Convert.ToDouble(NumberOne * NumberTwo),
  99.             "Division" => GameSolution = Convert.ToDouble(NumberOne / NumberTwo),
  100.             _ => throw new Exception("Error Obtaining Solution")
  101.         };
  102.     }
  103.  
  104.     #region Getters/Setters
  105.     public string? GameText
  106.     {
  107.         get => _gameText;
  108.         set => this.RaiseAndSetIfChanged(ref _gameText, value);
  109.     }
  110.  
  111.     public string? Operation
  112.     {
  113.         get => _operation;
  114.         set => this.RaiseAndSetIfChanged(ref _operation, value);
  115.     }
  116.  
  117.     public string WinLoseText
  118.     {
  119.         get => _winLoseText;
  120.         set => this.RaiseAndSetIfChanged(ref _winLoseText, value);
  121.     }
  122.  
  123.     public string? ScoreInfo
  124.     {
  125.         get => _scoreInfo;
  126.         set => this.RaiseAndSetIfChanged(ref _scoreInfo, value);
  127.     }
  128.  
  129.     public int? NumberOne
  130.     {
  131.         get => _numberOne;
  132.         set => this.RaiseAndSetIfChanged(ref _numberOne, value);
  133.     }
  134.  
  135.     public int? NumberTwo
  136.     {
  137.         get => _numberTwo;
  138.         set => this.RaiseAndSetIfChanged(ref _numberTwo, value);
  139.     }
  140.  
  141.     public int? Score
  142.     {
  143.         get => _score;
  144.         set => this.RaiseAndSetIfChanged(ref _score, value);
  145.     }
  146.  
  147.     public double Solution
  148.     {
  149.         get => _solution;
  150.         set => this.RaiseAndSetIfChanged(ref _solution, value);
  151.     }
  152.  
  153.     public double GameSolution
  154.     {
  155.         get => _gameSolution;
  156.         set => this.RaiseAndSetIfChanged(ref _gameSolution, value);
  157.     }
  158.  
  159.     public bool IsCorrect
  160.     {
  161.         get => _isCorrect;
  162.         set => this.RaiseAndSetIfChanged(ref _isCorrect, value);
  163.     }
  164.  
  165.     public bool IsVisible
  166.     {
  167.         get => _isVisible;
  168.         set => this.RaiseAndSetIfChanged(ref _isVisible, value);
  169.     }
  170.     #endregion
  171.  
  172.     public ReactiveCommand<Unit, Unit> Submit { get; }
  173.     public ReactiveCommand<Unit, Unit> GoHome { get; }
  174. }
  175.  
Advertisement
Add Comment
Please, Sign In to add comment