Hallo Ich habe Probleme mit dieser Aufgabe. Mein Programm ist ein Memory Match Game in C++, und es läuft, aber ich muss die Zeichen in meinem 2D-Array bekommen, um Positionen zu verschieben, nachdem der Benutzer eine gültige oder ungültige Antwort eingegeben hat. Sie bleiben an der gleichen Position wie initialisiert. Ich habe mehrere Ideen ausprobiert, bin aber zu keinem Ergebnis gekommen. Sogar einige Hinweise, die helfen könnten, werden geschätzt.Memory Match Game ++ Wie zufällige Zeichen in einem Array
#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");
}
Was "mehrere Ideen" haben Sie versucht? – paddy
Warum niemals [** 'using namespace std;' **] (http://stackoverflow.com/questions/1452721/why-ususing-namespace-std-considered-bad-practice) und [** 'System();' Funktionen **] (http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong) – amanuel2
Ich wurde in der Klasse unterrichtet, um diese Funktionen zu verwenden, las ich durch das verbundene und Ich wusste nie, wie unbequem sie auf lange Sicht waren oo @ amanuel2. Edit: Danke m8 – RoninDaMerc