In previous articles, we created functions, multi-dimensional character array to store questions, options, and answers. Up to this point, we have everything ready to finish gameplay.
Here we will use the following techniques:
Here we will use the following techniques:
- Create for loop to display question one by one
- Create another infinite loop inside for loop to check the answer given by the user. The loop breaks only if the user-provided an answer is within options (a, b, c and d) otherwise it will loop infinitely.
- Calculate the score for the correct answer provided.
- Use gotoxy to manage text indentation.
void gamePlay(){
system("cls");
char question[10][250];
strcpy(question[0], "This is question no 1");
strcpy(question[1], "This is question no 2");
strcpy(question[2], "This is question no 3");
strcpy(question[3], "This is question no 4");
strcpy(question[4], "This is question no 5");
strcpy(question[5], "This is question no 6");
strcpy(question[6], "This is question no 7");
strcpy(question[7], "This is question no 8");
strcpy(question[8], "This is question no 9");
strcpy(question[9], "This is question no 10");
char option[10][4][50] = {
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
};
char answer[10]={'a','b','c','d','a','b','c','d','a','b'};
char letter[4]={'a','b','c','d'};
char ans;
int score=0;
for(int i=0;i<10;i++){
system("cls");
gotoxy(20,8);
printf("%s\n",question[i]);
for(int j=0;j<4;j++){
if(j==0 || j==2){
gotoxy(20,10 + j);
printf("%c) %s\n",letter[j],option[i][j]);
} else {
gotoxy(55,9 + j);
printf("%c) %s\n",letter[j],option[i][j]);
}
}
do{
gotoxy(22,15);
printf("Please Choose Answer using key a|b|c|d: ");
ans=tolower(getch());
if(ans=='a'||ans=='b'||ans=='c'||ans=='d')
break;
}while(1==1);
if(ans==answer[i]){
gotoxy(22,15);
printf("Correct Answer!!PRESS ANY KEY TO Continue....");
score++;
getch();
}else{
gotoxy(22,15);
printf("Wrong Answer!!PRESS ANY KEY TO Continue....");
getch();
}
}
system("cls");
gotoxy(22,8);
printf("You have score %d Out of 10.PRESS ANY KEY...",score);
getch();
mainMenu();
}
Explaining the code:
We will declare two variables "ans" and "score" to store the answer provided by the user and to store the total score.
char ans;
int score=0;
And two nested for loop is used to display answers and options. First for loop statement loops 10 times and another nested for loop statement loops 4 times so in total there will be 40 loops. Inside first for loop another infinite do-while loop is created to waits user input and validation. Users can provide any value outside the provided options. This scenario is handled using an infinite do-while loop it only breaks if and only the user provides the correct value.
do{
gotoxy(22,15);
printf("Please Choose Answer using key a or b or c or d: ");
ans=tolower(getch());
if(ans=='a'||ans=='b'||ans=='c'||ans=='d')
break;
}while(1==1);
Finally, the if-else statement is used to compare the answer provided by the user and correct answer stored in character array variables "answer".
if(ans==answer[i]){
gotoxy(22,15);
printf("Correct Answer!! PRESS ANY KEY TO Continue....");
score++;
getch();
}else{
gotoxy(22,15);
printf("Wrong Answer!! PRESS ANY KEY TO Continue....");
getch();
}
After completing the game, the final score is displayed and we will call mainMenu() function to direct the program to the main menu screen where the user can choose to play again or quit the game.
mainMenu();
No comments:
Post a Comment