Skip to content

Instantly share code, notes, and snippets.

@welvet
Created August 8, 2013 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save welvet/6182671 to your computer and use it in GitHub Desktop.
Save welvet/6182671 to your computer and use it in GitHub Desktop.
import java.util.Arrays;
/**
* User: AKutuzov
* Date: 8/8/13
* Time: 11:47 AM
*/
public class TestJava {
public static void main(String[] args) {
TestJava test = new TestJava();
test.c = new char[][]{
{'e', 's', 'a', 's', 'j'},
{'w', 'n', 'v', 'o', '9'},
{'r', 's', 'o', 'i', 'a'},
{'y', 's', 's', 'z', 'a'},
{'e', 's', 'r', 'a', 'd'}};
test.expectedWord = "dzone".toCharArray();
test.find();
}
char[][] c;
char[] expectedWord;
void find() {
for (int main_i = 0; main_i < c.length; main_i++) {
for (int main_j = 0; main_j < c[main_i].length; main_j++) {
if (checkLetters(main_i, main_j)) {
System.out.println("Found!!!");
return;
}
}
}
System.out.println("Not Found!!!");
}
boolean checkLetters(int main_i, int main_j) {
if (!checkLetter(0, main_i, main_j)) {
return false;
}
int step = 0;
boolean[] direction = {true, true, true, true, true, true, true, true};
int[][] mask = {{0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}};
while (step < expectedWord.length - 1) {
step++;
boolean nothing = true;
for (int d = 0; d < direction.length; d++) {
if (direction[d]) {
if (!checkLetter(step, main_i + step * mask[d][0], main_j + step * mask[d][1])) {
direction[d] = false;
} else {
nothing = false;
}
}
}
if (nothing) return false;
}
System.out.println("We found something on: x = " + main_i + " y = " + main_j);
System.out.println("Directions " + Arrays.toString(direction));
return true;
}
boolean checkLetter(int pos, int i, int j) {
if (i < 0 || j < 0) return false;
if (c.length <= i || c[i].length <= j) return false;
return expectedWord[pos] == c[i][j];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment