How can I switch values between arrays? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can I switch values between arrays?

I need to change the order to 0-15. Im having trouble on how to switch the values. help :,v https://code.sololearn.com/cMVSDd0kD4PR/?ref=app

18th Sep 2018, 6:16 AM
Jossue
Jossue - avatar
2 Answers
0
Hi, like this? public class change { private int [][]tab; public change(){ int [][]numeracion = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}}; tab=numeracion; } public void showtab(){ for(int i=0;i<4;i++){ for(int j= 0;j<4;j++){ System.out.print(tab[i][j]+" "); } System.out.println(); } System.out.println(); } public void goback(){ int courant; for(int i=3;i>=0;i--){ for(int j=2;j>=0;j--){ courant=tab[i][j]; tab[i][j]=tab[i][j+1]; tab[i][j+1]=courant; } if(i!=0){ courant=tab[i-1][3]; tab[i-1][3]=tab[i][0]; tab[i][0]=courant; } } } public static void main(String [] args){ change c = new change(); c.showtab(); c.goback(); c.showtab(); } }
19th Sep 2018, 11:47 AM
Frontend's Lover
Frontend's Lover - avatar
0
yes but not exactly. Its suppose to be a sliding puzzle game. 0 being the empty spot and it doesnt necessary need to be the last spot. so lets say 0 is at array [1][2] for example. I should only allow the options to move from array [1][1], [1][3], [0][2] and [2][2], if im right.
19th Sep 2018, 12:18 PM
Jossue
Jossue - avatar