What is needed? Write a menu driven C++ program for the kids games. Implement th
ID: 3838619 • Letter: W
Question
What is needed? Write a menu driven C++ program for the kids games. Implement the following menu to interact with the a kid. C:Windowssystem32cmd.exe Do you want to play(y/Y) y Memory Puzzle Guess the missing letter Sort the words Exit Implementation details: Option: Memory Puzzle A 4 times 4 board full of words. There is a pair for each word. The players select two cells each time. If they match, then they stay overturned. Otherwise, they flip back. There are 16 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 he displayed on the board, otherwise, the selected words will be shown to the player for a while then the player should center any character to clear the screen and display the board to continue the game.Explanation / Answer
Below is the code for puzzle option. Please don't forget to rate the answer if it helped.
Kindly note, if you are using windows, you can directly use system("cls") in clear() function. Since the platform is not known, I have tried both the calls.
The player wins if he finds 6 or more pairs in 8 moves.
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
void clear()
{
if(system("cls")!=0)
system("clear");
}
void pause()
{
cout<<"press enter to continue..."<<endl;
if(getchar()!=' ')
getchar();
}
//initialize the 16 positions in the 4x4 grid with 8 pairs of words
void initialize(string board[4][4], string words[8])
{
int r,c;
srand(time(NULL));
for(int i=0; i<8; i++) //for each word
{
for(int j=0; j<2;) // to be done 2 times
{
r = rand() % 4; //generate a random row and col and try to fill it if its empty
c = rand() % 4;
if(board[r][c].empty())
{
board[r][c] = words[i];
j++; //increment j only if we filled a empty spot, otherwise continue gnerating anohter random spot
}
}
}
}
//displays the board
void display(string board[4][4])
{
for(int i=0; i<4; i++)
{
cout<<" ";
for(int j=0; j<4; j++)
{
cout<<" ["<<setw(8)<<left<<board[i][j]<<"]";
}
}
cout<<endl<<endl;
}
void puzzle_menu()
{
string solved[4][4],puzzle[4][4];
string words[8]={"car","bat","ball","pen","hat","apple","ring","bird"};
int found=0, moves=0;
int r1, c1, r2, c2;
initialize(puzzle,words); //this is the actual puzzle generated
cout<<"The puzzle board is as follows... please memorize the positions ...."<<endl;
display(puzzle);
pause();
clear();
//solved is teh variable representing the solved puzzle so far . initially all spots are blanks in solved
while(moves!=8)
{
display(solved);
cout<<" Enter row col for Cell1 : ";
cin>>r1>>c1; //user will enter row value in range 1-4 and col also a number in 1-4
cout<<" Enter row col for Cell2 : ";
cin>>r2>>c2;
//grab the values from the actual puzzle and put in solved. display solved to user
solved[r1-1][c1-1] = puzzle[r1-1][c1-1];
solved[r2-1][c2-1] = puzzle[r2-1][c2-1];
display(solved);
cin.ignore(); //flush newline
pause();
if(solved[r1-1][c1-1] == solved[r2-1][c2-1]) //if the 2 values uncovered are same
{
found++;
}
else //the 2 values are not same, clear their positions in solved
{
solved[r1-1][c1-1] = "";
solved[r2-1][c2-1] = "";
}
moves++;
clear();
}
display(solved);
cout<<"You found "<<found<<" pairs in "<<moves<<" moves. ";
if(found >=6 ) //finding 6 or more pairs in 8 moves is winner
cout<<"You win!"<<endl;
else
cout<<"You loose!"<<endl;
}
int main()
{
puzzle_menu();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.