Code:
//ASSIGNMENT 1, TENNIS GAME
// MORE FUNCTIONS TO BE ADDED AS PROGRAM PROGRESSES 19/08/07 Version
#include <stdio.h>
#define PRINT 0
#define DEUCE 1
int scoreA = 0; //Declaration of global variables
int scoreB = 0;
int gameA = 0;
int gameB = 0;
int scoreFunct(char c, char team, int gameA, int gameB); // THIS FUNCTION DOES THE SCORE CALCULATION
void printFunct(int scoreA, int scoreB, char c, char team, int gameA, int gameB); //THIS FUNCTION DOES THE MAJOR PRINTING(SCORES,GAMES etc.)
int main (int argc, char **argv){
char team;
char c;
int cnt = 0;//counting variable for while loop
while(((team = getchar()) != EOF) && (cnt < 1)){ //This loop here ends after 1 letter is detected
if((team == 'A') || (team == 'a') || (team == 'B') || (team == 'b')){
scoreFunct(c,team,gameA,gameB);
}
cnt++;
}
return(0);
}
//THIS IS THE SCORING FUNCTION
int scoreFunct(char c, char team, int gameA, int gameB){
while((c = getchar()) != EOF){
if (c == 'S'){
printFunct(scoreA,scoreB,c,team,gameA,gameB);
return(PRINT);
}
else if(c == 'B'){
if(scoreB < 30){
scoreB+=15;
}
else if(scoreB == 30){
scoreB+=10;
}
else if(scoreB == 40){
if(team == 65){
gameB = gameB + 1;
scoreB = scoreB - scoreB; //Since scoreB is a global variable, we need to reset it to 0 after the game to start the new score for next game
scoreA = scoreA - scoreA;
team = 65 + 1;
}
else if(team == 66){
gameB = gameB + 1;
scoreB = scoreB - scoreB;
scoreA = scoreA - scoreA;
team = 66 - 1;
}
}
}
else if(c == 'A'){
if(scoreA < 30){
scoreA+=15;
}
else if(scoreA == 30){
scoreA+=10;
}
else if(scoreA == 40){
if(team == 65){
gameA = gameA + 1;
scoreA = scoreA - scoreA; //Since scoreA is a global variable, we need to reset it to 0 after the game to start the new score for next game
scoreB = scoreB - scoreB;
team = 65 + 1;
}
else if(team == 66){
gameA = gameA + 1;
scoreA = scoreA - scoreA;
scoreB = scoreB - scoreB;
team = 66 - 1;
}
}
}
}
return(0);
}
//THIS IS THE PRINTING FUNCTION
void printFunct(int scoreA, int scoreB, char c, char team, int gameA, int gameB){
if(scoreFunct(c,team,gameA,gameB) == PRINT){
if((scoreA == 0) && (scoreB == 0)){
printf("Team %c to serve:\n", team);
printf("%d-%d\n",gameA,gameB);
printf("Love-All\n");
printf("\n");
}
else if((scoreA < 40) && (scoreB < 40)){
if(team == 65){
printf("Team %c to serve:\n", team);
printf("%d-%d\n",gameA,gameB);
printf("%d-%d\n", scoreA, scoreB);
printf("\n");
}
else if(team == 66){
printf("Team %c to serve:\n", team);
printf("%d-%d\n",gameA,gameB);
printf("%d-%d\n", scoreB, scoreA);
printf("\n");
}
}
else if((scoreA == 40) || (scoreB == 40)){
if((scoreA == 40) && (scoreB == 40)){
printf("Team %c to serve:\n", team);
printf("%d-%d\n",gameA,gameB);
printf("DEUCE\n");
printf("\n");
}
else{
if(team == 65){
printf("Team %c to serve:\n", team);
printf("%d-%d\n",gameA,gameB);
printf("%d-%d\n", scoreA, scoreB);
printf("\n");
}
else if(team == 66){
printf("Team %c to serve:\n", team);
printf("%d-%d\n",gameA,gameB);
printf("%d-%d\n", scoreB, scoreA);
printf("\n");
}
}
}
else if((scoreA == 0) && (scoreB > 0)){
if(team == 65){
printf("Team %c to serve:\n", team);
printf("%d-%d\n",gameA,gameB);
printf("Love-%d\n",scoreB);
printf("\n");
}
else if(team == 66){
printf("Team %c to serve:\n", team);
printf("%d-%d\n",gameA,gameB);
printf("%d-Love\n",scoreB);
printf("\n");
}
}
else if((scoreA > 0) && (scoreB == 0)){
if(team == 65){
printf("Team %c to serve:\n", team);
printf("%d-%d\n",gameA,gameB);
printf("%d-Love\n",scoreA);
printf("\n");
}
else if(team == 66){
printf("Team %c to serve:\n", team);
printf("%d-%d\n",gameA,gameB);
printf("Love-%d\n",scoreA);
printf("\n");
}
}
}
}