Hello there!
I'm a C begginer and I'm writing a Math 24 Game clone in C. http://en.wikipedia.org/wiki/Math_24
The goal of this game is to manipulate 4 random numbers into a result of 24 using addition, subtraction, multiplication or division. For example, the numbers 6, 6, 12 and 3. One possible solution is: 12/6 = 2, 2 + 6 = 8, 8*3 = 24. Numbers have to be used one time only.
My version of the game works like this: 4 integer numbers between 1 and 10 are generated, then the player introduces the solution and the program checks if the solution is right or wrong.
A very important thing is that the 4 generated numbers are not impossible to manipulate into 24. So after the numbers are generated, the program runs a function to see if there are any solutions. This is where the problem lies. I opted for a very simple way but not very effective way to solve this.
It goes like this:
Code:
int a, b, c, d;
a = rand() % 10 + 1;
b = rand() % 10 + 1;
c = rand() % 10 + 1;
d = rand() % 10 + 1;
if (a + b + c + d == 24)
printf("Solution: %d + %d + %d + %d\n", a, b, c, d);
if (a + b + c - d == 24)
printf("Solution: %d + %d + %d - %d\n", a, b, c, d);
if (a + b + c * d == 24)
printf("Solution: %d + %d + %d * %d\n", a, b, c, d);
if (a + b + c / d == 24)
printf("Solution: %d + %d + %d / %d\n", a, b, c, d);
if (a + b - c + d == 24)
printf("Solution: %d + %d - %d + %d\n", a, b, c, d);
//etc..... all the combinations of +-/*. 4 ^ 3 = 64
The problem is that sometimes the result of some conditions is 24.1, 24.2, 24.3 or other, and the if statement assumes it is correct and prints those numbers and operations. I just want it to printf when the condition is exactly 24.
How can I solve this?
Thanks for reading.