Thread: 24 game problem

  1. #1
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373

    24 game problem

    Okay, this isn't really a game.

    I had a math problem, and i wanted to make a program where you enter 4 numbers, and the program finds what combinations can be used without exponents or grouping signs and would equal 24.

    I have the adding/subtractions part working, just a few small problems with grouping, but i dont want to use if else if the whole time. I addded the mulitplication and division, just to have it break out of the loop.

    Is there an algorithim i could use for this, or a certain command? Thanks.
    This war, like the next war, is a war to end war.

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I'm not sure if I quite follow what you want it to do, but couldn't you use recursion?

    Do you mean the user enters in 4 numbers like "12 1 9 5" or like "5613"? And if you mean the second one, then would your program count "5+6+13"?

  3. #3
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    if you enter 1 2 3 4, i want the program to make a random equation, test it, and displayt the equation if it equals 24, else, repeat. After it fails a bunch of times, say "cannot get ot 24"
    This war, like the next war, is a war to end war.

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    so the user can only input four 1 digit numbers?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Then I would say just use recursion, that way it is easily changed to any amount of numbers you want to test. Something like this:

    Code:
    void recursion(vector<int> nums, int spot, int max, int currentTotal, int target)
    {
          currentTotal+=nums[spot];
          if spot==max
               if currentTotal==target then add one to your counter
          else recursion one farther
          currentTotal-=nums[spot]; 
    
          currentTotal-=nums[spot];
          etc etc
    }
    // edit although now that I think about it, for multiplication and division you'll only do that operation with the previous number, not the whole current total. I'd suggest doing similar to above, but instead adding the operator and the number to a string instead of add/subtract/etc with an int. Then when you hit the max recursion depth, you have a function parse the string to figure out what the total is.

  6. #6
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    Originally posted by neandrake
    so the user can only input four 1 digit numbers?
    No, they can be big multi-digit numbers. but im doing a for loop on an int array of four digits, so it runs like this:

    -----------------
    Enter 4 numbers: 12 34 15 18
    You entered:
    1: 12
    2: 34
    3: 15
    4: 18
    ------------------

    As for recursion, vectors dont mean anything to me, i use them to find out how many units are in the magnitude of a right triangle.

    All i was really wanting was to put 12 34 15 18 into a random equation, solve it, if it equals twenty four, then output that equation.

    I can do it, but i'd have to use a lot of if else if statements.
    This war, like the next war, is a war to end war.

  7. #7
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Vectors are nothing more than smarter arrays really. Just substitute in an array and it would work the same. The benefit of using recursion this way is that it will test all possibilities, not just some.

  8. #8
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    Recursion... tests all? okay. but how does that work on making an equation, solving it, and outputting the equation?

    On short, throw me some code that does that
    This war, like the next war, is a war to end war.

  9. #9
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Okay, I'll show you a simple example which you'd have to update to accomodate the bigger numbers for your program. Say you have a function called
    Code:
    solve(string s)
    which takes a string like "1+3*4-9" and is able to parse and figure out what that is equal to, and if you want maybe print out the equation if it equals a certain number. Then your recursion would look something like this (i haven't tested it though!):
    Code:
    int main()
    {
       ....
       char nums[4]={'1','3','4','9'};
       string s="";
    
       recursion(nums, 0, 3, s);
       ...
    }
    
    void recursion(char nums[], int spot, int max, string s)
    {
       s+=nums[spot];
       if (spot==max)
       {
             solve(s);
             return;
       }
    
       s+='+';
       recursion(nums, spot+1, max, s);
       s[s.length()-1]='-';
       recursion(nums, spot+1, max, s);
       s[s.length()-1]='*';
       recursion(nums, spot+1, max, s);
       s[s.length()-1]='/';
       recursion(nums, spot+1, max, s);
    }
    There are ways to optimize this better, just a quick idea. Its basically equivalent to four nested for loops running through all of the operator possibilities in between each pair of numbers and solving it in the innermost for loop. The reason I chose recursion is that it can easily be done to say ten numbers instead of only four without having to change anything.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Here's my ideas on how to solve it

    Code:
    In pseudo-code, it looks something like this
    
    
    // prototype
    bool solve ( int target, int values[], int numvals );
    
    // The initial call would be
    int nums[] = { 1, 2, 3, 4 };
    bool result = solve( 24, nums, 4 );
    
    The basic idea is that if you have
    1 + (solve("2,3,4")) == 24
    You can rearrange that into
    solve(24-1,"2,3,4")
    
    That is to say, knowing that adding 1 to the result will
    make 24, you can solve the recursive step for 24-1
    
    Similarly, if you had
    2 * (solve("1,3,4") == 24
    You would invoke
    solve(24/2,"1,3,4")
    
    At each recursive level you try
    - 4 operators
    - each element of the array
    
    Eventually, you end up with a simple case where the function is called
    with a single number and a single target value.  If they match, you
    have an answer to the problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    but what is the code behind the declaration? I know how i would use it, but how would i code it to actually solve?

    Right now i am using a class, called class Array, and its working quite well, it works for the following:

    Addition,
    Subtraction,
    Addition & Subtraction,
    Multiplication,
    Division,
    Multiplication & Divison
    Multiplication & Addition
    Multiplication & Subtraction
    Division & Addition
    and
    Division & Subtraction

    Works wonders for these. But im using if else if statements, and i grow tired of entering
    Code:
    cout << n[1] << " + " << n[2] << " + " << n[3] << " + " << n[4] << " = 24\n";
    if (n[1] + n[2] + n[3] + n[4] == 24)
    {
     cout << "Correct\n";
     getche();
    }
    else
     cout << "Incorrect\n";
    see what i mean?

    Shouldn't there be something in math.h or something for this problem?

    And one more thing you should know: I suck at programming, haha.

    Just check out my best work at http://blizzarddog.tk and get TDOD and TDOD Music Pack from the projects->released page:
    Thats my best... And yes, based on if else if statements, a bunch of switch case statements and voids.

    Ill upload the much better version later though.

    Any how... I've included the source file for the 24 game thingy.
    This war, like the next war, is a war to end war.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Linking problem
    By Agent89 in forum C++ Programming
    Replies: 4
    Last Post: 03-27-2005, 03:03 PM
  4. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM
  5. Problem with a game im making
    By TheGr8one in forum Game Programming
    Replies: 2
    Last Post: 10-19-2001, 07:38 PM