Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SopaLetras {
- private char[][] grelha;
- private int linha;
- private int col;
- private int pesquisa_linha;
- private int pesquisa_col;
- public SopaLetras(char[][] grelha){
- this.grelha=grelha;
- this.linha=0;
- this.col=0;
- }
- public static void main(String[] args) throws FileNotFoundException {
- char[][]teste = {{'a','n','a','a','y'},{'a','n','a','x','y'},{'r','n','a','x','y'},{'t','n','a','x','y'},{'a','n','a','x','y'}};
- String[] pal ={"ana","art","arroz"};
- for(int i=0; i<teste.length;i++){
- for(int j=0; j<teste.length;j++){
- System.out.print(teste[i][j] + " ");
- }
- System.out.println();
- }
- SopaLetras sp = new SopaLetras(teste);
- sp.procurar(pal[1]);
- }
- private boolean primeiraLetra(String word){
- for(int i=linha;i<grelha.length;i++){
- for(int j=col;j<grelha[i].length;j++){
- if(word.charAt(0)==grelha[i][j]){
- linha = i;
- col = j;
- pesquisa_linha=i;
- pesquisa_col=j;
- return true;
- }
- }
- }
- return false;
- }
- public boolean procurar_palavra(String word, Direction tipe){
- String temp="";
- int c=0;
- while(pesquisa_linha>=0 && pesquisa_col>=0 && pesquisa_linha<grelha.length && pesquisa_col<grelha.length && c<word.length()){
- temp+=grelha[pesquisa_linha][pesquisa_col];
- direction(tipe);
- System.out.println("col"+pesquisa_col);
- c++;
- }
- System.out.println(tipe+": "+temp);
- if(word.equalsIgnoreCase(temp)){
- return true;
- }
- return false;
- }
- public void direction(Direction tipe){
- switch(tipe){
- case left:
- --pesquisa_col;
- case downleft:
- --pesquisa_col;
- ++pesquisa_linha;
- case upleft:
- --pesquisa_col;
- --pesquisa_linha;
- case right:
- ++pesquisa_col;
- case upright:
- ++pesquisa_col;
- --pesquisa_linha;
- case downright:
- ++pesquisa_col;
- ++pesquisa_linha;
- case down:
- ++pesquisa_linha;
- case up:
- --pesquisa_linha;
- }
- }
- public void procurar(String word){
- boolean flag = false;
- while(!flag){
- System.out.println(linha);
- System.out.println(col);
- primeiraLetra(word);
- flag = avaliar(word,Direction.left);
- flag = avaliar(word,Direction.downleft);
- flag = avaliar(word,Direction.upleft);
- flag = avaliar(word,Direction.right);
- flag = avaliar(word,Direction.downright);
- flag = avaliar(word,Direction.upright);
- flag = avaliar(word,Direction.up);
- flag = avaliar(word,Direction.down);
- if(col<grelha.length){
- col++;
- }else if(linha<grelha.length){
- linha++;
- col=0;
- }else{
- System.out.print("Não encontrou");
- break;
- }
- }
- }
- public boolean avaliar(String word, Direction tipe){
- if(procurar_palavra(word,tipe)){
- System.out.printf("%-10s%-5d%d,%d%10s",word.toUpperCase(),word.length(),linha,col,tipe);
- return true;
- }else{
- pesquisa_linha = linha;
- pesquisa_col = col;
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement