Question

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. 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:...
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: 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....

  • Please program this in Visual Basic 6. You have chosen to create an electronic version of the sliding tile puzzle game....

    Please program this in Visual Basic 6. You have chosen to create an electronic version of the sliding tile puzzle game. The object of the game is to slide the tiles so that they end up in the required order. The images shown below are examples of the two different versions of this puzzle (numeric and graphical) Puzzle Board-Numeric Puzzle Board-Graphical File Options Help Elapsed Time File Options Help Elapsed Time 00:02:12 00:04:20 5 6 7 8 9 10 11...

  • 2 A Game of UNO You are to develop an interactive game of UNO between a...

    2 A Game of UNO You are to develop an interactive game of UNO between a number of players. The gameplay for UNO is described at https://www.unorules.com/. Your program should operate as follows. 2.1 Setup 1. UNO cards are represented as variables of the following type: typedef struct card_s { char suit[7]; int value; char action[15]; struct card_s *pt; } card; You are allowed to add attributes to this definition, but not to remove any. You can represent colors by...

  • This program should be in c++. Rock Paper Scissors: This game is played by children and...

    This program should be in c++. Rock Paper Scissors: This game is played by children and adults and is popular all over the world. Apart from being a game played to pass time, the game is usually played in situations where something has to be chosen. It is similar in that way to other games like flipping the coin, throwing dice or drawing straws. There is no room for cheating or for knowing what the other person is going to...

  • 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...

  • "Blackjack 40" my teacher created this assignment and I need help. He is using c++ and...

    "Blackjack 40" my teacher created this assignment and I need help. He is using c++ and has provided this template: Further instructions are below the template! #include <iostream> #include <cmath> #include <cstdlib> using namespace std; //Will return a number from 1 to 13, representing the face of a card int draw_card() { return rand() % 13 + 1; } int main() { const int BET = 10; //This will allow you to control chance, to make testing easier cout <<...

  • C++ Project - Create a memory game in c++ using structs and pointers. For this exercise,...

    C++ Project - Create a memory game in c++ using structs and pointers. For this exercise, you will create a simple version of the Memory Game. You will again be working with multiple functions and arrays. You will be using pointers for your arrays and you must use a struct to store the move and pass it to functions as needed. Program Design You may want to create two different 4x4 arrays. One to store the symbols the player is...

  • 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 /*...

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