I need some help with this program:
Write a program that presents the backs of a number of cards to the player. You should have at least 10 cards. The player selects 2 cards (one at a time), if they match, the player gets a point and the cards remain face up. If they do not match, the cards must turn back over. The game continues until all cards have been turned over.
I wrote most of the program but it didn't work out. I don't know how to check if the pairs of cards is match or not but I am trying to do it.
Here my code:
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
int main()
{
int row1, column1, row2, column2, board[2][5];
int choiceBoard[2][5] =
{
{0,1,2,3,4},
{5,6,7,8,9}
};
char displayBoard[2][5] =
{
{'0','1','2','3','4'},
{'5','6','7','8','9'}
};
char displayLetter[2][5] =
{
{'A','B','C','D','E'},
{'E','D','C','B','A'}
};
int firstChoice;
int secondChoice;
bool playing = true;
while (playing)
{
for (int row = 0; row < 2; row )
{
for (int column = 0; column < 5; column )
{
cout << "[" << displayBoard[row][column] << "]";
}
cout << endl;
}
cout << "Choose a card to reveal it: ";
cin >> firstChoice;
for (row1 = 0; row1 < 2; row1 ) {
for (column1 = 0; column1 < 5; column1 ) {
if (choiceBoard[row1][column1] == firstChoice) {
displayBoard[row1][column1] = displayLetter[row1][column1];
}
}
}
cout << "Choose another card to reveal it: ";
cin >> secondChoice;
for (row2 = 0; row2 < 2; row2 ) {
for (column2 = 0; column2 < 5; column2 ) {
if (choiceBoard[row2][column2] == secondChoice) {
displayBoard[row2][column2] = displayLetter[row2][column2];
}
}
}
if (board[row1][column1] == board[row2][column2]) {
cout << "Match!" << endl;
Sleep(2000);
}
else {
cout << "Card do not match!" << endl;
Sleep(1000);
}
system("cls");
if (firstChoice == secondChoice)
{
cout << "Error..." << endl;
playing = false;
}
}
return 0;
}
CodePudding user response:
I don't know what these boards' functions are, but if you want to judge if the selected pair is match, you can try this:
if (displayLetter[firstChoice / 5][firstChoice % 5] == displayLetter[secondChoice / 5][secondChoice % 5]) {
cout << "Match!" << endl;
Sleep(2000);
}
Then you can change the display content, I don't think some of the for-loops are necessary.
