View Full Version : A Little Competition
Tigman_1
10-10-2008, 07:10 PM
OK guys got a little something for you (No rewards but worth it...)
I have this game called 24, don't know if your familiar with it but it works like this:
You get a card with 4 numbers (eg. 4446) and your aim is to get those digits to 24 using add, subtract, multiply and division. You have to use your products to get there also eg.
4 divided by 4 = 1
1 times 4 = 4
4 times 6 = 24
The game is pretty simple. Another example for those of you who do not grasp the idea.
8581
8 - 5 = 3
3 X 8 = 24
24 X 1 = 24
simple. You may not use Squares or Cubes ect... (they do get harder...)
Anyway, so my challenge is to:
Create a program (using what ever program you like) so that you can insert any 4 digit numbers (1-9only) and it will come with an outcome telling you how you can reach 24 using the above rules. (Add, Sub, Dev, Mult).
The reason why I am posting this is because we play this game in my maths class (like 1nc a week), and I would like to own everyone at this game purely in a matter of time that is takes me to insert the digits into your program.
Please this would be a great amount of help to me, if you know of any such thing already please post, also if any questions please post and finally, if you are going to take part (in the free comp with no prizes) please upload it so that I can download it, and also anyone else interested ><
Thanks a lot
Tigman_1
Cloud_Ratha
10-10-2008, 07:12 PM
http://www.forteachersonly.com/images/productimages/standard/125.gif
:D ages 6 +
\o/
Azimuth
10-10-2008, 07:31 PM
So you want us to help you cheat, basically.
Fail.
Chippit
10-10-2008, 07:32 PM
I actually really liked that game. It was awesome.
Of course, we played so much that it eventually got to a point where we actually didn't need to work out the answers anymore; we'd seen all the cards so many times we knew all the solutions immediately. It was fun until then, though.
That said, should I be so inclined, I could probably make this in 5 minutes. But then the game wouldn't be fun anymore. I don't like breaking the fun.
Kensei
10-10-2008, 07:39 PM
It would be a fun exercise to create this though :)
i.e. as a training tool, you have say 2 minutes to figure it out - if you don't the AI wins and shows you the solution.
You could expand it so that the program generates new ones, or even extend it to include new combinations (i.e you have to make 36 instead of 24)
Tigman_1
10-10-2008, 07:46 PM
Kensei, I like that idea of yours.
Maybe to sections to it, 2 minute answer thing that you said and another one for adding in your own combs that checks it and if correct, adds it to its own library if it does not have it...
So I have No idea how to make this... any tips ?
Ps. gonna try game maker (anyone object?)
ShadowMaster
10-10-2008, 08:07 PM
Here's a good chance to learn to program yourself, it's not hard try it yourself.
We all have better things to do than make games for someone we don't really know and especially for someone who by the likes of it doesn't want to learn the art of game.dev .
Put we'll gladly guide you if the latter is not the case. :)
Tigman_1
10-10-2008, 11:39 PM
I agree 100%
OK first things first...(i am using G maker 6 pro) is there a code that allows you to type a number that will display at X and Y?
ie. that I want to type my numbers in...
Or should I make it by making 9 objects...And each object is a number (1-9) and when I press a number it appears at position X but if in colision with another number move 32 positions X...and so on...?so is that a possibility or what? (surly there must be an easier way)
Or (I think is better:)
I have one control and every time I hit a number (say 1-9) it creates that variable number as well as drawing it at position X, and if variable space occupied draw the text 32 X to ther right and so on....
Any suggestions?
Sorry, please bear with me
Kensei
11-10-2008, 12:36 AM
To be perfectly honest, this concept would work best with a textbased programming language. When it is working like that, then you can create it graphically in Game Maker.
Something that will let you first do the calculation of whether the 4 digits make 24 and return either true or false.
I am not 100% sure if there is an easier way but you could create a method that goes through the 4 numbers and sees if they create 24 at the end. This way is a bit of a brute force method though
Start simple, determine if the numbers first add up to 24
e.g.
Get the values
Store them as number1, number2, number3 and number4
Total = 24
compareTotal = number1 + number2 + number3 + number4
if Total = compareTotal then you have numbers that add up to 24
else you don't
It gets a bit more complex when you expand it to create different combination of numbers
Tigman_1
11-10-2008, 08:44 AM
So I have to manually make the input? That would be suicide, surely there is a code that can do that?
XERXES
11-10-2008, 08:57 AM
@Kensei
Cool, what programming languages do you know?
Kensei
11-10-2008, 11:49 AM
So I have to manually make the input? That would be suicide, surely there is a code that can do that?
Yes, it would. But it would be a start - so, first you create the code to test whether the 4 numbers make up 24, then when that is working, you can either:
- introduce a way to randomly generate those numbers and test that way (if that works, then you can draw them on the screen and get the user to try solve it)
- introduce a different operand.
My logic behind this would be:
1. Create code that makes sure the numbers can make 24
2. Expand it so you can test if they can be added, or subtracted or divided or multiplied, to create 24
3. Introduce a user interface to show how it worked it out
4. introduce a way for the user to calculate the total
This is quite a complex little program to create - so start small. Get a program that can tell you if the 4 numbers you put in add up to 24.
@Kensei
Cool, what programming languages do you know?
Erm, I know quite a few -_-* I am proficient in C#.Net and Java. But I am familar with VB (6 and .Net though I avoid them), PHP, Assembler, JavaScript, ASP.Net, C++, Delphi, Pascal and, obviously, GML (Game Maker Language)
I think that is it...
I 'should' get round to familiarizing myself with the XNA framework for C#.Net since it seems all the cool kids are using it, but yeah...
Tigman_1
11-10-2008, 12:58 PM
Ok cool, I'll take that advice, get back to you when I got that far...
Tigman_1
11-10-2008, 01:20 PM
Ok, my greatest problem:
how do I write a script that can generate 4 numbers that add to 24?
dislekcia
11-10-2008, 02:21 PM
Ok, my greatest problem:
how do I write a script that can generate 4 numbers that add to 24?
You would randomly generate numbers between 1 and some higher bound, probably not 24.
To do that in GM you can use the random(<higher bound>) function that will return real numbers though, so you'll probably also want to either round off those real parts using round(<number>) or truncate them using floor(<number>).
Tigman_1
11-10-2008, 05:44 PM
OK, I am now seriously confused (As usual)
this is what I have:
9 variables each named and valued (in the create event):
number1 = 1
number2 = 2
ect... the pattern goes on.
Now how can I say choose a random pick from the list of 9 numbers so that I can have 4 variables but they must add up to 24?
Kensei
11-10-2008, 06:05 PM
It would be easier to just have 4 variables.
i.e. number1 = random(upper_bound) etc
The only way I can figure how to make the numbers add up to 24 would be via a brute force method,
as I said earlier
Start simple, determine if the numbers first add up to 24
e.g.
Get the values
Store them as number1, number2, number3 and number4
Total = 24
compareTotal = number1 + number2 + number3 + number4
if Total = compareTotal then you have numbers that add up to 24
else you don't
It gets a bit more complex when you expand it to create different combination of numbers
dislekcia
11-10-2008, 06:15 PM
OK, I am now seriously confused (As usual)
this is what I have:
9 variables each named and valued (in the create event):
number1 = 1
number2 = 2
ect... the pattern goes on.
Now how can I say choose a random pick from the list of 9 numbers so that I can have 4 variables but they must add up to 24?
I don't know... How would you, as a human being, pick 4 random numbers that add up to 24? Would you randomly pick numbers and then see if they can somehow make 24 using all the possible mathematical combinations you have available? Would you pick one number and then pick another based on some sort of mathematical rule? What would you do? When you've got a method that you can do in your head, sit down and turn that into logic on the PC.
Also, I have no idea why you're creating those 9 variables. A variable is a place to store information, not a label. So given what you want to do, you'll probably only need 4 variables, one for each random number that you're testing as part of your combination.
Tigman_1
11-10-2008, 06:25 PM
ok thanks for the help...(helped a lot) but:
please explain what you mean by: random(upper_bound)
this is what I have: random(9)
now what I understand is that it will choose a number from 1-9...my second question...what can I add so that it rounds the number to the nearest number because I get random numbers but with decimals...
Tigman_1
11-10-2008, 06:30 PM
got it sorted... thanks so far
Tigman_1
11-10-2008, 07:01 PM
ok, i'm on a role, but one problem...
so far I have a number generator that generates 4 number (2-9) and I have discovered that they will always add up to 24 as long as you don't have 3 of the same numbers (eg. 5552)
so how do I say if variables 1 2 3 or 124 or 234 are equal, do a random swap again?
Tigman_1
11-10-2008, 07:50 PM
Ok, great news, I can make the numbers ALWAYS add up to 24, but now I need to make a script that can allow it to ALWAYS add up to 24 using /+-*
any suggestions?
ps. thinks about this:
if numbershown1+numbershown2+numbershown3+numbershown 4 != 24 { room_restart()}
*numbershown is my variable
can I say something like:
if numbershown1 +or-or*or/ numbershown2 +or-or*or/ numbershown3 ...?
in other words "permutation"
Agrajag
12-10-2008, 02:27 PM
I would write this whole thing in a text based system, it's much simpler for this sort of thing. I'm actually tempted to pull out Java and get going on this myself but school has me busy so I really shouldn't be on the computer at all.
In terms of using game maker, there are options there for getting in text input. I can't remember what it's called but it's a handy little thing that comes up and waits for the user to enter some text. If you can't find it on the tabs in the whole object window thing, try to find it using the help file. Game Maker has very good documentation.
I would get a solution by somehow having 3 nested loops to brute force the solution. The outermost loop is all to do with different ways to arrange your brackets. The next loop (inside of the brackets loop if you didn't know what I meant by saying that they are nested) is all to do with the order that your numbers appear in. The third loop nested inside the order loop changes which opperations are used, possibly by associating each operation with a constant (eg. add=0, subtract=1 etc). How long could it take? My maths says that there are only 67584 possible ways to do maths on those 4 numbers but it could probably be decreased by thinking through where repetition would take place.
Tigman_1
12-10-2008, 05:08 PM
ok...so im pretty muched doomed?
any other suggestions?
ShadowMaster
12-10-2008, 10:20 PM
I would write this whole thing in a text based system, it's much simpler for this sort of thing. I'm actually tempted to pull out Java and get going on this myself but school has me busy so I really shouldn't be on the computer at all.
In terms of using game maker, there are options there for getting in text input. I can't remember what it's called but it's a handy little thing that comes up and waits for the user to enter some text. If you can't find it on the tabs in the whole object window thing, try to find it using the help file. Game Maker has very good documentation.
I would get a solution by somehow having 3 nested loops to brute force the solution. The outermost loop is all to do with different ways to arrange your brackets. The next loop (inside of the brackets loop if you didn't know what I meant by saying that they are nested) is all to do with the order that your numbers appear in. The third loop nested inside the order loop changes which opperations are used, possibly by associating each operation with a constant (eg. add=0, subtract=1 etc). How long could it take? My maths says that there are only 67584 possible ways to do maths on those 4 numbers but it could probably be decreased by thinking through where repetition would take place.
Using post fix notation (mentally at least) I get there are 1536 solutions in total. There are 4! = 4*3*2*1 ways of ordering the numbers multiplied with the 4*4*4 = 64 possible orderings of the operators.
My idea is a bit simpler but to do it will take a tad of work, require an array and will result 7 nested for loops. But while it may sound complicated, it's dirt simple straight forward code.
Here is my pseudo code to check whether a given set of numbers has a solution:
//numbers is the array containing the numbers
numbers[0] = random(1, 10); //assuming random returns an int greater or equal to 1
//and less than ten, ie less than or equal to 9.
numbers[1] = random(1, 10);
numbers[2] = random(1, 10);
numbers[3] = random(1, 10);
has_solution = false;//whether an solution exists.
//the for loops that decide the ordering of the numbers
//the && !has_solution in every for loop is there because
//we only want to check if there is a solution, if we found one we can stop.
for (a = 0; a < 4 && !has_solution; a++)
{
for (b = 0; b < 4 && !has_solution; b++)
{
//check if b isn't the same as a, because we may not
//select the same number twice
if (b == a)
{
continue;//don't break because a might equal 1 then we
//would still have to check b = 2 and 3
}
for (c = 0; c < 4 && !has_solution; c++)
{
//check if c isn't the same as a and b, because we may
//not select the same number twice
if (c == a || c == b)
{
continue;
}
for (d = 0; d < 4 && !has_solution; d++)
{
//check if d isn't the same as a, b and c,
//because we may not select the same number twice
if (d == a || d == b || d == c)
{
continue;
}
//now the for loops to decide the operators
//we arbitarily assign the numbers
//0 to addition
//1 to subtraction
//2 to multiplication
//3 to division
for (i = 0; i < 4 && !has_solution; i++)
{
temp_val = 0;//this is an float
if (i == 0)
temp_val = numbers[a] + numbers[b];
else if (i == 1)
temp_val = numbers[a] - numbers[b];
else if (i == 2)
temp_val = numbers[a] * numbers[b];
else if (i == 3)
temp_val = numbers[a] / numbers[b];
for (j = 0; j < 4 && !has_solution; j++)
{
if (j == 0)
temp_val = temp_val + numbers[c];
else if (j == 1)
temp_val = temp_val - numbers[c];
else if (j == 2)
temp_val = temp_val * numbers[c];
else if (j == 3)
temp_val = temp_val / numbers[c];
for (k = 0;k < 4 && !has_solution; k++)
{
if (k == 0)
temp_val = temp_val + numbers[d];
else if (k == 1)
temp_val = temp_val - numbers[d];
else if (k == 2)
temp_val = temp_val * numbers[d];
else if (k == 3)
temp_val = temp_val / numbers[d];
if (temp_val == 24)
has_solution = true;
}
}
}
}
}
}
}
Kensei
12-10-2008, 11:24 PM
Nice, ShadowMaster :)
You could substitute some of the if statements with switch case statements
e.g, instead of
if (j == 0)
temp_val = temp_val + numbers[c];
else if (j == 1)
temp_val = temp_val - numbers[c];
else if (j == 2)
temp_val = temp_val * numbers[c];
else if (j == 3)
temp_val = temp_val / numbers[c];
You can have:
switch(j){
case(0): temp_val = temp_val + numbers[c];
case(1): temp_val = temp_val - numbers[c];
case(2): temp_val = temp_val * numbers[c];
case(3): temp_val = temp_val / numbers[c];
}
Agrajag
13-10-2008, 12:23 AM
Using post fix notation (mentally at least) I get there are 1536 solutions in total. There are 4! = 4*3*2*1 ways of ordering the numbers multiplied with the 4*4*4 = 64 possible orderings of the operators.
Ok, I did make a small maths error by multiplying by 4^4 instead of 4^3 for the operations but you have forgotten that the order that operations are carried out (which I like to denote with brackets) can also change the result. At least I think it should. I worked out that there are 11 different ways to organise the brackets.
Nice pseudocode by the way.
Oh, and Tigman, don't let the number of possibilities intimidate you. The point is to set up the loops so that the computer goes through all the possibilities systematically for you. Keep trying, you'll get it eventually.
dislekcia
13-10-2008, 01:11 AM
This sounds like a computer olympiad type of problem, designed to make you dive into one approach and miss a much easier and simpler implementation ;)
Why not go "backwards" from your desired final value? You know that you need to get 24, why not pick an operator and split your total into two numbers via that operation? The logic is astoundingly simple: Create a tree structure where nodes with children have an operator assigned to them and leaf nodes are simply values; Create logic to populate leaves on a node that's been assigned an operator (with a few edge-case if statements to make sure you don't go outside your allowed bounds on the final numbers); Set up a tree with 24 as a root node and let your logic loose.
I can guarantee it'll be done an order of magnitude faster than a brute-force approach ;)
ShadowMaster
13-10-2008, 07:05 AM
Ok, I did make a small maths error by multiplying by 4^4 instead of 4^3 for the operations but you have forgotten that the order that operations are carried out (which I like to denote with brackets) can also change the result. At least I think it should. I worked out that there are 11 different ways to organise the brackets..
With my approach there is no multiplication is carried out before addition etc, so the order of the operators doesn't matter. I do however simplify brackets by simply reordering the numbers.
This sounds like a computer olympiad type of problem, designed to make you dive into one approach and miss a much easier and simpler implementation ;)
Why not go "backwards" from your desired final value? You know that you need to get 24, why not pick an operator and split your total into two numbers via that operation? The logic is astoundingly simple: Create a tree structure where nodes with children have an operator assigned to them and leaf nodes are simply values; Create logic to populate leaves on a node that's been assigned an operator (with a few edge-case if statements to make sure you don't go outside your allowed bounds on the final numbers); Set up a tree with 24 as a root node and let your logic loose.
I can guarantee it'll be done an order of magnitude faster than a brute-force approach ;)
dislekcia, that is pretty, hardest part will be to make sure that each solution you give will be unique. Though I doubt if Tigman_1 can do it (prove me wrong ;) ).
The easiest solution in my opinion is work out all the possible problems (though you still need a method of checking whether the problem has a solution), work out some heuristic of ranking them and store them in a file. To minimize the number of problems you have to have you can only allow numbers that are sorted, ie 1, 2, 3, 4 is a valid problem, because it's sorted and has an answer, but 2, 1, 3, 4 is not because it's not sorted.
I'll calculate the number of possibilities today when I get home again but if I remember correctly it isn't more than a few thousand.
dislekcia
13-10-2008, 11:44 AM
dislekcia, that is pretty, hardest part will be to make sure that each solution you give will be unique. Though I doubt if Tigman_1 can do it (prove me wrong ;) ).
You simply use an element of random to ensure uniqueness, MathsterMind does something very similar in its random equation generation.
Remember, the simplest solution is just a recursive function that accepts 4 numbers and checks to see if any of their possible combinations reach 24. You're right about order switching being the easiest way to handle bracketing.
I think (but I'm not sure) that the 24 game doesn't have hundreds of cards because they eliminate sequences with only 1 possible method of reaching 24. I'd argue that you could even rank sequences by number of ways you could get to 24. Don't they do something like that with card colours? I would.
ShadowMaster
13-10-2008, 02:36 PM
You simply use an element of random to ensure uniqueness, MathsterMind does something very similar in its random equation generation.
Remember, the simplest solution is just a recursive function that accepts 4 numbers and checks to see if any of their possible combinations reach 24. You're right about order switching being the easiest way to handle bracketing.
I think (but I'm not sure) that the 24 game doesn't have hundreds of cards because they eliminate sequences with only 1 possible method of reaching 24. I'd argue that you could even rank sequences by number of ways you could get to 24. Don't they do something like that with card colours? I would.
Yeah I thought about randomization like that, but it's pretty hard to generate all numbers like that, but for random cards it's the best.
I have written a quick test program to find out how many possible cards there are (with or without solution), where the numbers are from 1 to 9 and got the answer 715. So the list of all problems will be at most that, because some may not have a solution.
I did that by generating all sequences of numbers a, b, c, and d and counting the number that satisfy the following: a <= b <= c <= d.
I think a good heuristic for checking difficulty is to somehow generate all solutions (my brute force would need some work to remove some duplicates for instance a+b+c+d and a+b+d+c).
Then I would give a weight to each solution because even if it only has one solution it may not necessarily be hard consider the case where the only solution is to sum all the numbers.
So I would give that a weight of say a hundred and some more interesting things a+c-d*b a weight of 10 or something (it would need proper balancing). Then I would would rank them according to the sum of the weights of the solutions, lower being harder. Then I would say between a certain range is hard, another range is medium and so on.
This may not be the best solution (it's an heuristic so it probably isn't) but it should work reasonably well.
What you could also do is to change the game to use fractions (which will take a bit of work, but won't change the base code) or change it to go to different numbers say Maths 32 or Maths 30. Which will also be simple even more so if you manage dis's approach.
Chippit
13-10-2008, 03:35 PM
I think (but I'm not sure) that the 24 game doesn't have hundreds of cards because they eliminate sequences with only 1 possible method of reaching 24. I'd argue that you could even rank sequences by number of ways you could get to 24. Don't they do something like that with card colours? I would.
As far as I recall, each card had a series of dots in the corners or edges that dictate its difficulty. Cards with two or three dots had more division/multiplication and fewer possible solutions compared to the 'easy' cards.
Tigman_1
13-10-2008, 05:37 PM
Using post fix notation (mentally at least) I get there are 1536 solutions in total. There are 4! = 4*3*2*1 ways of ordering the numbers multiplied with the 4*4*4 = 64 possible orderings of the operators.
My idea is a bit simpler but to do it will take a tad of work, require an array and will result 7 nested for loops. But while it may sound complicated, it's dirt simple straight forward code.
Here is my pseudo code to check whether a given set of numbers has a solution:
//numbers is the array containing the numbers
numbers[0] = random(1, 10); //assuming random returns an int greater or equal to 1
//and less than ten, ie less than or equal to 9.
numbers[1] = random(1, 10);
numbers[2] = random(1, 10);
numbers[3] = random(1, 10);
has_solution = false;//whether an solution exists.
//the for loops that decide the ordering of the numbers
//the && !has_solution in every for loop is there because
//we only want to check if there is a solution, if we found one we can stop.
for (a = 0; a < 4 && !has_solution; a++)
{
for (b = 0; b < 4 && !has_solution; b++)
{
//check if b isn't the same as a, because we may not
//select the same number twice
if (b == a)
{
continue;//don't break because a might equal 1 then we
//would still have to check b = 2 and 3
}
for (c = 0; c < 4 && !has_solution; c++)
{
//check if c isn't the same as a and b, because we may
//not select the same number twice
if (c == a || c == b)
{
continue;
}
for (d = 0; d < 4 && !has_solution; d++)
{
//check if d isn't the same as a, b and c,
//because we may not select the same number twice
if (d == a || d == b || d == c)
{
continue;
}
//now the for loops to decide the operators
//we arbitarily assign the numbers
//0 to addition
//1 to subtraction
//2 to multiplication
//3 to division
for (i = 0; i < 4 && !has_solution; i++)
{
temp_val = 0;//this is an float
if (i == 0)
temp_val = numbers[a] + numbers[b];
else if (i == 1)
temp_val = numbers[a] - numbers[b];
else if (i == 2)
temp_val = numbers[a] * numbers[b];
else if (i == 3)
temp_val = numbers[a] / numbers[b];
for (j = 0; j < 4 && !has_solution; j++)
{
if (j == 0)
temp_val = temp_val + numbers[c];
else if (j == 1)
temp_val = temp_val - numbers[c];
else if (j == 2)
temp_val = temp_val * numbers[c];
else if (j == 3)
temp_val = temp_val / numbers[c];
for (k = 0;k < 4 && !has_solution; k++)
{
if (k == 0)
temp_val = temp_val + numbers[d];
else if (k == 1)
temp_val = temp_val - numbers[d];
else if (k == 2)
temp_val = temp_val * numbers[d];
else if (k == 3)
temp_val = temp_val / numbers[d];
if (temp_val == 24)
has_solution = true;
}
}
}
}
}
}
}
Could you please explain this is more detail and what it means, *Please*
ShadowMaster
13-10-2008, 06:14 PM
The first 4 for loops are there to switch around the order of the number.
Consider the numbers being numbered from 0 to 3.
Consider
for (a = 0; a < 4 && !has_solution; a++)
for (b = 0; b < 4 && !has_solution; b++)
for (c = 0; c < 4 && !has_solution; c++)
for (d = 0; d < 4 && !has_solution; d++)
These decide which number to use where. a is the first number, b the second, c the third and d the forth.
Now these if statements prevent a number being chosen twice:
if (b == a)
if (c == a || c == b)
if (d == a || d == b || d == c)
Note that in the second and third if b==a isn't checked again, because it would have already been checked by the first one and the same for the checks in the second if there after.
So now you have the sequence of 4 numbers.
Now you have to check whether using them in that order will yield an answer, by using the operators in all possible combinations.
Which is where these 3 for loops come in:
for (i = 0; i < 4 && !has_solution; i++)
for (j = 0; j < 4 && !has_solution; j++)
for (k = 0;k < 4 && !has_solution; k++)
As I said in the comments the values of i, j and k have been arbitarily assigned to a mathematical operator.
In the first of the 3 for loops I create the var temp_val and just at this time I realise a slight mistake.
The code should actually be:
for (i = 0; i < 4 && !has_solution; i++)
{
temp_val = numbers[a];//this is an float
if (i == 0)
temp_val = temp_val + numbers[b];
else if (i == 1)
temp_val = temp_val - numbers[b];
else if (i == 2)
temp_val = temp_val * numbers[b];
else if (i == 3)
temp_val = temp_val / numbers[b];
for (j = 0; j < 4 && !has_solution; j++)
{
temp_val1 = temp_val;
if (j == 0)
temp_val1 = temp_val1 + numbers[c];
else if (j == 1)
temp_val1 = temp_val1 - numbers[c];
else if (j == 2)
temp_val1 = temp_val1 * numbers[c];
else if (j == 3)
temp_val1 = temp_val1 / numbers[c];
for (k = 0;k < 4 && !has_solution; k++)
{
temp_val2 = temp_val1;
if (k == 0)
temp_val2 = temp_val2 + numbers[d];
else if (k == 1)
temp_val2 = temp_val2 - numbers[d];
else if (k == 2)
temp_val2 = temp_val2 * numbers[d];
else if (k == 3)
temp_val2 = temp_val2 / numbers[d];
if (temp_val2 == 24)
has_solution = true;
}
}
}
Ok as I was saying in the top of each of the 3 for loops I create a temporary variable, to store the result of the previous for loop or in the case of the first chosen by using the value of a.
I have to use a temporary variable for each loop (in the loop itself) or I would end up having the results of the previous attempts by the nested for loops as my old version of code did.
Then using the following (slightly modified for each for loop) if statement tree:
if (i == 0)
temp_val = temp_val + numbers[b];
else if (i == 1)
temp_val = temp_val - numbers[b];
else if (i == 2)
temp_val = temp_val * numbers[b];
else if (i == 3)
temp_val = temp_val / numbers[b];
I decide which operator to use and apply it to the applicable temp_val and use the next number.
Then finally I use the following:
if (temp_val2 == 24)
has_solution = true;
To check whether the solution is correct and signal it.
Now the
&& !has_solution
in every for will cause them to stop running and the whole thing will stop running.
Now if you ran through all the possible potential solutions and never find one the has_solution will remain false and then you will have to pick different numbers.
I hope that that clears that up, if you have any more queries then just ask.
But in the future be a bit more specific.
Tigman_1
13-10-2008, 08:17 PM
Thanks so much, but if I put the code into GM it says at position 39 in this code:
for (a = 0; a < 4 && !has_solution; a++)
it expects an assignment operator, now I don't know where to ad one...
ShadowMaster
13-10-2008, 08:41 PM
Thanks so much, but if I put the code into GM it says at position 39 in this code:
for (a = 0; a < 4 && !has_solution; a++)
it expects an assignment operator, now I don't know where to ad one...
Well the code isn't really written in game maker script so you'll have to change parts of it.
The problem is Game Maker script doesn't have ++ or -- operators so you'll have to use -= and += or in code:
for (a = 0; a < 4 && !has_solution; a+=1)
change the "++" to " += 1", so something like this should work:
for (a = 0; a < 4 && !has_solution; a += 1)
If it still doesnt work you'll also have to change the && to just the word "and" to fix it. Hope this solves your problem.
Just a note, I recall it was pseudo code ShadowMaster gave so there are things that will have to be changed a little for it to work in Game Maker.
ShadowMaster
13-10-2008, 08:49 PM
GML accepts && and || too, but that's minor. If you want you can use the words "and" and "or" respectively. :)
Afflict
13-10-2008, 09:10 PM
So you want us to help you cheat, basically.
Fail.
ROFL, help would require him to do something. ;)
Anyway some 24 games Google found.
http://www.novelgames.com/flashgames/game.php?id=73
http://www.coolmath-games.com/arithmetic24/index.html
Thought it would help for inspiration :P
Anyway Kudos on trying to code it SM
Tigman_1
13-10-2008, 09:20 PM
all word except for this problem :( :
temp_val = temp_val / numbers[b];
at position 36 : division by 0
ShadowMaster
13-10-2008, 09:32 PM
Your random number function is returning zero, add one to it to prevent that and then make the max number equal to 8.
Tigman_1
13-10-2008, 10:14 PM
ShadowMaster, you are the master might I just say...
Thanks for all the help guys...
ps. (@ shadow, is there a way that I can display the way how the computer got to the answer? eg. what path it took to get to 24?)
Tigman_1
14-10-2008, 10:37 PM
Your random number function is returning zero, add one to it to prevent that and then make the max number equal to 8.
I have done that but no problem solved :(
ps. can you please give me your full script how i is meant to be ?
dislekcia
14-10-2008, 10:50 PM
ps. can you please give me your full script how i is meant to be ?
:|
Handouts don't help anyone. Think about it, you'll figure it out!
01DT!m3r
15-10-2008, 04:39 PM
:|
Handouts don't help anyone. Think about it, you'll figure it out!
Very wise words !
Tigman_1
15-10-2008, 06:02 PM
agreed...almost there but having a problem with my coding...
in my coding i have:
answer = get_string("What is your answer?",0)
if answer = 24 {show_message("working so far!")
Now my 1st problem is, it says answer is an unknown variable. (referring to if answer = 24)
I solved this by adding answer = 0 in the create event. Second problem arrives...at position 14 cannot compare arguments...(referring to: if answer = 24{show...line)
What I am trying to achieve is a box that pops up, you insert your answer (exactly how get_string works) , it checks what you wrote (but only allows numbers and mathematical terms (+-*/)) and if it adds up to a specific number, make x variable true/a number I want.
Am I going the right way about it? Any advice on solving the above?
Tigman_1
15-10-2008, 06:06 PM
Got IT!!!
so proud of myself :)
Tigman_1
15-10-2008, 06:10 PM
hit a stone :(
I converted answer into a real number...
answer = get_string("What is your answer?",0)
answer = real(answer)
if answer = 24 {show_message("working so far!")
but my problem is, if you type in say 1*2*3*4 it says:error in function real
is there a way to sort this out?
dislekcia
15-10-2008, 07:35 PM
hit a stone :(
I converted answer into a real number...
answer = get_string("What is your answer?",0)
answer = real(answer)
if answer = 24 {show_message("working so far!")
but my problem is, if you type in say 1*2*3*4 it says:error in function real
is there a way to sort this out?
You would have to parse through the string looking for mathematical operators and then perform those yourself. Rhe real function doesn't do maths, it just converts a string of numbers to their number counterparts: * isn't a number ;)
Tigman_1
15-10-2008, 07:48 PM
but how do I do that?
I know the idea behind the logic, if phrase contains a * or + etc...remove it from the phrase, after phrase has been converted, insert the *+_/() back to it's original position and then calculate the formula, but before doing so check that the 4 numbers in the equation are equivalent to the same numbers currently displayed...
so anyway...thats my plan...but how do I get GM to recognize *+-/() in a string?
dislekcia
15-10-2008, 07:56 PM
but how do I do that?
Well, there are a whole bunch of string commands in GM that let you pull out what's at specific points in a string or even search through it to find specific things. Those could help.
It might be easier to build an interface that lets people select a number, then an operator, then a number, operator, etc. That way you know what's coming so you don't have to pull it all apart.
Tigman_1
15-10-2008, 08:45 PM
I've searched everywhere for info on strings but have found so little that it is not even at all the slightest bit helpful :(. So seeing this is a good place to start....
say for example the persons input is 1*2*3*4
what I think may be helpful in my code s this: (got it off GM help)
string_delete(str,index,count) Returns a copy of str with the part removed that starts at position index and has length count.
Is the above the right one? If so, how would I incorporate it into my code ? eg. what index is.
If wrong, please supply me with a suitable command...and trust me, I sat forever trying to work it out, but I am 100% n0ob.
ps. @ dislekcia, sorry I don't like the interface idea :(, prefer the user typing in his/her code ...
Tigman_1
15-10-2008, 09:49 PM
@ shadowfire, your code is incorrect :(
sorry gotta start from the BEGINNING :(
dislekcia
16-10-2008, 03:19 AM
I've searched everywhere for info on strings but have found so little that it is not even at all the slightest bit helpful :(. So seeing this is a good place to start....
say for example the persons input is 1*2*3*4
what I think may be helpful in my code s this: (got it off GM help)
string_delete(str,index,count) Returns a copy of str with the part removed that starts at position index and has length count.
Is the above the right one? If so, how would I incorporate it into my code ? eg. what index is.
If wrong, please supply me with a suitable command...and trust me, I sat forever trying to work it out, but I am 100% n0ob.
ps. @ dislekcia, sorry I don't like the interface idea :(, prefer the user typing in his/her code ...
There is no "right function". There's a ton of combinations of functions that will give you the result you're after. The first step is to always think through what you would do, given a string like that, to turn it into a final sum.
Open up the GM help, go to index mode and type "string_" those are your tools to solve this problem with. Read up on them, then apply them.
01DT!m3r
16-10-2008, 06:14 AM
Tigman its like learning a new langauge . You cant write a book if you dont know the vocabulary. In gm those functions and commands (maybe even your own scripts ) are the vocabulary and the gm help file is your dictionary . Imo i think it would be easier to program this in delphi or VB but thats just my opinion.
Tigman_1
16-10-2008, 06:32 PM
Ok, this is it, I'm going solo, from the beginning till the end...i'll pitch up only if the world comes to an end!
wish me luck
tigman_1
(might post in like 20 minutes for help >< - but I am a man of my word, )
Tigman_1
16-10-2008, 06:36 PM
im back!!
ok, help:
im trying to make a variable = + or -
so for insance
z = +
2 z 2 = 4
how do I do that? is it possible?
dislekcia
16-10-2008, 06:58 PM
value = 0;
leftValue = <whatever number>;
rightValue = <whatever number>;
z = choose("+", "-", "*", "/");
switch (z)
{
case "+" : value = leftValue + rightValue;
break;
case "-" : value = leftValue - rightValue;
break;
case "*" : value = leftValue * rightValue;
break;
case "/" : value = leftValue / rightValue;
break;
}
//Now value is equal to what you were wanting...
So basically you have 4 different values for an operator and you test to see which one you're supposed to do and then do that in code.
Otherwise you could use an eval-type statement to calculate as though a variable were "code" but that's always messy and tricky to pull off.
Tigman_1
16-10-2008, 08:02 PM
O.K this is my code,
in the create event :
//Creating 4 random numbers:
a = round(random(7))+2;
b = round(random(7))+2;
c = round(random(7))+2;
d = round(random(7))+2;
// All the terms that need to be declared:
has_solution = false;
answer = 0;
brack = 0;
Term1 = 0;
Term2 = 0;
Term3 = 0;
In my step event:
//A little definition:
switch (Term1)
case 0: Term1 = +; break;
case 1: Term1 = -; break;
case 2: Term1 = *; break;
case 3: Term1 = /; break;
switch (Term2)
case 0: Term2 = +; break;
case 1: Term2 = -; break;
case 2: Term2 = *; break;
case 3: Term2 = /; break;
switch (Term3)
case 0: Term3 = +; break;
case 1: Term3 = -; break;
case 2: Term3 = *; break;
case 3: Term3 = /; break;
//Now for the code to get to 24:
if has_solution = false {
if brack = 0 {answer = (a Term1 b Term2 c Term3 d)};
if brack = 1 {answer = (a Term1 b) Term2 (c Term3 d)};
if brack = 2 {answer = (a Term1 b) Term2 c Term3 d};
if brack = 3 {answer = a Term1 b Term2 (c Term3 d)};
if brack = 4 {answer = (a Term1 b Term2 c) Term3 d};
if brack = 5 {answer = a Term1 (b Term2 c Term3 d)};
if brack = 6 {answer = a Term1 (b Term2 c) Term3 d};
if brack = 7 {answer = a Term1 ((b Term2 c) Term3 d)};
if brack = 8 {answer = a Term1 (b Term2 (c Term3 d))};
if brack = 9 {answer = (a Term1 (b Term2 c)) Term3 d};
if brack = 10 {answer = ((a Term1 b) Term2 c) Term3 d; if answer != 24 {room_restart()}};
if answer = 24 {has_solution = true;} else {Term1 +=1;}
if Term1 = 4 {Term1 = 0; Term2 +=1;}
if Term2 = 4 {Term2 = 0; Term3 +=1;}
if Term3 = 4 {Term3 = 0; brack +=1;}
}
Now can you see why I want to make variables = +/*-
Is there a simpler way to do that ie. if Term1 = 0 {Term1 = +;} litraley?
Help please*
remember I want to incorporate it by if Term1 = 0 {.....}
Tigman_1
17-10-2008, 07:20 PM
Any ideas? Anyone? - Sorry for posting twice ><
01DT!m3r
18-10-2008, 10:32 AM
I would go with dis's suggestion . To me that does exactly what i think you are asking .
Tigman_1
18-10-2008, 11:08 AM
no it doesn't, its excludes the fact that you can have brackets ect...but what I want is just to know how to make a variable = + or - etc. If I can do that them my problem is solved.
dislekcia
19-10-2008, 01:38 AM
no it doesn't, its excludes the fact that you can have brackets ect...but what I want is just to know how to make a variable = + or - etc. If I can do that them my problem is solved.
Brackets change the order that operations happen in, right? So if you have a series of loops that make sure EVERY possible combination of operators is checked, all you have to do is pass in the 4 numbers in every possible order of arrangement and you won't have to worry about brackets at all: You'll have done what they do anyway.
01DT!m3r
19-10-2008, 11:05 AM
Brackets change the order that operations happen in, right? So if you have a series of loops that make sure EVERY possible combination of operators is checked, all you have to do is pass in the 4 numbers in every possible order of arrangement and you won't have to worry about brackets at all: You'll have done what they do anyway.
Exactly . Whether u go this way or with brackets its still doing the same sum
Tigman_1
24-10-2008, 05:40 PM
OK this is what I have so far...
//the for loops that decide the ordering of the numbers
//the && !has_solution in every for loop is there because
//we only want to check if there is a solution, if we found one we can stop.
for (a = 0; a < 4 && !has_solution; a+=1)
{
for (b = 0; b < 4 && !has_solution; b+=1)
{
//check if b isn't the same as a, because we may not
//select the same number twice
if (b == a)
{
continue;//don't break because a might equal 1 then we
//would still have to check b = 2 and 3
}
for (c = 0; c < 4 && !has_solution; c+=1)
{
//check if c isn't the same as a and b, because we may
//not select the same number twice
if (c == a || c == b)
{
continue;
}
for (d = 0; d < 4 && !has_solution; d+=1)
{
//check if d isn't the same as a, b and c,
//because we may not select the same number twice
if (d == a || d == b || d == c)
{
continue;
}
//now the for loops to decide the operators
//we arbitarily assign the numbers
//0 to addition
//1 to subtraction
//2 to multiplication
//3 to division
for (i = 0; i < 4 && !has_solution; i+=1)
{
temp_val = 0;//this is an float
if (i == 0)
temp_val = numbers[a] + numbers[b];
else if (i == 1)
temp_val = numbers[a] - numbers[b];
else if (i == 2)
temp_val = numbers[a] * numbers[b];
else if (i == 3)
temp_val = numbers[a] / numbers[b];
for (j = 0; j < 4 && !has_solution; j+=1)
{
if (j == 0)
temp_val = temp_val + numbers[c];
else if (j == 1)
temp_val = temp_val - numbers[c];
else if (j == 2)
temp_val = temp_val * numbers[c];
else if (j == 3)
temp_val = temp_val / numbers[c];
for (k = 0;k < 4 && !has_solution; k+=1)
{
if (k == 0)
temp_val = temp_val + numbers[d];
else if (k == 1)
temp_val = temp_val - numbers[d];
else if (k == 2)
temp_val = temp_val * numbers[d];
else if (k == 3)
temp_val = temp_val / numbers[d];
if (temp_val == 24){
has_solution = true};
}
}
}
}
}
}
}
if has_solution = false{room_restart()}
if i = 0 {op1 = "+"}
if i = 1 {op1 = "-"}
if i = 2 {op1 = "*"}
if i = 3 {op1 = "/"}
if j = 0 {op2 = "+"}
if j = 1 {op2 = "-"}
if j = 2 {op2 = "*"}
if j = 3 {op2 = "/"}
if k = 0 {op3 = "+"}
if k = 1 {op3 = "-"}
if k = 2 {op3 = "*"}
if k = 3 {op3 = "/"}
What I am trying to achieve is, making a string = +/*- but use the correct one, but I am struggling with it...On what variable can I lock onto to tell me which operating sign is being used, if you get what I am saying...
dislekcia
25-10-2008, 03:00 AM
What I am trying to achieve is, making a string = +/*- but use the correct one, but I am struggling with it...On what variable can I lock onto to tell me which operating sign is being used, if you get what I am saying...
I don't understand... What do you mean by "lock on to"? And what do you want this string for? Am I understanding you correctly that you want to create a string that outputs for example "2+4*4=24" or something?
Tigman_1
25-10-2008, 09:15 AM
yes, I have my numbers drawn when the room starts, in my room I also have an "Answer" button. When that is pressed, The background falls away, I then (what I don't have currently) want the mathematical operators to appear, telling the player the solution.
I have done things like : if i = 0 {op1 = "+"}
etc.
but that just comes with a random operator and only like 1 in a 1000 are correct :(
So please, some help
dislekcia
25-10-2008, 08:10 PM
yes, I have my numbers drawn when the room starts, in my room I also have an "Answer" button. When that is pressed, The background falls away, I then (what I don't have currently) want the mathematical operators to appear, telling the player the solution.
I have done things like : if i = 0 {op1 = "+"}
etc.
but that just comes with a random operator and only like 1 in a 1000 are correct :(
So please, some help
Right.
The reason you're not getting useful info out of i, j or k is because they're constantly changed in the loops. What you need to do is set specific variables to the right operators when your testing shows that the answer works.
Tigman_1
25-10-2008, 11:43 PM
Now thats what I am confused about...
EDIT: A little guidence?
Tigman_1
27-10-2008, 07:41 PM
PLEASE somebody help me!
Basically, in your loops you'll want to have your check for when its 24 and then when you find a solution for 24 you have an ending string that has all the solutions listed, which you could just show at the end.
An example
//at the start of your code:
Endvar = "";
//in your loops:
if (temp_var == 24) {
has_solution = true;
Endvar += string(numbers[a]) + op1 + string(numbers[b]) + op2 + string(numbers[c]) + op3 + string(numbers[d]) + "=24 ";
}
Basically this will allow you to show the user all the solutions, and because everything is going in as a fixed length you can easily go through it in sections to see if the player has a working solution.
Although that means you need to move where you assign op1 op2 op3 and op4 to inside your loop to above my Endvar code in the if statement there, but then again, you dont even need the ending code if you assign op1 through op4 inside that if statement you will have a working solution every time, although it wont show every solution.
Tigman_1
28-10-2008, 12:53 PM
this is what I have so far:
//the for loops that decide the ordering of the numbers
//the && !has_solution in every for loop is there because
//we only want to check if there is a solution, if we found one we can stop.
for (a = 0; a < 4 && !has_solution; a+=1)
{
for (b = 0; b < 4 && !has_solution; b+=1)
{
//check if b isn't the same as a, because we may not
//select the same number twice
if (b == a)
{
continue;//don't break because a might equal 1 then we
//would still have to check b = 2 and 3
}
for (c = 0; c < 4 && !has_solution; c+=1)
{
//check if c isn't the same as a and b, because we may
//not select the same number twice
if (c == a || c == b)
{
continue;
}
for (d = 0; d < 4 && !has_solution; d+=1)
{
//check if d isn't the same as a, b and c,
//because we may not select the same number twice
if (d == a || d == b || d == c)
{
continue;
}
//now the for loops to decide the operators
//we arbitarily assign the numbers
//0 to addition
//1 to subtraction
//2 to multiplication
//3 to division
for (i = 0; i < 4 && !has_solution; i+=1)
{
temp_val = 0;//this is an float
if (i == 0)
temp_val = numbers[a] + numbers[b];
else if (i == 1)
temp_val = numbers[a] - numbers[b];
else if (i == 2)
temp_val = numbers[a] * numbers[b];
else if (i == 3)
temp_val = numbers[a] / numbers[b];
for (j = 0; j < 4 && !has_solution; j+=1)
{
if (j == 0)
temp_val = temp_val + numbers[c];
else if (j == 1)
temp_val = temp_val - numbers[c];
else if (j == 2)
temp_val = temp_val * numbers[c];
else if (j == 3)
temp_val = temp_val / numbers[c];
for (k = 0;k < 4 && !has_solution; k+=1)
{
if (k == 0)
temp_val = temp_val + numbers[d];
else if (k == 1)
temp_val = temp_val - numbers[d];
else if (k == 2)
temp_val = temp_val * numbers[d];
else if (k == 3)
temp_val = temp_val / numbers[d];
if (temp_val == 24){
has_solution = true;
if i = 0 {op1 = "+"}
if i = 1 {op1 = "-"}
if i = 2 {op1 = "x"}
if i = 3 {op1 = "/"}
if j = 0 {op2 = "+"}
if j = 1 {op2 = "-"}
if j = 2 {op2 = "x"}
if j = 3 {op2 = "/"}
if k = 0 {op3 = "+"}
if k = 1 {op3 = "-"}
if k = 2 {op3 = "x"}
if k = 3 {op3 = "/"}
Endvar += string(numbers[a]) + op1 + string(numbers[b]) + op2 + string(numbers[c]) + op3 + string(numbers[d]);
}
}
}
}
}
}
}
}
if has_solution = false{room_restart()}
Where would I have to place my op1/2/3?
this is what I have so far:
Where would I have to place my op1/2/3?
Could you elaborate a little please? I dont understand quite what the question is, but the way you have it written there now should do what you require. If you want I can write up an example in python quickly that you can use for testing and for comparison
Tigman_1
28-10-2008, 10:04 PM
OK, I have solved the plan!!!!I'm so proud, ps. would anyone like to download my game? I can provide a link... ?
Glad you have managed to do so. :)
If you give a link to your game people can help you test it, and possibly give you more expansion ideas. I wouldnt mind giving it a go either.
Tigman_1
29-10-2008, 10:26 PM
I'll get it on by Monday, just making it user friendly :)
Kensei
29-10-2008, 11:53 PM
OK, I have solved the plan!!!!I'm so proud, ps. would anyone like to download my game? I can provide a link... ?
I would love to :)
Unknownx
30-10-2008, 05:49 PM
I played that game yesterday, at school!!! it was a bit boring!
Tigman_1
30-10-2008, 10:31 PM
Have a quick question, is there a way to store a variable, but that does not get effected by room_restarts...?
Cause I want to say make bob = 0 but the say, bob += 1, then restart the room but I still want bob to = 1
Kensei
30-10-2008, 11:25 PM
Create a global object, in its create event declare the variable.
If you declare it as a global variable, e.g. 'global.bob' then you can use it in other places, such as your code (just call it as 'global.bob')
make sense?
Tigman_1
31-10-2008, 06:32 PM
Yebo Yes!
Here's the link: http://www.filefactory.com/file/e8c137/n/NAG_exe
It'd be great with some comments :)
EDIT: Use the Basic download
Yebo Yes!
Here's the link: http://www.filefactory.com/file/e8c137/n/NAG_exe
It'd be great with some comments :)
EDIT: Use the Basic download
I do believe you have an infinite loop somewhere. It locks up one whole core when I try open it.
Tigman_1
31-10-2008, 10:57 PM
Nope, there's no infinite loops, my pc handles it quite well and easily
Nope, there's no infinite loops, my pc handles it quite well and easily
I'll let it run for a while and see if it works.
EDIT: ah, I assumed you were using Game Maker 7. Just ran the convertor so I could use it on vista. It looks good, just one issue I have is the music, is it from TF2?
Tigman_1
31-10-2008, 11:26 PM
ye lol, by the way I was using GM6, PS. I got new music now
ye lol, by the way I was using GM6, PS. I got new music now
Ok cool, its good to know you dont have TF2 music (copyrighted stuff) anymore. I like that you can use any number you chose to be the target, but lets face it 9^4 is 6561, you cant go higher and find a solution for higher numbers without exposnents can you?
Tigman_1
31-10-2008, 11:59 PM
ye, I know I saw that problem like a hour ago...and fixed that, thanks for the heads up anyway
EDIT: the tf2 music was for experimental purposes in early stages
Powered by vBulletin® Version 4.2.4 Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.