Thread: 24 Math Game clone

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    11

    24 Math Game clone

    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.
    Last edited by no_one_knows; 06-22-2008 at 04:44 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Remember that 8/3 is 2, not 2.6666666666666666666666666666666666666666666. That's most likely where your problem lies.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You need to use doubles for a, b, c, and d instead of int. Also, because of the switch to double, you will need to check that the result lies within a certain error of 24, since manipulation of doubles can result in rounding errors, and you want to account for them.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    Thanks!

    Changed the variables to double and instead of 24 put 24.00 and it works great. Just had to change the %f to %.0f to make it more presentable.

    Thanks a lot, I will continue with the work now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# - Building a SCUMM-like game.. questions
    By Iyouboushi in forum Game Programming
    Replies: 0
    Last Post: 05-24-2008, 10:54 PM
  2. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  3. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 07:58 PM
  4. Game Programmer's AIM Circle: Join Today
    By KingZoolerius66 in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 12-20-2003, 12:12 PM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM