【发布时间】:2016-10-12 02:00:33
【问题描述】:
您好,我对这项作业有疑问。我的程序是 C++ 中的内存匹配游戏,它可以运行,但是每次用户输入有效或无效响应后,我都需要让二维数组中的字符移动位置。它们停留在与初始化相同的位置。我尝试了多种想法,但没有得出任何结论。甚至一些可能会有所帮助的提示也会受到赞赏。
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<string>
using namespace std;
class Game //Memory Match Game
{
private:
char cards[4][4] = { { '@', '#', '$', '%' },
{'^', '&', '*', '+'},
{'@', '#', '$', '%'},
{'^', '&', '*', '+' } };
char deck[16] = { '@','#', '$', '%', '^','&', '*', '+' }; //not being used atm
int r1, r2, c1, c2;
public:
Game()
{
r1 = 0; r2 = 0; c1 = 0; c2 = 0;
}
void begin()
{
srand((unsigned)time(NULL));
for (int x = 0; x < 4; x++) //begin designating cards
{
for (int y = 0; y < 4; y++)
{
cards[rand() % x][rand() % y]; // <:::::[:::] FIX
cout << " " << cards[x][y] << " ";
}
cout << endl;
}
cout << " \n 1 2 3 4\t\n";
cout << " ";
for (int x = 0; x <= 8; x++)
{
cout << "_";
}
cout << endl;
for (int x = 0; x < 4; x++) //appropriate positioning
{
cout << x + 1 << " | ";
for (int y = 0; y < 4; y++)
{
cout << "? ";
}
cout << endl;
}
cout << endl;
input();
}
void input()
{
srand(time(0));
cout << "Please type in the row and column of choice.\n"; //begin user play
cin >> r1;
cin >> c1;
cout << "Please type in the row and column of choice\n";
cin >> r2;
cin >> c2;
r1--; r2--; c1--; c2--;
cout << " 1 2 3 4\n";
cout << " ";
for (int x = 0; x <= 8; x++)
{
cout << "_";
}
cout << endl;
for (int x = 0; x < 4; x++)
{
cout << x + 1 << " | ";
for (int y = 0; y < 4; y++)
{
if ((x == r1) && (y == c1))
{
cout << cards[x][y] << " ";
}
else if ((x == r2) && (y == c2))
{
cout << cards[x][y] << " ";
}
else cout << "? ";
}
cout << endl;
}
match();
}
void match()
{
if (cards[r1][c1] == cards[r2][c2])
{
cout << "Valid Match!\n";
input();
}
else
cout << "Invalid Match!\n";
input();
}
};
int main()
{
Game memoryMatch;
memoryMatch.begin();
system("pause");
}
【问题讨论】:
-
你尝试过哪些“多种想法”?
-
我在课堂上被教导如何使用这些功能,我通读了链接,我从不知道从长远来看它们有多不方便 o-o @amanuel2。编辑:谢谢 m8
-
@paddy 我已经尝试在我的 begin() 函数的第一个 for 循环中操作 cards[x][y] 2D 数组,但没有成功。
-
@RoninDaMerc 你试过
std::vector吗? :) 。从长远来看非常方便,如果您可以使用库的话!