用C++制作一个记忆卡翻转游戏
Making a memory card flipping game in C++
我需要一些关于这个程序的帮助:
编写一个程序,向玩家展示多张牌的背面。你应该在
至少10张牌。玩家选择 2 张牌(一次一张),如果它们匹配,则玩家获得一分,并且
牌面朝上。如果它们不匹配,则卡片必须翻过来。比赛继续
直到所有牌都被翻开。
我写了大部分程序,但没有成功。我不知道如何检查卡片对是否匹配,但我正在尝试这样做。
这是我的代码:
#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;
}
我不知道这些板子的功能是什么,但是如果你想判断选择的对是否匹配,你可以试试这个:
if (displayLetter[firstChoice / 5][firstChoice % 5] == displayLetter[secondChoice / 5][secondChoice % 5]) {
cout << "Match!" << endl;
Sleep(2000);
}
然后你可以改变显示内容,我觉得有些for-loops是不需要的。
我需要一些关于这个程序的帮助:
编写一个程序,向玩家展示多张牌的背面。你应该在 至少10张牌。玩家选择 2 张牌(一次一张),如果它们匹配,则玩家获得一分,并且 牌面朝上。如果它们不匹配,则卡片必须翻过来。比赛继续 直到所有牌都被翻开。
我写了大部分程序,但没有成功。我不知道如何检查卡片对是否匹配,但我正在尝试这样做。
这是我的代码:
#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;
}
我不知道这些板子的功能是什么,但是如果你想判断选择的对是否匹配,你可以试试这个:
if (displayLetter[firstChoice / 5][firstChoice % 5] == displayLetter[secondChoice / 5][secondChoice % 5]) {
cout << "Match!" << endl;
Sleep(2000);
}
然后你可以改变显示内容,我觉得有些for-loops是不需要的。