Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(in c++) Hi, I need help with writing the function ShowCards following , with proper explanation, comments and code correction .Here is the assignment and

(in c++) Hi, I need help with writing the function ShowCards following , with proper explanation, comments and code correction .Here is the assignment and my code below :

#include #include #include #include #include const int LENGTH = 4; using namespace std; // Function prototypes void InitializeCards(int cards[][LENGTH]); void ShowCards(int cards[][LENGTH], bool faceup[][LENGTH]); // you may have more function prototypes // ====================== // main function // ====================== int main() {

// 1) Welcome the user and ask him to enter his full name. string fullName; cout

// 2) Create a two 2D array that will hold the number for // each card.Pass it to // InitializeCards function. int cardHolder[LENGTH][LENGTH]; InitializeCards(cardHolder);

// 3) Create the 2D array of Booleans that represents //whether or not the card is matched. // This should be initialized to all FALSE at first. bool isMatch[LENGTH][LENGTH]; for (int i = 0; i > x1 >> y1; cout > x2 >> y2; attempts++; if (cardHolder[x1][y1] == cardHolder[x2][y2]) { cout > choice; if (choice == 'q') gameOver = true; } ofstream fout; fout.open("file.txt"); if (!fout) { cout

else { cout

// InitializeCards // Places pairs of numbers in the 2D array and then // randomly shuffles them. // ====================== //Notice that when you pass a 2D-array to a function, you MUST // specify the number of columns void InitializeCards(int cards[][LENGTH]) { rand(); srand(time(NULL)); int x1, y1, x2, y2; int i; int temp; // Place pairs in known locations - this is how our array // looks like after cards[0][0] = 1; cards[0][1] = 1;// 1 1 2 2 cards[0][2] = 2; cards[0][3] = 2; cards[1][0] = 3; cards[1][1] = 3;// 3 3 4 4 cards[1][2] = 4; cards[1][3] = 4; cards[2][0] = 5; cards[2][1] = 5;// 5 5 6 6 cards[2][2] = 6; cards[2][3] = 6; cards[3][0] = 7; cards[3][1] = 7;// 7 7 8 8 cards[3][2] = 8; cards[3][3] = 8; // Now that 2D has been initialized with 8 pairs, we want to //shuffle the board so // that the pairs aren't right next to each other for (int i = 0; i

image text in transcribedimage text in transcribed image text in transcribed

THE FLOW OF OUR PROGRAM 1) Welcome the user and ask him to enter his full name. 2) Create 2D array of values. First put everything in order and shuffle the location of the elements. This step is given. 3) Create the 2D array of Booleans that represents whether or not the card is matched. This should be initialized to all FALSE at first. Why? Because no cards are matched initially! 4) Start a while loop that does the following a. Display the current state of the game board. Remember that non-matched pairs should be "facedown" and that matched pairs should be "faceup" b. Prompt the user to enter the coordinates of two cards c. If the values of the two coordinates match, then say that a match has been found and flip the cards over forever. If the values do not match, temporarily flip over the cards to show the user the values, then flip them over again. 5) Save the user's name and how many turns it took to win the game into an output text file. If he quits before winning save "Don't give up" message in the file. A template for the assignment is provided. You must not delete any code there. You might delete the commetns. You must have at least three functions: void InitializeCards(int cards [][LENGTH]); void ShowCards(int cards[][LENGTH], bool faceup[][LENGTH]); int main () you may add more functions if you like. Submit the .cpp file only. Snippets of a sample run: Welcome to my game! Please enter your full name: abeer alameer Find all the matching pairs on the board! 0123 Enter x and y position of the first card: 01 Enter x and y position of the second card: 22 No match. Flipping cards over again. Enter ' q ' to quit or press any key to continue... We will be creating a program that simulates a simple memory game. In this memory game, a grid of face down cards is created. Every element of the grid has a matching pair. The user's goal is to find the location of all pairs. For example, For every iteration of a loop, the grid is displayed, and the user is prompted to enter two coordinates; x and y. where x is for row and y is for column. For example, the coordinates could be (row, column) and start at 0 . Once the coordinates are entered, the two cards at the corresponding coordinates will be compared to each other 1) If the cards match each other, the user has found a pair, so we keep them flipped over 2) If the cards do not match, we temporarily show the user the values of the cards at the coordinates, and then we flip the cards over again This loop should continue until the user finds all the pairs, or until they choose to quit. OUR APPROACH One "difficult" part of this lab is figuring out how to keep track of what cards have been found and what cards haven't. Remember that matched pairs need to be displayed, while all other pairs should be hidden. For example, So how to we keep track of what numbers to display and what numbers to put stars over? The approach used in the program below is to maintain two separate 2D arrays: one will hold the actual numerical values and the other will hold booleans that represent whether or not the value has been matched. Think back to parallel 1D arrays. This is a similar approach

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions