Thread: Help with math game program.

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    24

    Help with math game program.

    I am developing a program for a class in which different levels have different types of basic arithmetic using the 4 operands, +, -, *, and /.

    here is my code for the game:

    Code:
    int start_game(int difficulty, int score){
    	int integer1=0, integer2=0, integer3=0, integer4=0, operand1 = -1, operand2 = -1, operand3 = -1, check, counter = -1;
    	double real_ans, user_ans;
    	char char_op1, char_op2, char_op3;
    	do
    	{
    	system("cls");
    	switch(difficulty)
    	{
    	case 1:
    			integer1 = (rand()%9)+1;
    			integer2 = (rand()%9)+1;
    			integer3 = (rand()%9)+1;
    			operand1 = (rand()%2);
    			operand2 = (rand()%2);
    			if(operand1 == 0)
    			{
    				char_op1 = '+';
    			}else{
    				char_op1 = '-';
    			}
    			if(operand2 == 0)
    			{
    				char_op2 = '+';
    			}else{
    				char_op2 = '-';
    			}
    			printf("Equation %d: %d %c %d %c %d\n\n", (counter+2), integer1, char_op1, integer2, char_op2, integer3);
    			break;
    	case 2:
    			integer1 = (rand()%9)+1;
    			integer2 = (rand()%9)+1;
    			operand1 = (rand()%2)+2;
    			if(operand1 == 2)//*
    			{
    				char_op1 = 'x';
    			}else{
    				char_op1 = '/';
    			}
    			printf("Equation %d: %d %c %d\n\n", (counter+2), integer1, char_op1, integer2);
    			break;
    	case 3:
    			integer1 = (rand()%9)+1;
    			integer2 = (rand()%9)+1;
    			integer3 = (rand()%9)+1;
    			operand1 = (rand()%4);
    			operand2 = (rand()%4);
    			switch(operand1)
    			{
    			case 0:
    				char_op1 = '+';
    				break;
    			case 1:
    				char_op1 = '-';
    				break;
    			case 2:
    				char_op1 = 'x';
    				break;
    			case 3:
    				char_op1 = '/';
    				break;
    			}
    			switch(operand2)
    			{
    			case 0:
    				char_op2 = '+';
    				break;
    			case 1:
    				char_op2 = '-';
    				break;
    			case 2:
    				char_op2 = 'x';
    				break;
    			case 3:
    				char_op2 = '/';
    				break;
    			}
    			printf("Equation %d: %d %c %d %c %d\n\n", counter, integer1, char_op1, integer2, char_op2, integer3);
    			break;
    	case 4:
    			integer1 = (rand()%9)+1;
    			integer2 = (rand()%99)+1;
    			integer3 = (rand()%9)+1;
    			operand1 = (rand()%4);
    			operand2 = (rand()%4);
    			switch(operand1)
    			{
    			case 0:
    				char_op1 = '+';
    				break;
    			case 1:
    				char_op1 = '-';
    				break;
    			case 2:
    				char_op1 = 'x';
    				break;
    			case 3:
    				char_op1 = '/';
    				break;
    			}
    			switch(operand2)
    			{
    			case 0:
    				char_op2 = '+';
    				break;
    			case 1:
    				char_op2 = '-';
    				break;
    			case 2:
    				char_op2 = 'x';
    				break;
    			case 3:
    				char_op2 = '/';
    				break;
    			}
    			printf("Equation %d: %d %c %d %c %d\n\n", (counter+2), integer1, char_op1, integer2, char_op2, integer3);
    			break;
    	case 5:
    			integer1 = (rand()%999)+1;
    			integer2 = (rand()%9)+1;
    			integer3 = (rand()%9)+1;
    			integer4 = (rand()%99)+1;
    			operand1 = (rand()%4);
    			operand2 = (rand()%4);
    			operand3 = (rand()%4);
    			switch(operand1)
    			{
    			case 0:
    				char_op1 = '+';
    				break;
    			case 1:
    				char_op1 = '-';
    				break;
    			case 2:
    				char_op1 = 'x';
    				break;
    			case 3:
    				char_op1 = '/';
    				break;
    			}
    			switch(operand2)
    			{
    			case 0:
    				char_op2 = '+';
    				break;
    			case 1:
    				char_op2 = '-';
    				break;
    			case 2:
    				char_op2 = 'x';
    				break;
    			case 3:
    				char_op2 = '/';
    				break;
    			}
    			switch(operand3)
    			{
    			case 0:
    				char_op3 = '+';
    				break;
    			case 1:
    				char_op3 = '-';
    				break;
    			case 2:
    				char_op3 = 'x';
    				break;
    			case 3:
    				char_op3 = '/';
    				break;
    			}
    			printf("Equation %d: %d %c %d %c %d %c %d\n\n", (counter+2), integer1, char_op1, integer2, char_op2, integer3, char_op3, integer4);
    			break;
    			
    	}
    	//printf("%c", char_op1);
    
    
    	real_ans = get_equation(integer1, integer2, integer3, integer4, operand1, operand2, operand3, difficulty);
    	
    	//printf("%lf\n", real_ans);
    	//printf("%d\n%d\n", operand1, operand2);
    	
    	user_ans = get_answer();
    	check = compare_numbers(real_ans, user_ans);
    	if(check == 1)
    	{
    	printf("Good Job! Try another\n");
    		switch(difficulty)
    	{
    		case 1:
    			score +=10;
    			break;
    		case 2:
    			score += 50;
    			break;
    		case 3:
    			score += 200;
    			break;
    		case 4:
    			score += 1000;
    			break;
    		case 5:
    			score += 5000;
    			break;
    	}
    	getch();
    	}else{
    		score -= 50;
    	printf("Wrong answer, the answer is %.2lf\n", real_ans);
    	getch();
    	}
    	i++;
    	}while(counter<10);
    	return score;
    }
    This code is the main code for the game with other functions around it. However, I cannot figure out how to fine the right answer for the equations that are randomly generated. Does anyone know how I can go about using the appropriate order of operations to do this. For example: say my code randomly generates the equation y = 7 + 2 * 8. How can I tell the computer to compute the 2*8 before the 7+2 but be general enough to compute an equation like 7 / 2 * 8. See what I mean?
    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int doowop( int a, int b, int op ) /* * < / < + < - */
    {
       ...
    }
    ...
    if( op1 < op2 )
        answer = doowop( doowop( a, b, op1 ) c, op2 )
    else
        answer = doowop( a, doowop( b, c, op2 ), op1 );
    That should work.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Programming general questions... and Math?
    By MsJordan in forum Game Programming
    Replies: 15
    Last Post: 11-06-2010, 07:46 PM
  2. simple game, need help with loops and math
    By joeman in forum C++ Programming
    Replies: 5
    Last Post: 02-08-2010, 07:47 AM
  3. 24 Math Game clone
    By no_one_knows in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:18 PM
  4. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM
  5. Game math (distance increments)
    By jdinger in forum Game Programming
    Replies: 6
    Last Post: 03-25-2002, 03:47 PM