I'm trying to create the 24 game in java to practice. The 24 Game is an arithmetical game in which the objective is to find a way to manipulate four integers so that the end result is 24. Addition, subtraction, multiplication, or division in any order of the numbers may be used to make the four digits operations from one to nine equal 24.
The rules are simple: you have to use each number only once and only the 4 numbers that were read from the user to find one equation to obtain 24.
Basically my problem is I am trying to append the equations that == 24 to the StringBuffer output. I may be using StringBuffer incorrectly. I want to add all the equations to the StringBuffer and then delete all of the duplicate equations before printing the StringBuffer to the console. However, no matter what all the equations print and I'm not sure why. I'm not sure if this is even possible with StringBuffer as it may only append chars.
int i = 0;
public void Solve(double w, double x, double y, double z){
double [] nums = {w,x,y,z};
int length = nums.length;
double eHold=0, fHold=0;
boolean [] used = new boolean[length];
StringBuffer output = new StringBuffer(400);
int position = 0;
int value = 0;
if (position == nums.length){
//for (int i=0; i<output.length();i++){
//if (output.)
System.out.println("");
return;
//}
}
else {
for (int i=0; i<nums.length; i++){
if (((nums[i]/nums[i+1])*fHold)==24)
output.append (nums[i]+" / "+nums[i+1]+" * ("+nums[i+3]+" + "+nums[i+4]+")" + " = 24");
else if (((nums[i]/nums[i+1])-fHold)==24)
output.append (nums[i]+" / "+nums[i+1]+" - ("+nums[i+3]+" + "+nums[i+4]+")" + " = 24");
else if (((nums[i]*nums[i+1])-fHold)==24)
output.append (nums[i]+" * "+nums[i+1]+" - ("+nums[i+3]+" + "+nums[i+4]+")" + " = 24");
else if (((nums[i]*nums[i+1])/fHold)==24)
output.append (nums[i]+" * "+nums[i+1]+" / ("+nums[i+3]+" + "+nums[i+4]+")" + " = 24");
else if (((nums[i]-nums[i+1])*fHold)==24)
output.append (nums[i]+" - "+nums[i+1]+" * ("+nums[i+3]+" + "+nums[i+4]+")" + " = 24");
else if (((nums[i]-nums[i+1])/fHold)==24)
output.append (nums[i]+" - "+nums[i+1]+" / ("+nums[i+3]+" + "+nums[i+4]+")" + " = 24");
else value++;
fHold=nums[i+3]-nums[i+4];
if (((nums[i]/nums[i+1])*fHold)==24)
output.append (nums[i]+" / "+nums[i+1]+" * ("+nums[i+3]+" - "+nums[i+4]+")" + " = 24");
else if (((nums[i]/nums[i+1])+fHold)==24)
output.append (nums[i]+" / "+nums[i+1]+" + ("+nums[i+3]+" - "+nums[i+4]+")" + " = 24");
else if (((nums[i]*nums[i+1])+fHold)==24)
output.append (nums[i]+" * "+nums[i+1]+" + ("+nums[i+3]+" - "+nums[i+4]+")" + " = 24");
else if (((nums[i]*nums[i+1])/fHold)==24)
output.append (nums[i]+" * "+nums[i+1]+" / ("+nums[i+3]+" - "+nums[i+4]+")" + " = 24");
else if (((nums[i]+nums[i+1])*fHold)==24)
output.append (nums[i]+" + "+nums[i+1]+" * ("+nums[i+3]+" - "+nums[i+4]+")" + " = 24");
else if (((nums[i]+nums[i+1])/fHold)==24)
output.append (nums[i]+" + "+nums[i+1]+" / ("+nums[i+3]+" - "+nums[i+4]+")" + " = 24");
else value++;
fHold=nums[i+3]*nums[i+4];
if (((nums[i]/nums[i+1])-fHold)==24)
output.append (nums[i]+" / "+nums[i+1]+" * ("+nums[i+3]+" * "+nums[i+4]+")" + " = 24");
else if (((nums[i]/nums[i+1])+fHold)==24)
output.append (nums[i]+" / "+nums[i+1]+" + ("+nums[i+3]+" * "+nums[i+4]+")" + " = 24");
else if (((nums[i]-nums[i+1])+fHold)==24)
output.append (nums[i]+" - "+nums[i+1]+" + ("+nums[i+3]+" * "+nums[i+4]+")" + " = 24");
else if (((nums[i]-nums[i+1])/fHold)==24)
output.append (nums[i]+" - "+nums[i+1]+" / ("+nums[i+3]+" * "+nums[i+4]+")" + " = 24");
else if (((nums[i]+nums[i+1])-fHold)==24)
output.append (nums[i]+" + "+nums[i+1]+" - ("+nums[i+3]+" * "+nums[i+4]+")" + " = 24");
else if (((nums[i]+nums[i+1])/fHold)==24)
output.append (nums[i]+" + "+nums[i+1]+" / ("+nums[i+3]+" * "+nums[i+4]+")" + " = 24");
else value++;
fHold=nums[i+3]/nums[i+4];
if (((nums[i]-nums[i+1])*fHold)==24)
output.append (nums[i]+" - "+nums[i+1]+" * ("+nums[i+3]+" / "+nums[i+4]+")" + " = 24");
else if (((nums[i]-nums[i+1])+fHold)==24)
output.append (nums[i]+" - "+nums[i+1]+" + ("+nums[i+3]+" / "+nums[i+4]+")" + " = 24");
else if (((nums[i]*nums[i+1])+fHold)==24)
output.append (nums[i]+" * "+nums[i+1]+" + ("+nums[i+3]+" / "+nums[i+4]+")" + " = 24");
else if (((nums[i]*nums[i+1])-fHold)==24)
output.append (nums[i]+" * "+nums[i+1]+" - ("+nums[i+3]+" / "+nums[i+4]+")" + " = 24");
else if (((nums[i]+nums[i+1])*fHold)==24)
output.append (nums[i]+" + "+nums[i+1]+" * ("+nums[i+3]+" / "+nums[i+4]+")" + " = 24");
else if (((nums[i]+nums[i+1])-fHold)==24)
output.append (nums[i]+" + "+nums[i+1]+" - ("+nums[i+3]+" / "+nums[i+4]+")" + " = 24");
else value++;
fHold=nums[i+1]*nums[i+3];
if (((nums[i]-fHold)/nums[i+4])==24)
output.append (nums[i]+" - ("+nums[i+1]+" * "+nums[i+3]+") / "+nums[i+4] + " = 24");
else if (((nums[i]-fHold)+nums[i+4])==24)
output.append (nums[i]+" - ("+nums[i+1]+" * "+nums[i+3]+") + "+nums[i+4] + " = 24");
else if (((nums[i]/fHold)+nums[i+4])==24)
output.append (nums[i]+" / ("+nums[i+1]+" * "+nums[i+3]+") + "+nums[i+4] + " = 24");
else if (((nums[i]/fHold)-nums[i+4])==24)
output.append (nums[i]+" / ("+nums[i+1]+" * "+nums[i+3]+") - "+nums[i+4] + " = 24");
else if (((nums[i]+fHold)/nums[i+4])==24)
output.append (nums[i]+" + ("+nums[i+1]+" * "+nums[i+3]+") / "+nums[i+4] + " = 24");
else if (((nums[i]+fHold)-nums[i+4])==24)
output.append (nums[i]+" + ("+nums[i+1]+" * "+nums[i+3]+") - "+nums[i+4] + " = 24");
else value++;
fHold=nums[i+1]-nums[i+3];
if (((nums[i]*fHold)/nums[i+4])==24)
output.append (nums[i]+" * ("+nums[i+1]+" - "+nums[i+3]+") / "+nums[i+4] + " = 24");
else if (((nums[i]*fHold)+nums[i+4])==24)
output.append (nums[i]+" * ("+nums[i+1]+" - "+nums[i+3]+") + "+nums[i+4] + " = 24");
else if (((nums[i]/fHold)+nums[i+4])==24)
output.append (nums[i]+" / ("+nums[i+1]+" - "+nums[i+3]+") + "+nums[i+4] + " = 24");
else if (((nums[i]/fHold)*nums[i+4])==24)
output.append (nums[i]+" / ("+nums[i+1]+" - "+nums[i+3]+") * "+nums[i+4] + " = 24");
else value++;
fHold=nums[i+1]/nums[i+3];
if (((nums[i]-fHold)*nums[i+4])==24)
output.append (nums[i]+" - ("+nums[i+1]+" / "+nums[i+3]+") * "+nums[i+4] + " = 24");
else if (((nums[i]-fHold)+nums[i+4])==24)
output.append (nums[i]+" - ("+nums[i+1]+" / "+nums[i+3]+") + "+nums[i+4] + " = 24");
else if (((nums[i]*fHold)+nums[i+4])==24)
output.append (nums[i]+" * ("+nums[i+1]+" / "+nums[i+3]+") + "+nums[i+4] + " = 24");
else if (((nums[i]*fHold)-nums[i+4])==24)
output.append (nums[i]+" * ("+nums[i+1]+" / "+nums[i+3]+") - "+nums[i+4] + " = 24");
else if (((nums[i]+fHold)*nums[i+4])==24)
output.append (nums[i]+" + ("+nums[i+1]+" / "+nums[i+3]+") * "+nums[i+4] + " = 24");
else if (((nums[i]+fHold)-nums[i+4])==24)
output.append (nums[i]+" + ("+nums[i+1]+" / "+nums[i+3]+") - "+nums[i+4] + " = 24");
else value++;
fHold=nums[i+1]+nums[i+3];
if (((nums[i]*fHold)/nums[i+4])==24)
output.append (nums[i]+" * ("+nums[i+1]+" + "+nums[i+3]+") / "+nums[i+4] + " = 24");
else if (((nums[i]*fHold)-nums[i+4])==24)
output.append (nums[i]+" * ("+nums[i+1]+" + "+nums[i+3]+") - "+nums[i+4] + " = 24");
else if (((nums[i]/fHold)-nums[i+4])==24)
output.append (nums[i]+" / ("+nums[i+1]+" + "+nums[i+3]+") - "+nums[i+4] + " = 24");
else if (((nums[i]/fHold)*nums[i+4])==24)
output.append (nums[i]+" / ("+nums[i+1]+" + "+nums[i+3]+") * "+nums[i+4] + " = 24");
else value++;
if (value == 192){
output.append("There is no solution for this set of numbers");
this.i ++;
}
position ++;
eHold=nums[i+3];
nums[i+3]=nums[i+4];
nums[i+4]=eHold;
}
position ++;
eHold=nums[i+1];
nums[i+1]=nums[i+3];
nums[i+3]=nums[i+4];
nums[i+4]=eHold;
}
position ++;
eHold=nums[i];
nums[i]=nums[i+1];
nums[i+1]=nums[i+3];
nums[i+3]=nums[i+4];
nums[i+4]=eHold;
}
public static void main(String[]args){
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Enter 4 numbers between 1 - 9");
double w,x,y,z;
Game Player1 = new Game();
w = input.nextDouble();
x = input.nextDouble();
y = input.nextDouble();
z = input.nextDouble();
input.close();
Player1.Solve(w,x,y,z);
}
}
Stringbuffer doesn't really let you delete content (well it does but not on a line by line basis) you're probably better off creating a Set in which you add your lines. Note, this is unordered which might not be ideal, but it will guarantee that you won't print the same string twice.