Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. public class SopaLetras {
  2.     private char[][] grelha;
  3.     private int linha;
  4.     private int col;
  5.     private int pesquisa_linha;
  6.     private int pesquisa_col;
  7.     public SopaLetras(char[][] grelha){
  8.         this.grelha=grelha;
  9.         this.linha=0;
  10.         this.col=0;
  11.     }
  12. public static void main(String[] args) throws FileNotFoundException {
  13.        
  14.         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'}};
  15.         String[] pal ={"ana","art","arroz"};
  16.         for(int i=0; i<teste.length;i++){
  17.             for(int j=0; j<teste.length;j++){
  18.                 System.out.print(teste[i][j] + " ");
  19.             }
  20.             System.out.println();
  21.         }
  22.         SopaLetras sp = new SopaLetras(teste);
  23.         sp.procurar(pal[1]);
  24.        
  25.     }
  26.    
  27.     private boolean primeiraLetra(String word){
  28.        
  29.         for(int i=linha;i<grelha.length;i++){
  30.             for(int j=col;j<grelha[i].length;j++){
  31.                 if(word.charAt(0)==grelha[i][j]){
  32.                     linha = i;
  33.                     col = j;
  34.                     pesquisa_linha=i;
  35.                     pesquisa_col=j;
  36.                     return true;
  37.                 }
  38.             }
  39.         }
  40.         return false;
  41.     }
  42.     public boolean procurar_palavra(String word, Direction tipe){
  43.         String temp="";
  44.         int c=0;
  45.         while(pesquisa_linha>=0 && pesquisa_col>=0 && pesquisa_linha<grelha.length && pesquisa_col<grelha.length && c<word.length()){
  46.             temp+=grelha[pesquisa_linha][pesquisa_col];
  47.            
  48.             direction(tipe);
  49.             System.out.println("col"+pesquisa_col);
  50.             c++;
  51.            
  52.         }
  53.         System.out.println(tipe+": "+temp);
  54.         if(word.equalsIgnoreCase(temp)){
  55.             return true;
  56.         }
  57.         return false;
  58.     }
  59.     public void direction(Direction tipe){
  60.         switch(tipe){
  61.         case left:
  62.             --pesquisa_col;
  63.         case downleft:
  64.             --pesquisa_col;
  65.             ++pesquisa_linha;
  66.         case upleft:
  67.             --pesquisa_col;
  68.             --pesquisa_linha;
  69.         case right:
  70.             ++pesquisa_col;
  71.         case upright:
  72.             ++pesquisa_col;
  73.             --pesquisa_linha;
  74.         case downright:
  75.             ++pesquisa_col;
  76.             ++pesquisa_linha;
  77.         case down:
  78.             ++pesquisa_linha;
  79.         case up:
  80.             --pesquisa_linha;
  81.         }
  82.        
  83.     }
  84.     public void procurar(String word){
  85.         boolean flag = false;
  86.         while(!flag){
  87.             System.out.println(linha);
  88.             System.out.println(col);
  89.             primeiraLetra(word);
  90.             flag = avaliar(word,Direction.left);
  91.             flag = avaliar(word,Direction.downleft);
  92.             flag = avaliar(word,Direction.upleft);
  93.             flag = avaliar(word,Direction.right);
  94.             flag = avaliar(word,Direction.downright);
  95.             flag = avaliar(word,Direction.upright);
  96.             flag = avaliar(word,Direction.up);
  97.             flag = avaliar(word,Direction.down);
  98.             if(col<grelha.length){
  99.                 col++;
  100.             }else if(linha<grelha.length){
  101.                 linha++;
  102.                 col=0;
  103.             }else{
  104.                 System.out.print("Não encontrou");
  105.                 break;
  106.             }
  107.            
  108.         }
  109.        
  110.        
  111.     }
  112.     public boolean avaliar(String word, Direction tipe){
  113.         if(procurar_palavra(word,tipe)){
  114.             System.out.printf("%-10s%-5d%d,%d%10s",word.toUpperCase(),word.length(),linha,col,tipe);
  115.             return true;
  116.         }else{
  117.             pesquisa_linha = linha;
  118.             pesquisa_col = col;
  119.             return false;
  120.         }
  121.     }
  122.    
  123.    
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement