'why is my code not giving me a correct value?
I am doing a memory game (you choose 2 cards, if they are the same, you gain a point), and after finishing my 1v1 version, I wanted to make a bot that I can play against, but somehow, something fails in the 2nd bot attempt, and he gives a coordinate that he shouldn't.
The bot passes thru a series of filters where he won't choose a card that has already been paired, and he even if he follows rules on first attempt, second bot attempt is wrong.
#include <iostream>
#include <time.h>
#include <cstdlib>
using namespace std;
int main()
{
srand(time(0));
int x = 0, y = 0, n = 0, r = 0, d = 2, s = 0, a = 0;
int p[2]{ 0,0 };
int dif[3]{ 40,80,100 };
string e = "", o = "";
string letras[6]{ "A","B","C","D","E","F" };
string cartas[5][36]
{
{ "A1","A2","A3","A4","A5","A6","B1","B2","B3","B4","B5","B6","C1","C2","C3","C4","C5","C6","D1","D2","D3","D4","D5","D6","E1","E2","E3","E4","E5","E6","F1","F2","F3","F4","F5","F6" },
{ "_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_" },
{ "A","B","C","A","B","C","D","E","F","D","E","F","G","H","I","G","H","I","J","K","L","J","K","L","M","N","O","M","N","O","P","Q","R","P","Q","R" },
{ "","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""},
{ "","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""}
};
string memo[36]{ "","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","" };
bool rep = true, war = true, adv = true;
do
{
for (int i = 0;i < 36;i++)
{
r = rand() % 36;
if (cartas[3][r] != "")
{
i--;
}
else
{
cartas[3][r] = cartas[2][i];
cartas[4][r] = cartas[0][r];
}
}
do
{
for (int c = 0;c < 3;c++)
{
system("cls");
cout << "Jugador: " << p[0] << "\nComputadora: " << p[1] << endl << endl;
cout << " 1 2 3 4 5 6" << endl;
for (int i = 0;i < 36;i++)
{
if (i % 6 == 0)
{
cout << letras[i / 6] << " ";
}
cout << cartas[1][i];
if (i % 6 != 5)
{
cout << " ";
}
else
{
cout << endl << endl;
}
}
switch (c)
{
case 0:
if (n % 2 == 0)
{
cout << "Jugador" << endl << endl << "Seleccione una carta: ";
cin >> e;
for (int i = 0;i < 36;i++)
{
if (e == cartas[0][i])
{
x = i;
break;
}
}
for (int i = 0;i < 36;i++)
{
if (e == cartas[0][i])
{
adv = false;
}
}
if (adv)
{
cout << endl << "Esa carta no existe" << endl << endl;
system("pause");
c--;
}
else
{
for (int i = 0;i < 36;i++)
{
if (e == cartas[4][i])
{
war = false;
cartas[1][x] = cartas[3][x];
memo[x] = cartas[2][x];
}
}
if (war)
{
cout << endl << "Esa carta ya ha sido emparejada" << endl << endl;
system("pause");
c--;
}
}
war = true, adv = true;
}
else
{
do
{
r = rand() % 36;
for (int i = 0;i < 36;i++)
{
if (cartas[0][r] == cartas[4][i])
{
rep = false;
x = r;
}
}
}
while (rep);
rep = true;
r = rand() % 100;
if (r < dif[d])
{
s = rand() % 36;
a = rand() % 36;
for (int i = 0;i < 36;i++)
{
for (int z = 0;z < 36;z++)
{
if ((memo[s % 36] == memo[a % 36]) && (memo[s % 36] != "") && (s % 36 != a % 36))
{
x = s % 36;
}
a++;
}
s++;
}
}
cout << "Computadora" << endl << endl << "Seleccione una carta: " << cartas[0][x] << endl << endl;
system("pause");
cartas[1][x] = cartas[3][x];
memo[x] = cartas[2][x];
}
break;
case 1:
if (n % 2 == 0)
{
cout << "Jugador" << endl << endl << "Seleccione otra carta: ";
cin >> e;
for (int i = 0;i < 36;i++)
{
if (e == cartas[0][i])
{
y = i;
break;
}
}
if (x == y)
{
cout << endl << "Si no le sabe, no le mueva" << endl << endl;
system("pause");
c--;
}
else
{
for (int i = 0;i < 36;i++)
{
if (e == cartas[0][i])
{
adv = false;
}
}
if (adv)
{
cout << endl << "Esa carta no existe" << endl << endl;
system("pause");
c--;
}
else
{
for (int i = 0;i < 36;i++)
{
if (e == cartas[4][i])
{
war = false;
cartas[1][y] = cartas[3][y];
memo[y] = cartas[2][y];
}
}
if (war)
{
cout << endl << "Esa carta ya ha sido emparejada" << endl << endl;
system("pause");
c--;
}
}
}
war = true, adv = true;
}
else
{
do
{
r = rand() % 36;
for (int i = 0;i < 36;i++)
{
if (cartas[0][r] == cartas[4][i])
{
rep = false;
y = r;
}
}
if (x == y)
{
rep = true;
}
}
while (rep);
rep = true;
r = rand() % 100;
if (r < dif[d])
{
s = rand() % 36;
a = rand() % 36;
for (int i = 0;i < 36;i++)
{
for (int z = 0;z < 36;z++)
{
if ((memo[s % 36] == memo[a % 36]) && (memo[s % 36] != "") && (s % 36 != a % 36))
{
y = s % 36;
rep = false;
}
a++;
}
s++;
}
}
cout << "Computadora" << endl << endl << "Seleccione otra carta: " << cartas[0][y] << endl << endl;
system("pause");
cartas[1][y] = cartas[3][y];
memo[y] = cartas[2][y];
}
break;
case 2:
system("pause");
if (cartas[3][x] == cartas[3][y])
{
memo[x] = "", memo[y] = "";
cartas[4][x] = "", cartas[4][y] = "";
p[n % 2]++;
}
else
{
cartas[1][x] = "_", cartas[1][y] = "_";
n++;
}
break;
}
}
if ((p[0] + p[1]) == 18)
{
system("cls");
if (p[0] > p[1])
{
cout << "El jugador";
}
else
{
cout << "La computadora";
}
cout << " ha ganado" << endl << endl;
system("pause");
rep = false;
}
}
while (rep);
rep = true;
system("cls");
cout << "Revancha? ";
cin >> o;
if (o != "si" && o != "sI" && o != "Si" && o != "SI")
{
rep = false;
}
for (int i = 0;i < 36;i++)
{
cartas[3][i];
cartas[1][i] = "_";
p[i % 2] = 0;
n = 0;
memo[x] = "";
}
} while (rep);
system("cls");
cout << "Gracias por usar el programa" << endl << endl;
system("pause");
return 0;
}
If I start by putting A1, A2, the bot writes some coords on the next turn, but after I put my second set of coords like A3, A4, he writes A4. He needs to put different ones. The game works my writing coordenates in you turn, if those coords' cards are the same, they are revealed and you gain a point. If both players have 18 points in total, game ends.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|