mekasu0124

Untitled

Oct 17th, 2023
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.36 KB | None | 0 0
  1. using ReactiveUI;
  2. using System;
  3. using System.Reactive;
  4. using MeksMathGame.Models;
  5. using MeksMathGame.Services;
  6.  
  7. namespace MeksMathGame.ViewModels;
  8.  
  9. public class GameEngineViewModel : ViewModelBase
  10. {
  11.     public string? _gameText;
  12.     public string? _operation;
  13.     public string? _finishText;
  14.     public string? _scoreInfo;
  15.  
  16.     public int? _numOne;
  17.     public int? _numTwo;
  18.     public int? _userScore = 0;
  19.  
  20.     public double? _userSolution;
  21.     public double? _gameSolution;
  22.  
  23.     public bool? _isCorrect;
  24.     public bool? _isVisible = false;
  25.  
  26.     public GameEngineViewModel(Game game)
  27.     {
  28.         GameText = $"Game: {game.GameType} - Difficulty: {game.Difficulty}";
  29.         ScoreInfo = $"Score: {UserScore} / {game.TotalQuestions}";
  30.  
  31.         /*NumOne = GetNumber(game.Difficulty, game.GameType);
  32.         NumTwo = GetNumber(game.Difficulty, game.GameType);
  33.         Operation = GetOperation(game.GameType);*/
  34.  
  35.         GameSolution = GetSolution(game.GameType);
  36.  
  37.         IObservable<bool> entryOk = this.WhenAnyValue(
  38.             x => x.UserSolution,
  39.             x => x >= 0 || !string.IsNullOrEmpty(x.ToString()));
  40.  
  41.         Submit = ReactiveCommand.Create(CheckSolution, entryOk);
  42.         GoHome = ReactiveCommand.Create(() => { });
  43.     }
  44.  
  45.     public void CheckSolution()
  46.     {
  47.         if (UserSolution == GameSolution)
  48.         {
  49.             UserScore++;
  50.             FinishText = "Correct Answer! Great Job!";
  51.         }
  52.         else
  53.         {
  54.             FinishText = $"Incorrect Answer. Expected {GameSolution}";
  55.         }
  56.  
  57.         IsVisible = true;
  58.     }
  59.  
  60.     public double? GetSolution(string gameType)
  61.     {
  62.         return gameType switch
  63.         {
  64.             "Addition" => GameSolution = Convert.ToDouble(NumOne + NumTwo),
  65.             "Subtraction" => GameSolution = Convert.ToDouble(NumOne - NumTwo),
  66.             "Multiplication" => GameSolution = Convert.ToDouble(NumOne * NumTwo),
  67.             "Division" => GameSolution = Convert.ToDouble(NumOne / NumTwo),
  68.             _ => throw new Exception("Unable To Determine Solution")
  69.         };
  70.     }
  71.  
  72.     public Game ReturnGameInfo(Game game)
  73.     {
  74.         Game newGame = new()
  75.         {
  76.             Username = game.Username,
  77.             Date = game.Date,
  78.             StartTime = "",
  79.             EndTime = "",
  80.             Duration = 0,
  81.             Score = UserScore,
  82.             TotalQuestions = game.TotalQuestions,
  83.             Difficulty = game.Difficulty,
  84.             GameType = game.GameType
  85.         };
  86.  
  87.         Database.CreateNewGame(newGame);
  88.         return newGame;
  89.     }
  90.  
  91.     private void ExecuteNextQuestion()
  92.     {
  93.  
  94.     }
  95.  
  96.     private bool CanExecuteNextQuestion() => true;
  97.  
  98.     #region Getters/Setters
  99.     public string? GameText
  100.     {
  101.         get => _gameText;
  102.         set => this.RaiseAndSetIfChanged(ref _gameText, value);
  103.     }
  104.     public string? Operation
  105.     {
  106.         get => _operation;
  107.         set => this.RaiseAndSetIfChanged(ref _operation, value);
  108.     }
  109.     public string? FinishText
  110.     {
  111.         get => _finishText;
  112.         set => this.RaiseAndSetIfChanged(ref _finishText, value);
  113.     }
  114.     public string? ScoreInfo
  115.     {
  116.         get => _scoreInfo;
  117.         set => this.RaiseAndSetIfChanged(ref _scoreInfo, value);
  118.     }
  119.     public int? NumOne
  120.     {
  121.         get => _numOne;
  122.         set => this.RaiseAndSetIfChanged(ref _numOne, value);
  123.     }
  124.     public int? NumTwo
  125.     {
  126.         get => _numTwo;
  127.         set => this.RaiseAndSetIfChanged(ref _numTwo, value);
  128.     }
  129.     public int? UserScore
  130.     {
  131.         get => _userScore;
  132.         set => this.RaiseAndSetIfChanged(ref _userScore, value);
  133.     }
  134.     public double? UserSolution
  135.     {
  136.         get => _userSolution;
  137.         set => this.RaiseAndSetIfChanged(ref _userSolution, value);
  138.     }
  139.     public double? GameSolution
  140.     {
  141.         get => _gameSolution;
  142.         set => this.RaiseAndSetIfChanged(ref _gameSolution, value);
  143.     }
  144.     public bool? IsCorrect
  145.     {
  146.         get => _isCorrect;
  147.         set => this.RaiseAndSetIfChanged(ref _isCorrect, value);
  148.     }
  149.     public bool? IsVisible
  150.     {
  151.         get => _isVisible;
  152.         set => this.RaiseAndSetIfChanged(ref _isVisible, value);
  153.     }
  154.     #endregion
  155.  
  156.     public ReactiveCommand<Unit, Unit> Submit { get; }
  157.     public ReactiveCommand<Unit, Unit> GoHome { get; }
  158.     public ReactiveCommand<Unit, Unit> NextQuestion { get; }
  159. }
  160.  
Advertisement
Add Comment
Please, Sign In to add comment