Difficulty moving character on board

Asked

Viewed 429 times

1

I have a matrix of structures as a board. My character starts at the position [0][0]:

tabuleiro[0][0] = personagem;

Tabuleiro inicialmente

The character is displayed correctly in its initial position, but I can’t move it using the code below (it’s incomplete, but it should work as far as I did):

void movimentar(criatura tabuleiro[][10], criatura personagem){
system("cls");
imprime(tabuleiro);
criatura vazio;
int i=0, j=0;
char mover;

printf("Use WASD ou as setas do teclado para se mover pelo tabuleiro!\n");
scanf(" %c",&mover);

if(mover=='s'){
    tabuleiro[i+1][j]= personagem;
    i++;
}
imprime(tabuleiro);
system("pause");            
}

The board and the character are defined some functions above. More or less in this way (I took what I consider less relevant):

criatura tabuleiro[5][10];
    criatura personagem;
    criatura chefe;
    criatura vazio; // isto aqui adicionei recentemente, ainda preciso verificar se faz sentido criar uma "criatura vazio" para colocá-la no local de onde o personagem saiu.
    vazio.classe = ' ';

    for(i=0;i<5;i++){
        for(j=0;j<10;j++){
            tabuleiro[i][j].status = 0; // aqui o tabuleiro todo é dado como desconhecido, a princípio (utilizo 0 como status para aquilo que não está visível).
            tabuleiro[i][j].classe = ' '; 
        }
    }

    // ATRIBUTOS PERSONAGEM 
    personagem.classe = classeE;
    personagem.atc = atcE;
    personagem.def = defE;
    personagem.saude = saudeE;
    personagem.status = 1;
    tabuleiro[0][0] = personagem;

When I press s, the character remains in the same position and a blank appears on the other side of the board.

tabuleiro após tentar mover personagem

The function to print the board is partly this:

for(j=0;j<10;j++){
            if(tabuleiro[i][j].status == 0){
                printf(" ?  ");
            }
            else{
                if(tabuleiro[i][j].classe=='X'){// X -> armadilha de grande potencial ofensivo
                    printf(" X  ");
                }
                else if(tabuleiro[i][j].classe=='x'){ // x -> armadilha de pequeno potencial ofensivo
                    printf(" x  ");
                }
                else if(tabuleiro[i][j].classe=='i'){//i -> inimigo
                    printf("%c_%c ",155,155);
                }
                else if(tabuleiro[i][j].elixir == 'e'){ // e -> elixir
                    printf(" %c  ",3);
                }
                else if(tabuleiro[i][j].classe == 'C'){ //C -> Chefe
                    printf("%c_%c ",227,224);
                }   
                else if(tabuleiro[i][j].classe == ' '){
                    printf("   ");
                }       
                else if(tabuleiro[i][j].classe=='g' || tabuleiro[i][j].classe=='b' || tabuleiro[i][j].classe=='p'){ // g,b ou p -> herói
                    printf("^_^ ");
                }
            }
        }

The character’s status is set to 1 at the beginning of the code, so that it is visible. I didn’t copy all the code here because it would be a lot, it is possible to know what is going wrong just with this information?

@Dit:

if(mover=='s'){
    tabuleiro[i+1][j]= personagem;
    i++;
}

for

 if(mover=='s'){
        tabuleiro[i+1][j] = personagem;
        tabuleiro[i+1][j].classe = 'g'; // onde g é a classe guerreiro
        tabuleiro[i][j].classe = ' ';
        i++;
    }

and it seems to be "working", I’m just not sure if it’s the right way...

  • Where does the i and the j that are associated in this drive attempt?

  • In principle both are defined as 0, since the character’s initial position will always be [0][0].

  • But what about updating it? It should not keep up with these values all its life. It has how to put more context around this part of mover? To know how the variables relate, where they come from, where they go

  • It’s true! I thought, then, to put an i++ after the board[i+1][j]= character; in the if of the "s". At first I had thought about doing a go to check where the character is, but the initial position is always known, so I think it is possible to work by only changing the value of i and j from 0.0, in this case. I will update the post with more information // @Jeffersonquesado added new information.

  • I can already say that the function movimentar is wrong. You have no persistence of the player’s current position, so he can’t, say go down twice. I do not know if I will be able to elaborate a response in time, but I recommend that you keep in the player itself what your current position.

  • Thanks @Jeffersonquesado, I noticed this when I tried to move here. The solution I found was to pass the values of the row and column by pointer: int *Lin, int *col Then store them in int variables within the function: int l = *Lin; int c = *col; Finally, I made the move as follows: if(board[l+1][c].class == ' '){ board[l+1][c] = character; board[l+1][c]. class = classeE; board[l][c]. class = ' ; l++; *Lin = l; It seems to have worked! Edit: here in the comments it gets hard to read, I’ll add in the answers

  • 1

    Your idea of criatura vazia can be comparable to something like a Objeto Nulo.

Show 2 more comments

1 answer

0


The solution I found was the following:

void movimentar(criatura tabuleiro[][10], criatura personagem,char classeE, int *saudeE, int *atcE, int *defE, int *lin, int *col, int *elixir){ // linha e coluna passados aqui
    system("cls");
    imprime(tabuleiro);
    criatura vazio;
    int l = *lin; 
    int c = *col;
    char mover;
    int retirar;
    srand(time(NULL));
    printf("Use W, A, S, ou D para se mover pelo tabuleiro!\n");
    scanf(" %c",&mover);

    if(mover=='s' || mover =='S' ){
        if(l+1<=5){
            if(tabuleiro[l+1][c].classe == ' '){ // a movimentação ocorre diretamente se o local para onde o usuário deseja ir não existe inimigo
                tabuleiro[l+1][c] = personagem;
                tabuleiro[l+1][c].classe = classeE; // classeE é a classe escolhida pelo usuário no início do jogo
                tabuleiro[l][c].classe = ' ';
                l++;
                *lin = l; // salvando o valor de volta na função anterior

I do not know if this solution is really adequate, but so far it has worked well. I created other ifs to move to the other sides and took care of all the special cases I imagined.

Browser other questions tagged

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