Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with this program, I have started the program and I am stuck.I canno

ID: 3757660 • Letter: I

Question

I need help with this program, I have started the program and I am stuck.I cannot use functions as a learning exercise, becuase I will need to do the same problem afterwards using functions.

Children often play a memory game in which a deck of cards containing matching pairs is used. The cards are shuffled and placed face down on a table. The players then take turns and select two cards at a time. If both cards match, they are left face up; otherwise, the cards are placed face down at the same positions. Once the players see the selected pair of cards and if the cards do not match, then they can memorize the cards and use their memory to select the next pair of cards. The game continues until all the cards are face up.Write a program play the memory game. Use a two - dimensional array of 4 rows and 4 columns to use a group of 16 cards with 8 matching pairs. You can use number 1 to 8 to mark the cards. Use random number genrators to randomly store the pairs in the array. I am posting the code i have started with, however I am not entirely confident with what I have is correct. #include #include #include #include using namespace std; int main(){ const int ROWSIZE = 4; const int COLSIZE = 4; int i = 0; int j = 0; int x = 0; int k = 0; int p = 0; int m = 0; int n = 0; int cardFront[ROWSIZE][COLSIZE]; char cardBack[ROWSIZE][COLSIZE]; srand(time(0)); // Displays back of cards for (i = 0; i < ROWSIZE; i++){ for (j = 0; j < COLSIZE; j++){ cardBack[i][j] = '#'; cout << cardBack[i][j]; } cout << endl; } // displays front of cards for (i = 0; i < ROWSIZE; i++){ for (j = 0; j < COLSIZE; j++){ if (i < 2){ cardFront[i][j] = COLSIZE * i + j + 1; } else{ cardFront[i][j] = (COLSIZE * i + j + 1) - 8; } cout << cardFront[i][j]; } cout << endl; } // randomizing numbers for (i = 0; i < 10; i++){ k = rand() % 4; p = rand() % 4; m = rand() % 4; n = rand() % 4; x = cardFront[k][p]; cardFront[k][p] = cardFront[m][m]; cardFront[m][k] = x; } for (i = 0; i < ROWSIZE; i++){ for (j = 0; j < COLSIZE; j++){ cout << cardFront[i][j]; } cout << endl; } return 0; }

Explanation / Answer

This game usually doesn't need two players. A single player can play the game. It's all about the number of chances he uses to lift all cards up.

This is the code for you. If you need any further modifications. Just ping me back.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
const int ROWSIZE = 4;
const int COLSIZE = 4;
int i = 0;
int j = 0;
int x = 0;
int k = 0;
int p = 0;
int m = 0;
int n = 0;
int cardFront[ROWSIZE][COLSIZE];
char cardBack[ROWSIZE][COLSIZE];
int row1, col1, row2, col2, count = 0;
bool allCardsOpened;
srand(time(0));
// Displays back of cards
cout<<"Back of Cards..."<<endl;
for (i = 0; i < ROWSIZE; i++)
{
for (j = 0; j < COLSIZE; j++)
{
cardBack[i][j] = '#';
cout << cardBack[i][j]<<" ";
}
cout << endl;
}
cout<<endl<<"Front of Cards..."<<endl;
// displays front of cards
for (i = 0; i < ROWSIZE; i++)
{
for (j = 0; j < COLSIZE; j++)
{
if (i < 2)
{
cardFront[i][j] = COLSIZE * i + j + 1;
}
else
{
cardFront[i][j] = (COLSIZE * i + j + 1) - 8;
}
cout << cardFront[i][j]<<" ";
}
cout << endl;
}
// randomizing numbers
for (i = 0; i < 10; i++)
{
k = rand() % 4;
p = rand() % 4;
m = rand() % 4;
n = rand() % 4;
x = cardFront[k][p];
cardFront[k][p] = cardFront[m][n];
cardFront[m][n] = x;
}
//Lets not show this to the players and let them play from here onwards...
//This code should be concealed from here...
/*cout<<"After randomizing, Front of Cards..."<<endl;
for (i = 0; i < ROWSIZE; i++)
{
for (j = 0; j < COLSIZE; j++)
{
cout << cardFront[i][j]<<" ";
}
cout << endl;
} */
//This code should be concealed till here...
cout<<"Welcome to the game of Memory...";
while(1)
{
allCardsOpened = true;
for(i = 0; i < ROWSIZE; i++)
for(j = 0; j < COLSIZE; j++)
if(cardBack[i][j] == '#')
allCardsOpened = false;
if(allCardsOpened == true)
{
cout<<"Game Over. You took "<<count<<" turns to finish the game."<<endl;
exit(0);
}
count++;
cout<<"Select any 2 cards: "<<endl;
cout<<"Card 1 (row, col): ";
cin>>row1>>col1;
cout<<"Card 2 (row, col): ";
cin>>row2>>col2;
if(row1 >= ROWSIZE || row2 >= ROWSIZE || col1 >= COLSIZE || col2 >= COLSIZE)
{
cout<<"This board is of size: "<<ROWSIZE<<" x "<<COLSIZE<<"."<<endl;
}
if(row1 == row2 && col1 == col2)
{
cout<<"Please select two different positions...."<<endl;
continue;
}
if(cardBack[row1][col1] != '#' || cardBack[row2][col2] != '#')
{
cout<<"Please select a position that is not opened...."<<endl;
continue;
}
if(cardFront[row1][col1] == cardFront[row2][col2])
cardBack[row1][col1] = cardBack[row2][col2] = '0'+cardFront[row1][col1];
else
{
cout<<"Card1: "<<cardFront[row1][col1]<<" ";
cout<<"Card2: "<<cardFront[row2][col2]<<" ";   
}
cout<<"Current Board State: "<<endl;
for(i = 0; i < ROWSIZE; i++)
{
for(j = 0; j < COLSIZE; j++)
cout<<cardBack[i][j]<<" ";
cout<<endl;
}
}
return 0;
}