Goals: Developing problem-solving skills, declaring variables, using loops, and
ID: 3632441 • Letter: G
Question
Goals: Developing problem-solving skills, declaring variables, using loops, and using 2-D arrays and functions.
Problem: A common memory matching game played by young children is to start with a deck of cards that contains identical pairs. For example, given six cards in the deck, two might be labeled “1,” two might be labeled “2,” and two might be labeled “3.” The cards are shuffled and placed face down on the table. The player then selects two cards that are face down, turns them face up, and if they match they are left face up. If the two cards do not match, they are returned to their original position face down. The game continues until all cards are face up.
Write a program that plays the memory matching game. Use 16 cards that are laid out in a 4x4 square and are labeled with pairs of numbers from 1 to 8. Your program should allow the player to specify the cards that she would like to select from the coordinate system.
For example, suppose the cards are in the following layout (during game play):
1 2 3 4
_______________________
1 | 8 * * *
2 | * * * *
3 | * 8 * *
4 | * * * *
All of the cards are face down except for the pair 8, which has been located at coordinates (1, 1) and (2, 3). To hide the cards that have been temporarily placed face up, output a large number of newlines to force the old board off the screen.
Hints: Use a two-dimensional array for the arrangement of cards and another two-dimensional array that indicates if a card is face up or face down. Write a function that “shuffles” the cards in the array by repeatedly selecting two cards at random and swapping them. You should swap the cards some non-trivial number of times before setting up the game board. “Non-trivial” here means more than 100.
You will have to do some minimal research on random number generation for the “selecting two cards at random”. In particular, check cplusplus.com for information on rand() and srand().
Your program should employ the following:
• as indicated in the problem description: 2 two-dimensional arrays, one for the arrangement, and one for the indicating if cards are face-up or face-down
• a function that shuffles cards in order to create the initial setup for the memory game; this should use random number generation of some sort
• a function that displays the current status of the game, as shown above, with stars to indicate cards that are face down
• cin statements to take coordinates from the player
• any other 2-D arrays or functions that you find appropriate to use to solve the problem
sub question: Make your program capable of creating differently-sized game boards than just the 4x4 version given here. Take into consideration that the game board must include an EVEN number of cards only, so a 5x5 will not work. Your program should allow for a: 2x2, 4x4, 6x6, 8x8, and 10x10 sized board (you may include others, including non-square, if you choose). Worth 10 points and must be completed without aid from the TA or Miss Plasterr.
Explanation / Answer
please rate - thanks
to get you started
sub question not included, but prepared for
#include <iostream>
#include <cstdlib>
#include <iostream>
using namespace std;
const int COL = 4;
const int ROW = 4;
void input(int& row,int& col, int n)
{cout<<" Input row "<<n<<": ";
cin>> row ;
cout<<"Input column "<<n<<": ";
cin>> col ;
row--;
col--;
cout<<endl<<" ";
for(int j=0;j<COL;j++)
cout<<j+1<<" ";
cout<<endl;
}
void print(char card[][4])
{int i,j;
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<<endl;
}
}
int main()
{
srand(time(0));
int i,j,k,count=0;
int row1,col1,row2,col2 ;
char value1, value2 ;
int check1 = 0, check2=0;
char data[ROW][COL];
char card[ROW][COL];
char keys[COL*ROW ];
char tmp;
j=0;
for(i=0;i<=COL*ROW;i+=2)
{keys[i]=j+'1';
keys[i+1]=j+'1';
j++;
}
for(i=0; i<ROW ; i++ )
for(j=0; j <COL; j++ )
card[i][j]='*';
for(i=0;i<500;i++)
{j=rand()%(ROW*COL);
k=rand()%(ROW*COL);
{tmp=keys[j];
keys[j]=keys[k];
keys[k]=tmp;
}
}
k=0;
for(i=0; i<ROW ; i++ )
for(j=0; j <COL; j++ )
data[i][j]=keys[k++];
//print(data);
do
{print(card);
input(row1,col1,1);
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<<endl;
}
input(row2,col2,2);
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<<endl;
}
count++;
if(data[row1][col1]==data[row2][col2])
{card[row1][col1]=' ';
card[row2][col2]=' ';
check1++;
}
system("pause");
for(i=0;i<25;i++)
cout<<endl;
}while(check1<ROW*COL/2);
cout<<"The Board ";
print(data);
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.