How to brute force a math equation in a way that does not slow down an Android app

Viewed 66

I am building a math game for Android using Java. It includes a 3x3 grid of numbers that the user has to solve using mathematical operations. Before the start of the game, three random numbers 1-6 are rolled.

However, during my testing, I have discovered that some of the numbers are not solvable, say 4, with 1, 1, 1. (These numbers would never be rolled, but you get the point.)

I am using MxParser to find if the generated equation is correct, but the help I need is on how to create valid math equations.

I have decided to take the brute-force approach, but I am unsure as how to create valid mathematical equations with that method. It would have to generate equations like:

(Our numbers are: 1, 2, 3)

12^03^0 = 1 (True) Next number #1

12^03^0 = 2 (True) Next number #2

12^23^2 = 13 (False) Try again. #3

And so on.

I have tried brute-forcing already, but am unsure exactly how to do so. `

import org.mariuszgromada.math.mxparser.Expression;

import java.util.ArrayList;

public class GridParser {

public String num1String = "";
public String num2String = "";
public String num3String = "";
private static String l;
private static String q;
private static String a;
private static int p;
private static char[] w;
private static char[] c;

public void parseGrid(int num1, int num2, int num3, int gridID) {

    num1String = String.valueOf(num1);
    num2String = String.valueOf(num2);
    num3String = String.valueOf(num3);

    String operator1 = "+";
    String operator2 = "-";
    String operator3 = "/";
    String operator4 = "*";
    String operator5 = "^";

    ArrayList solutions3x3 = new ArrayList();
    ArrayList solutions4x4 = new ArrayList();
    ArrayList solutions6x6 = new ArrayList();

    String currentSolution = "";
    double NumberOnGrid = 0.0;

    int currentPOS = 0;


    if (gridID == 3) {
        NumberOnGrid = 3.0;
        String l = "1234567890-()=+√*/^";
        q = "";
        p = 0;
        w = a.toCharArray();
        c = l.toCharArray();

        while (!q.equals(a)) {
            for (int i = 0; i < l.length(); i++) {

                System.out.println("[❕] Trying: " + c[i]);

                if (c[i] == w[p]) {
                    q += c[i];
                    p++;
                    System.out.println("[✔️] Found: " + c[i]);
                    Expression targetExpression = new Expression(q);
                    double expressonResult = targetExpression.calculate();
                    if(expressonResult == NumberOnGrid){

                    }


                }

                if (q.equals(a)) {
                    System.out.println("\n[✔️] Equation Found!");
                }


            }
        }
    }
}
}

I apologize for my code, it is still in "Rough Draft" state. =)

Additionally, it would be great if this could run on a background thread so it would not slow down my Android app.

Thanks

Someone PLZZZ help!

0 Answers
Related