Write a menu driven C++ program for the kids games. Implement the following menu
ID: 3838113 • Letter: W
Question
Write a menu driven C++ program for the kids games. Implement the following menu to interact with the a kid. Do you want to play(y/Y) y Memory Puzzle Guess the missing letter Sort the words Exit A 4 times 4 board full of words. There is a pair for each word. The player select two cells each time. If they match, then they stay overturned. Otherwise, they flip back. There are16 words, the best case is to match all word correctly in eight moves, if player got 6-8 then he wins, otherwise he loses. You can use arrays (one dimensional array) as needed in order to build the board, The player will have the chance to see the whole board, then the screen should be cleared to start the game, Each time he selects the cells numbers, if the words match then the words will be displayed on the board, otherwise, the selected words will be shown to the player for a while then the player should enetr any character to clear the screen and display the board to continue the game.Explanation / Answer
Answer:
C++ code for the above problem is as follows:
#include<iostream>
#include<cstdlib>
#include<ctime> // library to use time related functions
using namespace std;
// The game is about matching the pair of words that are related The first word is a number
// and the second word is also a same number but at different position
int main()
{
std::string myarray[4][4]={{"one","eight","eight","six"},{"three","nine","one","six"},
{"seven","three","four","nine"},{"seven","two","four","two"}}; // initialize the 4*4 board
for(int i=0;i<4;i++) // for loop to display the board
{
for(int j=0;j<4;j++)
{
cout<<myarray[i][j]<<" ";
}
cout<<endl;
}
double elapsed_time=0;
clock_t t1=clock();
while(elapsed_time<5) // loop until the elapsed time is less than 5
{
clock_t t2=clock();
elapsed_time=(t2-t1)/CLOCKS_PER_SEC;
}
if(elapsed_time>=5) // display the board for 5 sec and then clear the screen
system("CLS");
int score=0; // initialize the score to zero
for(int k=0;k<8;k++) // repeat the game for 8 moves
{
int i1,j1,i2,j2;
cout<<"select two cells that match with each other"<<endl;
cout<<"enter first cell's horizantal and vertical postion"<<endl;
cin>>i1>>j1; // take input from the user i.e, first cell's horizantal and vertical postion
cout<<"enter second cell's horizantal and vertical postion"<<endl;
cin>>i2>>j2; // // take input from the user i.e, second cell's horizantal and vertical postion
if(myarray[i1][j1]==myarray[i2][j2]) // if the matched pair is correct
{
for(int i=0;i<4;i++) // display the selected words
{
for(int j=0;j<4;j++)
{
if(((i==i1) & (j==j1))||((i==i2) & (j==j2)))
{
cout<<myarray[i][j]<<" ";
}
}
cout<<endl;
}
score++; // score is increased by 1
}
else // if the matched words are incorrect
{
for(int i=0;i<4;i++) // display the selected words for a while
{
for(int j=0;j<4;j++)
{
if(((i==i1) & (j==j1))||((i==i2) & (j==j2)))
{
cout<<myarray[i][j]<<" ";
}
}
cout<<endl;
}
/*double elapsed_time=0;
clock_t t1=clock();
while(elapsed_time<3)
{
clock_t t2=clock();
elapsed_time=(t2-t1)/CLOCKS_PER_SEC;
}*/
char inp;
cout<<"Enter the character @ to clear the screen"<<endl;
cin>>inp;
if(inp=='@')
system("CLS"); // clear the screen if the user inputs the character @
}
cout<<"The score is "<<score<<endl; // display the score after each move
}
if(score==6) // if the score is 6 in 8 moves
{
cout<<"you won the game"<<endl; // print you won the game
}
else
{
cout<<"you won the game"<<endl; // print you lost the game
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.