/* Victor Chen
11-2-09
COP 3223 Section 3
Creates a game from a choice of 3: addition, multiplication, factoring
and calculates a score based on time spent on the problems and whether the user
got the question right or wrong
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ADD 1
#define MULT 2
// enabling functions to be called
double arithGame(int max, int quantity, int op);
double factorGame(int max, int quantity);
int numPoints(double timesec);
int menu();
int main(){
int choice, ans;
int e,f,g;
int x, y;
int points = 0;
srand(time(0));
//prints menu
ans = menu();
// loops for the correct choice
while(ans!=4){
// if user selects Arithmetic
if(ans == 1){
printf("Would you like, 1)Addition or 2)Multiplication?\n");
scanf("%d", &g);
printf("What is the maximum number you would like?\n");
scanf("%d", &e);
printf("How many problems do you want?\n");
scanf("%d", &f);
int start = time(0);
points= points+arithGame(e,f,g); // returns arithmetic game and calculates points
}
// if user selects to do Factorial game
if(ans == 2){
printf("Enter the maximum value of a factor.\n");
scanf("%d", &x);
printf("How many factoring problems do you want?\n");
scanf("%d", &y);
points = points+factorGame(x,y); // returns factorial game and calculates points
}
// if user requests score
if(ans == 3){
printf("Your score is %d.\n", points);
}
ans = menu();
}
printf("Thank you for playing!\n");
system("PAUSE");
return 0;
}
// Precondition: none
//Postcondition: Prints out the menu
int menu(){
int num = 0;
while (num!=1 && num!= 2 && num!=3 && num!=4){
printf("Please make a selection from the following:\n");
printf("1. Play Arithmetic Game.\n");
printf("2. Play Factoring Game.\n");
printf("3. Print Score.\n");
printf("4. Quit.\n");
scanf("%d", &num);
if (num!=1 && num!=2 && num!=3 && num!=4)
printf("Sorry, your choice is invalid, try again.\n");
}
return num;
}
//Precondition: User enters 1
//Postcondition:
double arithGame(int max, int quantity, int op)
{
int index, value1, value2, response;
int penalty = 0, score = 0;
int start = time(0);
// If user choosea Arithmetic Addition
if(op==ADD)
{
for (index = 0; index<quantity; index++)
{
value1 = rand()%max+1;
value2 = rand()%max+1;
printf("What is %d + %d?\n", value1, value2);
scanf("%d", &response);
if (response == value1+value2)
{
printf("Correct, great job!\n");
}
else if (response != value1+value2)
{
printf("Sorry, that's incorrect, the answer is %d.\n", value1+value2);
penalty=penalty+5;
}
}
}
// User chooses Arithmetic Multiplication
if(op==MULT)
{
for(index = 0; index<quantity; index++)
{
value1= rand()%max+1;
value2= rand()%max+1;
printf("What is %d x %d?\n", value1, value2);
scanf("%d", &response);
// response is equal to the 2 values multiplied
if(response == value1*value2)
{
printf("Correct, great job!\n");
}
// value not equal to the 2 values multiplied and adds a penalty to the score
else if (response != value1*value2)
{
printf("Sorry, that's incorrect, the answer is %d.\n", value1*value2);
penalty=penalty+5;
}
}
}
int end = time(0);
int timespent1 = end - start;
int timespent2 = (timespent1+penalty)/quantity;
score = numPoints(timespent2);
printf("You took an average of %d seconds.\n", timespent1);
printf("Your score for the round is %d.\n", score);
return score;
}
//Precondition: User chooses 2, gives
//Postcondition
double factorGame(int max, int quantity)
{
int index, value1, value2, ans1, ans2, number;
int count=0;
int penalty=0;
int score;
int countmeter[quantity];//array for storing the number of tries for each question
int start = time(0);
for(index = 0; index<quantity; index++)
{
countmeter[index]=0; // number of problems to store into array
value1 = rand()%max+3;
value2 = rand()%max+3;
number = value1*value2;
printf("Please factor %d.\n", number);
scanf("%d %d", &ans1, &ans2);
count = 0; // starts the count for attempts of problems
// if answers multiplied do not equal the number at all
// adds 5 second penalty for every wrong answer to the final time score
while(ans1*ans2 != number)
{
printf("Sorry %d x %d does not equal %d.\n", ans1, ans2, number);
count++;
scanf("%d %d", &ans1, &ans2);
penalty = penalty+5;
}
// If answers entered are not the same as the values that equal the number
// adds 2 second penalty to the end time score
if ((ans1!=value1)&&(ans2!=value2))
{
count++;
printf("Sorry that is not the factorization we are looking for.\n");
scanf("%d %d", &ans1, &ans2);
penalty = penalty +2;
}
// correct answer
if((ans1==value1) &&(ans2==value2))
{
printf("Correct, great job!\n");
count++;
printf("You got the factorization in %d guesses.\n", count);
}
}
countmeter[index]=count;
//calculates the timespent playing and also the score to return
int end = time(0);
int timespent1 = end - start;
int timespent2 = (timespent1+penalty)/quantity;
score = numPoints(timespent2);
printf("You took an average of %d seconds.\n", timespent1);
printf("Your score for the round is %d.\n", score);
return score;
}
//Precondition: User gives a value from calculations of time spent in a game.
//Postcondition: Returns a value 1-10 dependant on the users score for the last game.
int numPoints(double timesec){
if(timesec<1)
return 10;
else if((timesec>= 1)&&(timesec<2))
return 9;
else if((timesec>=2) && (timesec<3))
return 8;
else if((timesec>=3) && (timesec<4))
return 7;
else if((timesec>=4) && (timesec<5))
return 6;
else if((timesec>=5) && (timesec<6))
return 5;
else if((timesec>=6) && (timesec<7))
return 4;
else if ((timesec>=7) && (timesec<8))
return 3;
else if((timesec>=8) && (timesec<9))
return 2;
else if((timesec>=9) && (timesec<10))
return 1;
else if(timesec>=10)
return 0;
}