Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using MeksMathGame.Models;
- using ReactiveUI;
- using System;
- using System.Collections.Generic;
- using System.Reactive;
- namespace MeksMathGame.ViewModels;
- public class GameEngineViewModel : ViewModelBase
- {
- public string? _gameText;
- public string? _operation;
- public string? _winLoseText = "";
- public string? _scoreInfo;
- public int? _numberOne;
- public int? _numberTwo;
- public int? _score = 0;
- public double _solution;
- public double _gameSolution;
- public bool _isCorrect;
- public bool _isVisible = false;
- public List<string> operations = new() { "➕", "➖", "✖️", "➗" };
- public GameEngineViewModel(Game game)
- {
- GameText = $"Game: {game.GameType} - Difficulty: {game.Difficulty}";
- ScoreInfo = $"Score: {Score} / {game.TotalQuestions}";
- NumberOne = GetNumber(game.Difficulty);
- NumberTwo = GetNumber(game.Difficulty);
- Operation = GetOperation(game.GameType);
- GameSolution = GetSolution(game.GameType);
- WinLoseText = IsCorrect
- ? "Correct Answer! Great Job!"
- : $"Incorrect Answer! Expected: {GameSolution}";
- IObservable<bool> entryOk = this.WhenAnyValue(
- x => x.Solution,
- x => x >= 0);
- Submit = ReactiveCommand.Create(
- CheckSolution,
- entryOk);
- GoHome = ReactiveCommand.Create(() => { });
- }
- public int GetNumber(string diff)
- {
- Random rnd = new();
- return diff switch
- {
- "Easy" => rnd.Next(0, 300),
- "Medium" => rnd.Next(0, 600),
- "Hard" => rnd.Next(0, 900),
- _ => throw new Exception("Error Getting Random Number")
- };
- }
- public string GetOperation(string gameType)
- {
- Random rnd = new();
- int rndNum = rnd.Next(0, operations.Count);
- return gameType switch
- {
- "Addition" => "➕",
- "Subtraction" => "➖",
- "Multiplication" => "✖️",
- "Division" => "➗",
- "Random" => operations[rndNum],
- _ => throw new Exception("Error Getting Operation")
- };
- }
- public void CheckSolution()
- {
- if (Solution == GameSolution)
- {
- Score++;
- WinLoseText = "Correct Answer! Great Job!";
- }
- else
- {
- WinLoseText = $"Incorrect Answer! Expected: {GameSolution}";
- }
- IsVisible = true;
- }
- public double GetSolution(string gameType)
- {
- return gameType switch
- {
- "Addition" => GameSolution = Convert.ToDouble(NumberOne + NumberTwo),
- "Subtraction" => GameSolution = Convert.ToDouble(NumberOne - NumberTwo),
- "Multiplication" => GameSolution = Convert.ToDouble(NumberOne * NumberTwo),
- "Division" => GameSolution = Convert.ToDouble(NumberOne / NumberTwo),
- _ => throw new Exception("Error Obtaining Solution")
- };
- }
- #region Getters/Setters
- public string? GameText
- {
- get => _gameText;
- set => this.RaiseAndSetIfChanged(ref _gameText, value);
- }
- public string? Operation
- {
- get => _operation;
- set => this.RaiseAndSetIfChanged(ref _operation, value);
- }
- public string WinLoseText
- {
- get => _winLoseText;
- set => this.RaiseAndSetIfChanged(ref _winLoseText, value);
- }
- public string? ScoreInfo
- {
- get => _scoreInfo;
- set => this.RaiseAndSetIfChanged(ref _scoreInfo, value);
- }
- public int? NumberOne
- {
- get => _numberOne;
- set => this.RaiseAndSetIfChanged(ref _numberOne, value);
- }
- public int? NumberTwo
- {
- get => _numberTwo;
- set => this.RaiseAndSetIfChanged(ref _numberTwo, value);
- }
- public int? Score
- {
- get => _score;
- set => this.RaiseAndSetIfChanged(ref _score, value);
- }
- public double Solution
- {
- get => _solution;
- set => this.RaiseAndSetIfChanged(ref _solution, value);
- }
- public double GameSolution
- {
- get => _gameSolution;
- set => this.RaiseAndSetIfChanged(ref _gameSolution, value);
- }
- public bool IsCorrect
- {
- get => _isCorrect;
- set => this.RaiseAndSetIfChanged(ref _isCorrect, value);
- }
- public bool IsVisible
- {
- get => _isVisible;
- set => this.RaiseAndSetIfChanged(ref _isVisible, value);
- }
- #endregion
- public ReactiveCommand<Unit, Unit> Submit { get; }
- public ReactiveCommand<Unit, Unit> GoHome { get; }
- }
Advertisement
Add Comment
Please, Sign In to add comment