Reputation: 11
My task is to create an android application where the user is presented with four levels of game difficulty.
Is there an easier way to produce this project to include all other difficulties?
Any help is appreciated, Mark
Upvotes: 1
Views: 1182
Reputation:
Start with generating most basic methods that gives you a number and an integer.
public String number(){
return ""+(r.nextInt(40-1)+1);
}
public String operator(){
return ""+(operators[new Random().nextInt(operators.length)]);
}
Now you can build your equations using this method.
level 0 is easy. You can see that it is number() + operator() + number();
public String level0(){
return number() + operator() + number();
}
next level is number() + operator() + number() + operator() + number();
.
public String level1(){
return number() + operator() + number() + operator() + number();
}
However as you saw it is hard to create this for every level. However as you can see, first part of level 1 is same thing as level 0 (number() + operator() + number()
). All you do is adding another operator and number to end. You can actually write it like this.
public String level1(){
return level0() + operator() + number();
}
But this will also become a burden since there are many levels. An easier way is using recursion. Each level is actually "result of previous level" + operator + number; So nth level is (n-1)th level + operator + number;
public String level(int n){
return level(n-1) + operator() + number();
}
If you run this, you will get a stack overflow. Because n will go below 1 and there is no exit condition for your recursion.
public String level(int n){
if(n==1) return number() + operator() + number();
else return level(n-1) + operator() + number();
}
Now if you call level(1) it will simply return soemthing like "2*3". level(2) will return level(1)+operator()+number() which is "2*3"+"-"+"5" which will be "2*3-5"
Upvotes: 1
Reputation: 60968
I guess I'd do something recursive like this (pseudocode):
private Random rnd;
Expression randomExpression(int numTerms) {
if (numTerms == 1)
return LiteralExpression(rnd.nextInt(40 - 1) + 1)
int split = rnd.nextInt(numTerms - 1) + 1;
Expression left = randomExpression(split);
Expression right = randomExpression(numTerms - split);
Operator op = operators[rnd.nextInt(operators.length)];
return CompoundExpression(left, op, right);
}
Then the top level invocation could randomly choose the numTerms
suitable for the selected level. Both CompoundExpression
and LiteralExpression
would derive from an abstract base class Expression
, and would take care both of turning their content into a string and of computing the desired result.
The stringification of CompoundExpression
in particular would take a lot of work if you want to use exactly the minimal number of parentheses required. For that, you'd likely want more information about operators as well, e.g. their precedence and whether they are associative. That's the reason why I chose to make Operator
a class (or perhaps an enum
) instead of a simple char
. Of course, you can at first use a simple stringification which will always enclose a CompoundExpression
into parentheses, whether it needs them or not. You can always work on simplifying the string once the basic functionality is in place and you can actually see where more work is required.
Upvotes: 1