rahulbgautam
BAN USERpublic class RemoteLogic {
private char[][] keyBoard = new char[6][5];
public RemoteLogic(char[][] keyBoard) {
this.keyBoard = keyBoard;
}
// Function writes a word in form of u,d,l,r,!
public void writeWord(String wordToDisplay) {
//last row position of the cursor
int lastrowpos = 0;
//last column position of the cursor
int lastcolpos = 0;
for (int i = 0; i <= wordToDisplay.length() - 1; i++) {
// Convert each character in the word to ascii code
char c = wordToDisplay.charAt(i);
//Convert to zero based index for easier handling
int cint = (int) c - 97;
//Store current row and column position
int currrowpos = cint / 5;
int currcolpos = cint % 5;
moveCursor(currrowpos, currcolpos, lastrowpos, lastcolpos);
lastrowpos = currrowpos;
lastcolpos = currcolpos;
System.out.println("!");
}
}
//Gets the current and the last position of the cursor and moves the cursor accordingly
private void moveCursor(int currrowpos, int currcolpos, int lastrowpos,
int lastcolpos) {
//If on the same character , just press enter
if (currcolpos == lastcolpos && currrowpos == lastrowpos) {
System.out.println("!");
}
//first move within a column
if (currcolpos < lastcolpos) {
printMoves(lastcolpos - currcolpos, "l");
}
//then move between rows
if (currrowpos < lastrowpos) {
printMoves(lastrowpos - currrowpos, "u");
}
if (currcolpos > lastcolpos) {
printMoves(currcolpos - lastcolpos, "r");
}
if (currrowpos > lastrowpos) {
printMoves(currrowpos - lastrowpos, "d");
}
}
//Print number of moves
private void printMoves(int i, String string) {
for (int x = 1; x <= i; x++) {
System.out.print(string);
}
}
}
- rahulbgautam June 20, 2013