Difficulty with loops for memory game

Asked

Viewed 82 times

1

The program refers to a memory game composed of 3 4x4 arrays, where the player is against the program. My difficulty lies in the action of the program in cases where the player hits the pair of numbers, because I intend to leave the matrix with the revealed numbers "printed" on the screen and start the next move and in cases where he misses, because then who plays would be the computer and then return to the player.

Those are the criteria;

Composed of 16 pieces, character or number (8 pairs, 4x4);

There will be only one player playing "against the program";

With each new game the position of the pieces must be in different locations. For this, 3 matrices can be generated and a different one can be selected each new game;

On each move, the player informs the position (row and column) of the tiles to be shown;

The program must show the value of these tiles. If the tiles have the same value, they are unavailable and the "OK MOVE" message appears. Otherwise the message "NOK MOVE" appears;

Player can make 24 moves;

The game ends when the player closes the number of moves or until he discovers all pairs;

At the close the option for the player to play again or quit must be shown;

Do not use global variables;

using namespace std;

int main(){

int cpu1, cpu2, cpu3, cpu4;
int ti=2, tm=2, i, j, p1, p2, p3, p4, tent=24;
int mz[4][4]={0}, ma1[4][4]={1,4,5,7,2,5,8,2,6,3,4,3,7,1,8,6};

setlocale(LC_ALL, "Portuguese");

srand(time(NULL));
//abaixo a parte do código que tenho dificuldade

cout << "\t" << "1° COORDENADA" << "\n";//pedido das coordenadas 1
cout << "\n" << "N° da Linha: ";
cin >> p1;
cout << "\n" << "N° da Coluna: ";
cin >> p2;
cout << "\n";

for(i=0;i<4;i++){//laço para a matriz com o primeiro numero
    for(j=0;j<4;j++){
        if(p1 == i && p2 == j){
            cout << ma1[i][j] << "\t";
        }
        else if(p1<0 || p1>3 || p2<0 || p2>3){
            exit(0);
        }
        else{
            cout << mz[i][j] << "\t";
        }
    }
    cout << "\n";
}

cout << "\n\t" << "2° COORDENADA" << "\n";//pedido da coordenada 2
cout << "\n" << "N° da Linha: ";
cin >> p3;
cout << "\n" << "N° da Coluna: ";
cin >> p4;
cout << "\n";

for(i=0;i<4;i++){//laço com os dois números na matriz
    for(j=0;j<4;j++){
        if(p1 == i && p2 == j){
            cout << ma1[i][j] << "\t";
        }
        else if(p3 == i && p4 == j){
            cout << ma1[i][j] << "\t";
        }
        else if(p3 == p1 && p4 == p2){
            exit(0);
        }
        else if(p3<0 || p3>3 || p4<0 || p4>3){
            exit(0);
        }
        else{
            cout << mz[i][j] << "\t";
        }
    }
    cout << "\n";
}

//aqui inicia a verificação se o jogador errou ou acertou
//como pode-se ver só tem o caso se ele erra e passa a vez
//preciso saber como posso montar no caso que ele acerta

    if(ma1[p1][p2] != ma1[p3][p4]){
    tent--;

    cout << "\n" << "Você errou, lhe restam (" << tent << ") tentativas.";
    cout << "\n" << "CPU irá jogar agora!";

    Sleep(5000);
    system("cls");

    cout << "Eu escolho..." << "\n\n";
    cpu1 = rand() % 4;
    cpu2 = rand() % 4;

    for(i=0;i<4;i++){
        for(j=0;j<4;j++){
            if(i == cpu1 && j == cpu2){
                cout << ma1[i][j] << "\t";
            }
            else{
                cout << mz[i][j] << "\t";
            }
        }
        cout << "\n";
    }

    Sleep(3000);

    cout << "\n" << "E também..." << "\n\n";
    start1:
    cpu3 = rand() % 4;
    cpu4 = rand() % 4;

    for(i=0;i<4;i++){
        for(j=0;j<4;j++){
            if(i == cpu3 && j == cpu4){
                cout << ma1[i][j] << "\t";
            }
            else if(i == cpu1 && j == cpu2){
                cout << ma1[i][j] << "\t";
            }
            else if(cpu1 == cpu3 && cpu2 == cpu4){
                goto start1;
            }
            else{
                cout << mz[i][j] << "\t";
            }
        }
        cout << "\n";
    }
//ambas situações verificam apenas
//se o jogador e a cpu erraram, quero saber continuar o codigo
//quando eles acertam a coordenada.
}
return 0;

}

  • Your question seems to have some problems and your experience here in Stack Overflow may not be the best because of this. We want you to do well here and get what you want, but for that we need you to do your part. Here are some guidelines that will help you: Stack Overflow Survival Guide in English.

  • Important to read the material indicated in the previous comment, and then [Edit] the post providing a [mcve] only of the part with problem, and an explanation of what is difficult exactly, in a very clear way.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.