mekasu0124

Untitled

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