mekasu0124

Untitled

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