How can we obtain the coordinations(x,y) of a precise number in a 2D array? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
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

11th Mar 2022, 12:45 PM
Sirenix
4 Antworten
0
For earch press just swap the 0 with the corresponding index. if up is pressed &not at top Swap with the row above If right is pressed &not at rightEdge Swap with the col to the right Etc
11th Mar 2022, 12:54 PM
Raul Ramirez
Raul Ramirez - avatar
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
11th Mar 2022, 2:13 PM
Sirenix
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
11th Mar 2022, 2:15 PM
Sirenix
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
11th Mar 2022, 3:49 PM
Raul Ramirez
Raul Ramirez - avatar