0

# How can we obtain the coordinations(x,y) of a precise number in a 2D array?

I have to make a game, in which when we press an arrow on the keyboard, the number 0 can exchange it's place with the one next to him

4 Answers

0

For earch press just swap the 0 with the corresponding index.
if up is pressed ¬ at top
Swap with the row above
If right is pressed ¬ at rightEdge
Swap with the col to the right
Etc

0

Please can you explain it one more time, but for a be beginner to be able to understand. I stills don't get how we can exchange the numbers

0

In my head it was like by having the coordinations of 0 (x, y), if the player goes to the left, i will do (x-1, y) then exchange the values

0

For example you could:
prevX = x;
prevY = y;
If( left and x > 0) {
x = x-1;
}else if( right and x < max){
x = x+1;
}
// same sort logic for up and down
// set prev location to empty
grid[prevX,prevY] = 0;
// set new location to taken
grid[x,y] = 1