Using the MVVM Design Pattern I am developing a basic operations math application.When I give a wrong answer, my score and counter score increase as if I answered correctly. I used some artificial intelligence to solve this but still no solution. That’s why I decided to ask here. I would be glad if you help.
MultiplicationPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewModels="clr-namespace:MauiApp2.ViewModels"
xmlns:models="clr-namespace:MauiApp2.Models"
x:Class="MauiApp2.Views.MultiplicationPage"
x:DataType="viewModels:MultiplicationPageViewModel"
Title="MultiplicationPage">
<VerticalStackLayout VerticalOptions="Start">
<HorizontalStackLayout>
<Label Text="Score: " FontSize="18" />
<Label Text="{Binding playerModal.Score}" FontSize="18" />
</HorizontalStackLayout>
<HorizontalStackLayout>
<Label Text="Time Remaining: " FontSize="18" />
<Label Text="{Binding playerModal.TimeReaming}" FontSize="18" />
</HorizontalStackLayout>
<HorizontalStackLayout>
<Label Text="Question: " FontSize="18" />
<Label Text="{Binding playerModal.Question}" FontSize="18" />
</HorizontalStackLayout>
<Entry Placeholder="Enter Your Answer" Text="{Binding playerModal.Answer}" FontSize="18" />
<Button Text="Submit" Command="{Binding SubmitAnswerCommand }" FontSize="18" />
</VerticalStackLayout>
</ContentPage>
MultiplicationPageViewModel.cs
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MauiApp2.Models;
using MauiApp2.Services;
using MauiApp2.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace MauiApp2.ViewModels
{
public partial class MultiplicationPageViewModel : BaseViewModel
{
public ObservableCollection<PlayerModal> PlayerDetails { get; set; } = new ObservableCollection<PlayerModal>();
public PlayerModal playerModal { get; set; } = new PlayerModal();
public ICommand SubmitAnswerCommand { get; }
public MultiplicationPageViewModel(INavigationService navigationService) : base(navigationService)
{
playerModal.Score = 0;
playerModal.TimeReaming= 60;
SubmitAnswerCommand = new Command(SubmitAnswer);
// Create a question as an example
GenerateQuestion();
// Start the timer
StartTimer();
}
[RelayCommand]
public void GenerateQuestion()
{
Random random = new Random();
int number1 = random.Next(1, 11); // a random number between 1 and 10
int number2 = random.Next(1, 11); // a random number between 1 and 10
// playerModal.Answer = (number1 * number2).ToString(); Automatic Answer
playerModal.Question = $"{number1} x {number2}";
}
public async void StartTimer()
{
while (true)
{
await Task.Delay(1000); // wait 1 sec
playerModal.TimeReaming--;
if (playerModal.TimeReaming == 0)
{
if (playerModal.Score > playerModal.HighScore)
{
playerModal.HighScore = playerModal.Score; // Update high score
}
GameOver(); // Finish the game when time is up
}
}
}
public void SubmitAnswer()
{
if (int.TryParse(playerModal.Answer, out int userAnswer))
{
int correctAnswer = int.Parse(playerModal.Answer);
if (userAnswer == correctAnswer)
{
playerModal.Score += 10;
playerModal.TimeReaming += 5;
}
else
{
playerModal.Score -= 10;
playerModal.TimeReaming -= 5;
// Negative score and time control
if (playerModal.Score < 0)
{
playerModal.Score = 0;
}
if (playerModal.TimeReaming < 0)
{
playerModal.TimeReaming = 0;
}
}
GenerateQuestion();
}
playerModal.Answer = "";
// Check earned score and update new high score
if (playerModal.Score > playerModal.HighScore)
{
playerModal.HighScore = playerModal.Score;
}
}
public async void GameOver()
{
await NavigationService.NavigateToAsync(nameof(GameOverPage));
}
}
}
PlayerModel.cs
using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MauiApp2.Models
{
public partial class PlayerModal : ObservableObject
{
[ObservableProperty]
int score;
[ObservableProperty]
int highScore;
[ObservableProperty]
int timeReaming;
[ObservableProperty]
string answer;
[ObservableProperty]
string question;
}
}
>Solution :
first, you are parsing playerModal.Answer and assigning it to userAnswer
if (int.TryParse(playerModal.Answer, out int userAnswer))
then, you again parse playerModal.Answer and assign it to correctAnswer
int correctAnswer = int.Parse(playerModal.Answer);
finally, you compare userAnswer and correctAnswer
if (userAnswer == correctAnswer)
they will always match no matter what the user answers
a = z;
b = z;
if (a == b) will always be true
instead do something like
correctAnswer = (number1 * number2).ToString();