Question

Problem Develop a simple C++ memory puzzle game. The description of the game is as follows: A board has 4 by 4 overturn...

Problem
Develop a simple C++ memory puzzle game. The description of the game is as follows:
A board has 4 by 4 overturned cards. There is a pair for each card. The player flips over two cards. If they match, then they stay overturned. Otherwise they flip back after 3 seconds. The player needs to overturn all the cards in the fewest moves to win.
This is a console game. Use the alphabetical letters from A to H as cards. Whenever the game starts, the cards are randomly shuffled. The sample output looks like:
XXXX
XXXX
XXXX
XXXX
Choose two cards (or 'q' to quit the game): 0 0 1 3
AXXX XXXB XXXX XXXX
XXXX
XXXX
XXXX
XXXX
Choose two cards (or 'q' to quit the game): 0 0 3 1
AXXX
XXXX
XXXX
XAXX
Choose two cards (or 'q' to quit the game): 2 1 0 2
AXHX XXXX XEXX XAXX
AXXX
XXXX
XXXX
XAXX
Choose two cards (or 'q' to quit the game): 3 3 1 0
......
ACHE CGBB HEDF FADG
Congratulations! You solved the puzzle in 21 moves.

The four numbers that the player chooses are the coordinates of the first and second cards.
When two cards do not match, they show their contents for 3 seconds and then are flipped back to X’s (the back sides of the cards). In order to use this 3-second timer, refer to the following example:
COMP1230 – Assignment/Lab Exercises – page 2
#include "stdafx.h"
#include <iostream>
#include <thread>
using namespace std;
int main()
{
for (int i = 0; i < 60; i++)
{
this_thread::sleep_for(1s);
cout << i << endl;
}
}
In order to clear the console screen, use system("CLS");
The player can quit the game anytime by typing “q”.
When the game is over, hold the console screen using system("pause");
In order to get the screenshot of the game progress, comment out system("CLS");




o Program Design (UML diagram(s) or pseudo code)

• A zip file of an entire Visual Studio C++ project folder including (.cpp & .h) and executable file (.exe).


0 0
Add a comment Improve this question Transcribed image text
Answer #1

Unfortunately, I do not have Visual Studio but I will provide you with a working c++ code that you can run either in Linux or any windows IDE. Also please forgive me but I have not used the sample code given by you to stop the execution time for some seconds if the selected cards do not match. I will post some screenshots of my console output and I think the game works in the same way and the output is also produced in the same manner as suggested in the question. I have not deviated from it. My code will contain all the required and important documentation so any explanation over here I think would not be much necessary. Here goes the code (with necessary documentation) and the output screenshots after that.

#include <bits/stdc++.h> // imports all the necessary header files

#define ll long long int // all data types are in 64bit just for being in the safe side.

using namespace std;

vector <char> v;

int main()
{
   /*
       here i am storing all the characters from A to H in a vector twice because
       you need two of them and then i shuffled them randomly and also created a
       puzzle matrix and a visited matrix. The puzzle matrix stores the randomly
       shuffed vector of characters and visited marks the set of cards that are already
       the same and the player has selected. That is vis[i1][j1] = true and vis[i2][j2] = true
       denotes that the player has found out that the cards at (i1, j1) and (i2, j2) are the
       same.
   */
   for(char ch = 'A'; ch <= 'H'; ch++)
   {
       v.push_back(ch);
       v.push_back(ch);
   }
   srand(time(0));
   random_shuffle(v.begin(), v.end());
   ll sz = v.size();
   char puzzle[4][4];
   ll vis[4][4];
   for(ll i = 0; i < 4; i++)
       for(ll j = 0; j < 4; j++)
           vis[i][j] = false;
   ll r = -1, c = 0;
   // here i am transferring the values from the vector v to the puzzle matrix.
   for(ll i = 0; i < sz; i++)
   {
       if(i % 4 == 0)
       {
           r++;
           c = 0;
       }
       puzzle[r][c] = v[i];
       c++;
   }
   // game starts from here
   for(ll i = 0; i < 4; i++)
   {
       for(ll j = 0; j < 4; j++)
           cout << "X";
       cout << endl;
   }
   ll moves = 0;
   while(1)
   {
       cout << "Choose two cards (or 'q' to quit the game):";
       string inp;
       getline(cin, inp); // space separeted input is taken in the form a b c d.
       if(inp == "q") // if only "q" appears quit the game.
       {
           cout << "you have quitted the game" << endl;
           break;
       }
       moves++;
       ll x1 = inp[0] - '0'; // zeroth element is the first row
       ll y1 = inp[2] - '0'; // second element is the first column
       ll x2 = inp[4] - '0'; // fourth element is the second row
       ll y2 = inp[6] - '0'; // sixth element is the second column
       // 0 2 4 6 because it matches the input like (a_b_c_d) -> (0123456)
       if(puzzle[x1][y1] == puzzle[x2][y2]) // if the selected coordinates has the same cards
       {
           vis[x1][y1] = true;
           vis[x2][y2] = true; // mark them as true
           for(ll i = 0; i < 4; i++)
           {
               for(ll j = 0; j < 4; j++)
               {
                   if(vis[i][j])
                       cout << puzzle[i][j]; // if it is visited means the player has already discovered it
                   else
                       cout << "X"; // else it is undiscovered still.
               }
               cout << endl;
               // And print it!!
           }
       }
       else
       {
           // if the coordinates does not match
           for(ll i = 0; i < 4; i++)
           {
               for(ll j = 0; j < 4; j++)
               {
                   if(vis[i][j] || (i == x1 && j == y1) || (i == x2 && j == y2))
                       cout << puzzle[i][j]; // we see if it has been discovered or is the current index
                   else
                       cout << "X"; // else undiscovered
               }
               cout << " ";
           }
           cout << endl;
           for(ll i = 0; i < 4; i++)
           {
               for(ll j = 0; j < 4; j++)
               {
                   if(vis[i][j])
                       cout << puzzle[i][j];
                   else
                       cout << "X";
               }
               cout << endl;
           }
           // Just Normal printing after a round of the game.
       }
       bool ok = true;
       for(ll i = 0; i < 4; i++)
           for(ll j = 0; j < 4; j++)
               if(vis[i][j] == false)
                   ok = false;
       // Now if the entire vis matrix has been marked true means the player
       // has discovered all the cards and he has won the game and we exit the game.
       if(ok)
       {
           cout << "Congratulations! You solved the puzzle in " << moves << " moves.";
           break;
       }
   }
   return 0;  
}

- Terminal File Edit View Search Terminal Help Choose two cards (or q to quit the game) :0 1 2 BXXX XXCX XXXX XXXX cout <<p

I hope I explained the code clearly and the output is in the form suitable towards you. However if any kind of doubt still arises from your side regarding the code or the output, just leave a comment down below and I will be more than happy to help you out any time. Just ask me and I am there to help. If you think I did justice to your question give it a thumbs up. It means a lot. Thank you.

Add a comment
Know the answer?
Add Answer to:
Problem Develop a simple C++ memory puzzle game. The description of the game is as follows: A board has 4 by 4 overturn...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem Develop a simple C++ memory puzzle game. The description of the game is as follows:...

    Problem Develop a simple C++ memory puzzle game. The description of the game is as follows: A board has 4 by 4 overturned cards. There is a pair for each card. The player flips over two cards. If they match, then they stay overturned. Otherwise they flip back after 3 seconds. The player needs to overturn all the cards in the fewest moves to win. This is a console game. Use the alphabetical letters from A to H as cards....

  • For your Project, you will develop a simple battleship game. Battleship is a guessing game for...

    For your Project, you will develop a simple battleship game. Battleship is a guessing game for two players. It is played on four grids. Two grids (one for each player) are used to mark each players' fleets of ships (including battleships). The locations of the fleet (these first two grids) are concealed from the other player so that they do not know the locations of the opponent’s ships. Players alternate turns by ‘firing torpedoes’ at the other player's ships. The...

  • Project 2 – Memory Match Game Purpose This Windows Classic Desktop application plays a simple matching game. The game si...

    Project 2 – Memory Match Game Purpose This Windows Classic Desktop application plays a simple matching game. The game simulates a card game where the cards a placed face down and the player flips over pairs of cards in an attempt to find matching cards.   Program Procedure Display a 4x4 grid of “face down” cards. Assign the letters A through H randomly to the cards in pairs. Allow the user to click on a card to “flip” it over and...

  • you will implement the A* algorithm to solve the sliding tile puzzle game. Your goal is...

    you will implement the A* algorithm to solve the sliding tile puzzle game. Your goal is to return the instructions for solving the puzzle and show the configuration after each move. A majority of the code is written, I need help computing 3 functions in the PuzzleState class from the source code I provided below (see where comments ""TODO"" are). Also is this for Artificial Intelligence Requirements You are to create a program in Python 3 that performs the following:...

  • Missing multiple labeled functions. Card matching game in C. Shouldn't need any more functions. I am...

    Missing multiple labeled functions. Card matching game in C. Shouldn't need any more functions. I am lost on how to complete the main function (play_card_match) without the sub functions complete. The program runs fine as is, but does not actually have a working turn system. It should display question marks on unflipped cards when the player is taking a turn and should also clear the screen so the player can't scroll up to cheat. Thank you #include "cardMatch.h" //main function /*...

  • can someone solve this program using c++ (visual studio)? You have a grid of 4x4 cells...

    can someone solve this program using c++ (visual studio)? You have a grid of 4x4 cells which is filled by numbers from 1 to 8. Each number appears twice in the grid. The purpose of the Memory Game is to find the matching numbers. To start the game, Player 1 chooses two cells to uncover the numbers behind them. If the numbers match, player 1 gets to go on and uncover two more numbers. If the numbers don't match, player...

  • Please help me with this project by using Android Studio. I need to know the xml...

    Please help me with this project by using Android Studio. I need to know the xml and the java activities in both activities. Also the manifest activity. I appreciate the effort and I'll rate you with 5 stars. 1. SUMMARY This project will simulate the card game Concentration for one player. In Concentration, all cards, or pictures in this case, are upside down. Two cards are chosen, and if they match they are taken out of the game. If they...

  • The game lets players know whether they have won, lost, or tied a particular game. Modify...

    The game lets players know whether they have won, lost, or tied a particular game. Modify the game so the players are told the number of games they have won, lost or tied since the start of game play. Implement this change by adding three variables named $wins, $lost, and $ties to the program's Main Script Logic section, assigning each an initial value of 0. Next, modify the analyze_results method by adding programming logic that increments the values of $wins,...

  • I need help finding the error in my code for my Fill in the Blank (Making a Player Class) in C++. Everything looks good...

    I need help finding the error in my code for my Fill in the Blank (Making a Player Class) in C++. Everything looks good to me but it will not run. Please help... due in 24 hr. Problem As you write in your code, be sure to use appropriate comments to describe your work. After you have finished, test the code by compiling it and running the program, then turn in your finished source code. Currently, there is a test...

  • Please to indent and follow structure!!!!! Assignment 3 - The card game: War Due Date: June...

    Please to indent and follow structure!!!!! Assignment 3 - The card game: War Due Date: June 9th, 2018 @ 23:55 Percentage overall grade: 5% Penalties: No late assignments allowed Maximum Marks: 10 Pedagogical Goal: Refresher of Python and hands-on experience with algorithm coding, input validation, exceptions, file reading, Queues, and data structures with encapsulation. The card game War is a card game that is played with a deck of 52 cards. The goal is to be the first player to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT