0

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.

user17732522
  • 16,968
  • 2
  • 24
  • 58
  • 1
    What have you already tried to figure out the problem? – user253751 Apr 05 '22 at 09:08
  • 4
    Please learn how to define a custom function ASAP. You have put everything in `main` what makes code unreadable and over-complicated. Split code into multiple functions which do single simple task. – Marek R Apr 05 '22 at 09:12
  • 2
    After you did what @MarekR suggested I recommend to investigate into [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/7478597). – Scheff's Cat Apr 05 '22 at 09:26
  • Ok. Erm. I don’t want to sound condescending, but c++ has a pretty steep learning curve and I don’t know where to start with your snippet. Have you considered doing it in python? – viraltaco_ Apr 05 '22 at 11:22
  • I've tried by declaring some variables to 0 at the end of large processes like the 2 loops that check 1296 times memo[ ] variables, but it seems the problem is in that line of code or related to the program letting coordinates repeat after the 2nd turn. – Carlos Zamora Apr 05 '22 at 13:44
  • Avoid using variable names like `a, n` , and specify the purpose of each variable by naming or commenting. Try to comment out unnecessary code to make debugging easier. See [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Minxin Yu - MSFT Apr 06 '22 at 02:16
  • Is your problem solved? You can update the code by editing. – Minxin Yu - MSFT Apr 15 '22 at 03:33
  • @MarekR The longest main I encountered in my professional life was 4k lines of code ;) – BitTickler May 11 '22 at 16:07
  • @BitTickler I currently see 3k main :(. This proves we should keep training/teaching beginners to see this less often. – Marek R May 11 '22 at 16:38

0 Answers0