Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using ReactiveUI;
- using System;
- using System.Collections.Generic;
- using System.Reactive;
- using MeksMathGame.Models;
- using MeksMathGame.Services;
- namespace MeksMathGame.ViewModels;
- public class GameEngineViewModel : ViewModelBase
- {
- public string? _gameText;
- public string? _operation;
- public string? _finishText;
- public string? _scoreInfo;
- public int? _numOne;
- public int? _numTwo;
- public int? _userScore = 0;
- public double? _userSolution;
- 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}";
- NumOne = GetNumber(game.Difficulty, game.GameType);
- NumTwo = GetNumber(game.Difficulty, game.GameType);
- Operation = GetOperation(game.GameType);
- GameSolution = GetSolution(game.GameType);
- IObservable<bool> entryOk = this.WhenAnyValue(
- x => x.UserSolution,
- x => x >= 0 || !string.IsNullOrEmpty(x.ToString()));
- Submit = ReactiveCommand.Create(CheckSolution, entryOk);
- GoHome = ReactiveCommand.Create(() => { });
- }
- public int? GetNumber(string diff, string gameType)
- {
- Random rng = new();
- if (gameType == "Addition" || gameType == "Subtraction")
- {
- return diff switch
- {
- "Easy" => rng.Next(0, 300),
- "Medium" => rng.Next(0, 600),
- "Hard" => rng.Next(0, 900),
- _ => throw new Exception("Unable To Determine Difficulty")
- };
- }
- else
- {
- return diff switch
- {
- "Easy" => rng.Next(0, 100),
- "Medium" => rng.Next(0, 200),
- "Hard" => rng.Next(0, 300)
- _ => throw new Exception("Unable To Determine Difficulty")
- };
- }
- }
- public string? GetOperation(string gameType)
- {
- Random rng = new();
- int rndIndex = rng.Next(0, operations.Count);
- return gameType switch
- {
- "Addition" => operations[0],
- "Subtraction" => operations[1],
- "Multiplication" => operations[2],
- "Division" => operations[3],
- "Random" => operations[rndIndex],
- _ => throw new Exception("Unable To Determine Operation")
- };
- }
- public void CheckSolution()
- {
- if (UserSolution == GameSolution)
- {
- UserScore++;
- FinishText = "Correct Answer! Great Job!";
- }
- else
- {
- FinishText = $"Incorrect Answer. Expected {GameSolution}";
- }
- IsVisible = true;
- }
- public double? GetSolution(string gameType)
- {
- return gameType switch
- {
- "Addition" => GameSolution = Convert.ToDouble(NumOne + NumTwo),
- "Subtraction" => GameSolution = Convert.ToDouble(NumOne - NumTwo),
- "Multiplication" => GameSolution = Convert.ToDouble(NumOne * NumTwo),
- "Division" => GameSolution = Convert.ToDouble(NumOne / NumTwo),
- _ => throw new Exception("Unable To Determine Solution")
- };
- }
- public Game ReturnGameInfo(Game game)
- {
- Game newGame = new()
- {
- Username = game.Username,
- Date = game.Date,
- StartTime = "",
- EndTime = "",
- Duration = 0,
- Score = UserScore,
- TotalQuestions = game.TotalQuestions,
- Difficulty = game.Difficulty,
- GameType = game.GameType
- };
- Database.CreateNewGame(newGame);
- return newGame;
- }
- #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? FinishText
- {
- get => _finishText;
- set => this.RaiseAndSetIfChanged(ref _finishText, value);
- }
- public string? ScoreInfo
- {
- get => _scoreInfo;
- set => this.RaiseAndSetIfChanged(ref _scoreInfo, value);
- }
- public int? NumOne
- {
- get => _numOne;
- set => this.RaiseAndSetIfChanged(ref _numOne, value);
- }
- public int? NumTwo
- {
- get => _numTwo;
- set => this.RaiseAndSetIfChanged(ref _numTwo, value);
- }
- public int? UserScore
- {
- get => _userScore;
- set => this.RaiseAndSetIfChanged(ref _userScore, value);
- }
- public double? UserSolution
- {
- get => _userSolution;
- set => this.RaiseAndSetIfChanged(ref _userSolution, 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; }
- public ReactiveCommand<Unit, Unit> NextQuestion { get; }
- }
Advertisement
Add Comment
Please, Sign In to add comment