I have seen a few answers to this problem, but do not believe that they truely f
ID: 3546646 • Letter: I
Question
I have seen a few answers to this problem, but do not believe that they truely follow the parameters of the prompt:
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 math, then they can memorize the cards and use their memory to selesct the next pair of cards. The game continues until all of the cards are face up. Write a program to play the memory game. Use a two-dimensional array of 4 rows and 4 columns to use a deck of 16 cards with 8 matching pairs and you can use numbers 1 to 8 to mark the cards. (If you use a 6 by 6 array, then you will need 18 matching pairs, and so on). Use random number generatirs ti randomly store the pairs in the array. Use appropriate functions in your program, and the main program should be merely a call to functions.
I have the function main to reference, and I have fleshed it out a bit, but I have no idea how to make the functions create a board that randomly places "1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8" in the grid. I know that a bool value is used, but I am not sure where or how, and I do not know how to pass the board between functions without making it global.
Explanation / Answer
please rate - thanks
in C++
tested with dev C++
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int COL = 4;
const int ROW = 4;
int main()
{
srand(time(0));
int i,j,k,count=0,tmp;
int row1,col1,row2,col2 ;
int value1, value2 ;
int check1 = 0, check2=0;
int data[ROW][COL];
int card[ROW][COL];
int used[ROW*COL]={0};
for(i=0; i<ROW ; i++ )
for(j=0; j <COL; j++ )
card[i][j]=0;
for(i=0; i<ROW ; i++ )
{for(j=0; j <COL; j++ )
{tmp=rand()%(COL*ROW/2) ;
while(used[tmp]>=2)
tmp=rand()%(COL*ROW/2);
used[tmp]++;
data[i][j]=tmp+1;
}
}
/*
cout<<"The Board ";
for(i=0; i<ROW ; i++ )
{
for(j=0; j <COL; j++ )
{
cout << " " << data[i][j];
}
cout << " ";
}
*/
do
{cout<<endl<<" ";
for(j=0;j<COL;j++)
cout<<j+1<<" ";
cout<<endl;
for(i=0; i<ROW ; i++ )
{cout<<i+1<<" ";
for(j=0; j <COL; j++ )
{
cout << " " << card[i][j];
}
cout <<" ";
}
cout <<" Input row 1:";
cin >> row1 ;
cout <<"Input column 1:";
cin >> col1 ;
row1--;
col1--;
cout<<endl<<" ";
for(j=0;j<COL;j++)
cout<<j+1<<" ";
cout<<endl;
for(i=0; i<ROW ; i++ )
{cout<<i+1<<" ";
for(j=0; j <COL; j++ )
{
if(i==row1&&j==col1)
cout << " " << data[i][j];
else
cout << " " << card[i][j];
}
cout << " ";
}
cout <<" Input row 2:";
cin >> row2 ;
cout <<"Input column 2:";
cin >> col2 ;
row2--;
col2--;
cout<<endl<<" ";
for(j=0;j<COL;j++)
cout<<j+1<<" ";
cout<<endl;
for(i=0; i<ROW ; i++ )
{cout<<i+1<<" ";
for(j=0; j <COL; j++ )
{
if(i==row2&&j==col2||i==row1&&j==col1)
cout << " " << data[i][j];
else
cout << " " << card[i][j];
}
cout << " ";
}
count++;
if(data[row1][col1]==data[row2][col2])
{card[row1][col1]=data[row1][col1];
card[row2][col2]=data[row1][col1];
check1++;
}
}while(check1<ROW*COL/2+1);
cout<<"The Board ";
for(i=0; i<ROW ; i++ )
{
for(j=0; j <COL; j++ )
{
cout << " " << data[i][j];
}
cout << " ";
}
system("pause");
return 0;
}