Home > Software design >  why is the character not moving correctly in a dynamic 2d array?
why is the character not moving correctly in a dynamic 2d array?

Time:10-26

I have troubles with moving my character. The Program should behave like this:

Read from the input two numbers separated by a space, rows and cols. Create a 2D grid (array) of characters with dimensions of rows rows and cols columns. Initialize the characters in the grid to '.' (dot). Place the turtle in position (0, 0) - the zero row and the zero column (top left). Load characters sequentially from the program input. The characters will be separated by a white character (a space or a line).

If you hit the 'x' character, print the grid to the Program Output and exit the program.

If you encounter characters '<', '>', '^', 'v', Move the turtle in the corresponding direction (Left, Right, Up, Down).

If you encounter the character 'o', enter the character' o ' in the grid at the current position of the turtle.

If the turtle were to make a move that would take it out of the grid (e.g. to position (0, 0) it would receive the command to go to the left), then “teleport” the turtle to the opposite side of the grid.

My program (I do not why but when I press ">" or "<" it moves by two, not one):

#include <stdio.h>
#include <stdlib.h>
int main(){
    int rows, cols;
    int i;
    int j;
    int x = 0, y = 0;
    char symbol = 'o';
    scanf("%d %d", &rows, &cols);
    char* pole = (char*)malloc(rows * cols * sizeof(char));
    for (i = 0; i < rows; i  ){
        for(j = 0; j < cols; j  )
            pole[i * rows   j] = '.';
    }
    pole[x * rows   y] = 'o';
    while(symbol != 'x'){
        scanf("%c", &symbol);  
        if (symbol == '^') x--; 
        else if (symbol == 'v') x  ; 
        else if (symbol == '>') y  ; 
        else if (symbol == '<') y--; 
        else if (symbol == 'o') pole[x * rows   y] = 'o'; 
        if(0 < y)
            y = rows - 1; 
        else if(y > rows)
            y = 0;
        
        if(x < 0)
            x = cols - 1;
        else if(x > cols)
            x = 0;
    }
    for (i = 0; i < rows; i  ){
        for(j = 0; j < cols; j  ){
            printf("%c", pole[i * rows   j]);
        }
        printf("\n");
    }
    return 0;    
}

It should be like this:

3 3
o
>
v
o
>
v
o
x
o..
.o.
..o

But I have this:

3 3
o
>
v
o  
>
v
o
x
o..
..o
..o

CodePudding user response:

You've done a great job in your code but what's causing an error, in output is if(0<y) this if statement. Let's say you have this as your grid

enter image description here

and with pole[x * rows y] = 'o'; 0,0 is already set to 'o' while your turtle stays at (0,0).

Now, with your input stream,

1: 'o' - The 'o' at (0,0) gets overwritten with'o'

2: > - y gets incremented by 1

3: v - x gets incremented by 1

so now your turtle is at (1,1) i.e. position 4 at grid but now when if(0<y) is encountered it comes to be true cause y<0 where y=1 at the moment so y is changed to y=rows-1 i.e. 2 in this case So, now your turtle is at (0,2) i.e. position 5 at the grid, and now when 'o' is given as input it is set to 5th position of the grid.

This same thing will happen again n again with y no matter where you want it using '>', after if(0<y) y will be set to last column of the grid irrespective of the value of x

And if just in case y actually becomes less than 0 there will be an run time error in your code.

So, as it's solution you should use if(y<0) instead, in this case y will only be set to last column if user enters '<' when turtle is at anywhere in the first column of the grid.

CodePudding user response:

The code seems to work as expected for the input you have given when you change if (0 < y) to if (0 > y) (i.e., the opposite relation). By the way, I would suggest you to write it if (y < 0), which is more readable, at least for me.

(Thanks, Lundin, for a hint to this line.)

  • Related