You are viewing a single comment's thread. Return to all comments →
Can you please tell me what is the wrong with my code
public static boolean canWin(int leap, int[] game) { // Return true if you can win the game; otherwise, return false. int i=0; int n=game.length; while(i < n) { if((i+leap<=n-1 && game[i+leap]==0) || i+leap==n-1) i=i+leap; else if((i+1<=n-1 && game[i+1]==0) || i+1==n-1) i=i+1; else if( i-1 >=1 && game[i-1]==0 ) i=i-1; else return false; if (i==n-1) return true; } return true;}
Seems like cookies are disabled on this browser, please enable them to open this website
Java 1D Array (Part 2)
You are viewing a single comment's thread. Return to all comments →
Can you please tell me what is the wrong with my code
public static boolean canWin(int leap, int[] game) { // Return true if you can win the game; otherwise, return false. int i=0; int n=game.length; while(i < n) { if((i+leap<=n-1 && game[i+leap]==0) || i+leap==n-1) i=i+leap; else if((i+1<=n-1 && game[i+1]==0) || i+1==n-1) i=i+1; else if( i-1 >=1 && game[i-1]==0 ) i=i-1; else return false; if (i==n-1) return true; } return true;}