1
I have a matrix of structures as a board. My character starts at the position [0][0]:
tabuleiro[0][0] = personagem;
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.
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
iand thejthat are associated in this drive attempt?– Jefferson Quesado
In principle both are defined as 0, since the character’s initial position will always be [0][0].
– Caio Costa
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– Jefferson Quesado
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.
– Caio Costa
I can already say that the function
movimentaris 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.– Jefferson Quesado
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
– Caio Costa
Your idea of
criatura vaziacan be comparable to something like aObjeto Nulo.– Lacobus