/ Published in: Java

Simple function for finding shortest path given A and B variables. Each 'turn' the user can move 2 blocks horizontal and one block vertical or two blocks vertical and one block horizontal. Get to 0.0.
Expand |
Embed | Plain Text
public static int countTurns(int A, int B){ //check a and b. If either a or b is at 0, we know we're stuck. if(A == 0 && B == 0) return 0; else if(A == 0 || B == 0) return -1; //declare some variables. int turns = 0; //now convert them to their positive form. //now make our loop. while(a != 0 && b != 0){ //check who to move first. if(a > b){ //move a 2 and b 1. a -=2; b -=1; } else { a -=1; b -=2; } //now add our turn. turns += 1; //check for our 100,000,000 if(turns > 100000000) return -2; } //now a bit more logic. if(a == 0 && b == 0) return turns; else return -1; // thats it! }
You need to login to post a comment.