I am trying to develop a math problem-solving app (Linear Equation) in which the app will give an equation and the user will try to solve the equation step-by-step.
So basically what I'm trying to achieve is that I want to implement this solution path
For example, when the user approaches solution 1, the app will remove the other possible solution and focus on solution 1. As the user continues to solve the equation, the system will also continue to eliminate solutions that will not be taken anymore until the final answer is reached
Here is the sample UI. So the user will input answers and then when the user clicks Check Button, the app will check if the answer matches the possible solution path.
I tried nested if else but it was so messy. I think there's a better way to do it.
What would be the best approach or best way to implement this?
Thanks for all your answers guys
CodePudding user response:
My approach would look something like this in pseudocode.
for(Step currentStep in Steps
{
while(currentStep.IsSolved == false)
{
GetUserSolutionInput();
if(validSolutions.Contains(userSolution)
{
currentStep.IsSolved = true;
}
}
}
Reduced the problem space to the valid solutions at each step and keep looping until the user enters one of the valid solutions at that step. Either pre-generate a tree structure of steps or generate the next step only when you know which branch has been picked.
CodePudding user response:
I don't know where you get the possible solution paths etc from exactly.
But as you state you provide them beforehand I would suggest a data structure which allows for an actual tree hierarchy like e.g.
[CreateAssetMenu]
public class SolutionStep : ScriptableObject
{
public string left;
public string right;
public List<SolutionStep> possibleNextSteps;
}
which you create asset instance from via Assets
-> right click -> Create
-> SolutionStep
and fill in left
and right
accordingly as well as draggin in the next possible steps as well.
This way you could later on simply evaluate if any step is valid by doing e.g.
// here you reference the root "steps" -> initial equation
public List<SolutionStep> equationRoots;
private SolutionStep currentStep;
public void InitializeNextEquation()
{
// for now lets just assume random
currentStep = equationRoots[Random.Range(0, equationRoots.Count)];
// TODO display this equation and setup UI etc
// e.g.
//rootEquationText.text = $"{currentStep.left} = {currentStep.right}";
//steps.Clear(); // -> Destroys so far spawned step texts
//ShowUserInput();
}
public void CheckStep()
{
// wherever you get he user input from
var userLeft = xxx;
var userRight = xxx;
// Try to find a matching next step
var potentialNextStep = currentStep.possibleNextSteps.FirstOrDefault(step => step.left == userLeft);
if(potentialNextStep == null)
{
Debug.LogError("NOT A VALID NEXT STEP!");
return;
}
// if one with left side match was found now also check right side
if(potentialNextStep.righ != userRight)
{
Debug.LogError("EQUATION NOT MATCHING!");
return;
}
// otherwise this is a valid next step and you can log this in and go to the next round
Debug.Log("CORRECT NEXT STEP");
currentStep = potentialNextStep;
// TODO update UI e.g.
//steps.Add(currentStep); // -> Spawns new text item for this step
if(currentStep.possibleNextSteps.Count == 0)
{
Debug.Log("CONGRATS, SOLVED COMPLETELY!");
//HideUserInput();
// e.g. show Next button for invoking InitializeNextEquation again
}
}