Link to home
Start Free TrialLog in
Avatar of Fatima113
Fatima113

asked on

Problem with word puzzle program

Hi All,
I need a help with my program. My program is about finding a word in 2D array horizontal, vertical, or diagonal in any direction but I have some problems with finding the word in the diagonal direction. I don't know what I did wrong.Can anyone help me to solve this problem ?
my code is below:
// my code in the main class
for (int i=0;i<wordlist.length;i++){
           str1=wordlist[i];
           str1=str1.toUpperCase();
               for( int j=0;j<puzzleinput.length;j++){
                  for(int l=0;l<puzzleinput.length;l++){
                      if (str1.charAt(0)==puzzleinput[j][l]){
                          if (HorizForward(str1,j,l)){
                              direction="hf";
                              SolutionArray(str1,j,l,direction);}
                          else if (HorizBackward(str1,j,l)){
                              direction="hb";
                              SolutionArray(str1,j,l,direction);}
                          else if (VerticalUp(str1,j,l)){
                              direction="vu";
                              SolutionArray(str1,j,l,direction);}
                          else if (VerticalDown(str1,j,l)){
                              direction="vd";
                              SolutionArray(str1,j,l,direction);}
                          else if (DiagonalUpRight(str1,j,l)==true){
                              direction="dur";
                              SolutionArray(str1,j,l,direction);}
                          else if (DiagonalUpLeft(str1,j,l)==true){
                              direction="dul";
                              SolutionArray(str1,j,l,direction);}
                          else if (DiagonalDownRight(str1,j,l)==true){
                              direction="ddr";
                              SolutionArray(str1,j,l,direction);}
                          else if (DiagonalDownLeft(str1,j,l)==true){
                              direction="ddl";
                              SolutionArray(str1,j,l,direction);}
                      }}
                      }

// this is all the functions that I have used

public static boolean HorizForward(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(j>49)break;
            if (str.charAt(k)==puzzleinput[i][j])
             j=j+1;   
            else break;
        }
        if (k==str.length()) return true;
        else 
            return false;
    }
    
    public static boolean HorizBackward(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(j<0)break;
            if (str.charAt(k)==puzzleinput[i][j])
                j=j-1;     
        else
                break;}
        if (k==str.length()) return true;
        else 
            return false;
    }
    
    public static boolean VerticalUp(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(i<0)break;
            if (str.charAt(k)==puzzleinput[i][j])
                i=i-1;     
        else
                break;}
        if (k==str.length()) return true;
        else 
            return false;
    }
    
    public static boolean VerticalDown(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(i>49)break;
            if (str.charAt(k)==puzzleinput[i][j])
                i=i+1;     
        else
                break;}
        if (k==str.length()) return true;
        else 
            return false;
    }
    
    public static boolean DiagonalUpRight(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(j<0 ||j>49 || i>49||i<0)break;
            if (str.charAt(k)==puzzleinput[i][j]){
                i=i-1;j=j+1; }    
            else break;}
        if (k==str.length()) return true;
        else return false;
    }
    
    public static boolean DiagonalUpLeft(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(j<0 ||j>49 || i>49||i<0)break;
            if (str.charAt(k)==puzzleinput[i][j]){
                i--;j--; }    
        else
                break;}
        if (k==str.length()) return true;
        else 
            return false;
    }
    
    public static boolean DiagonalDownRight(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(j<0 ||j>49 || i>49||i<0)break;
            if (str.charAt(k)==puzzleinput[i][j]){
                i++;j++; }    
        else
                break;}
        if (k==str.length()) return true;
        else 
            return false;
    }
    public static boolean DiagonalDownLeft(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(j<0 ||j>49 || i>49||i<0)break;
            if (str.charAt(k)==puzzleinput[i][j]){
                i=i+1;j=j-1; }    
            else 
                break;}
        if (k==str.length()) return true;
        else 
            return false;
    }
    
    
    public static void SolutionArray(String str,int i, int j, String d){
         
        switch (d){
            case "hf":
                for(int k=0;k<str.length();k++){
                    solutionarray[i][j]=puzzleinput[i][j];
                    j=j+1;
                } break;
            case "hb":
                for(int k=0;k<str.length();k++){
                    solutionarray[i][j]=puzzleinput[i][j];
                    j=j-1;
                } break;
            case "print":
                for(int k=0;k<solutionarray.length;k++)
                     System.out.println(solutionarray[k]);
            case "vu":
                for(int k=0;k<str.length()-1;k++){
                solutionarray[i][j]=puzzleinput[i][j];
                i=i-1;
                }
            case "vd":
                for(int k=0;k<str.length();k++){
                solutionarray[i][j]=puzzleinput[i][j];
                i=i+1;
                }
            case "dur":
                for(int k=str.length()-1;k>=0;k--){
                solutionarray[i][j]=puzzleinput[i][j];
                i=i-1;j=j+1;
                }
            case "dul":
                for(int k=str.length()-1;k>=0;k--){
                    if(i>49||j>49)break;
                solutionarray[i][j]=puzzleinput[i][j];
                i=i+1;j=j+1;
                }
            case "ddr":
                for(int k=str.length()-1;k>=0;k--){
                    if(i>49||j>49)break;
                solutionarray[i][j]=puzzleinput[i][j];
                i=i+1;j=j+1;
                }
            case "ddl":
                if(i>49||j>49)break;
                for(int k=str.length()-1;k>=0;k--){    
                solutionarray[i][j]=puzzleinput[i][j];
               i=i+1;j=j-1;
                }
                
    }
        
                
        
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Fatima113
Fatima113

ASKER

This is my entire program
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package wordpuzzle;
import java.util.*;
import java.io.*;

/**
 *
 * @author Fadel
 */
public class WordPuzzle {
static char solutionarray[][]=new char[50][50];
static char puzzleinput[][]= new char[50][50];
     
    public static boolean HorizForward(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(j>49)break;
            if (str.charAt(k)==puzzleinput[i][j])
             j=j+1;   
            else break;
        }
        if (k==str.length()) return true;
        else 
            return false;
    }
    
    public static boolean HorizBackward(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(j<0)break;
            if (str.charAt(k)==puzzleinput[i][j])
                j=j-1;     
        else
                break;}
        if (k==str.length()) return true;
        else 
            return false;
    }
    
    public static boolean VerticalUp(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(i<0)break;
            if (str.charAt(k)==puzzleinput[i][j])
                i=i-1;     
        else
                break;}
        if (k==str.length()) return true;
        else 
            return false;
    }
    
    public static boolean VerticalDown(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(i>49)break;
            if (str.charAt(k)==puzzleinput[i][j])
                i=i+1;     
        else
                break;}
        if (k==str.length()) return true;
        else 
            return false;
    }
    
    public static boolean DiagonalUpRight(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(j<0 ||j>49 || i>49||i<0)break;
            if (str.charAt(k)==puzzleinput[i][j]){
                i=i-1;j=j+1; }    
            else break;}
        if (k==str.length()) return true;
        else return false;
    }
    
    public static boolean DiagonalUpLeft(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(j<0 ||j>49 || i>49||i<0)break;
            if (str.charAt(k)==puzzleinput[i][j]){
                i--;j--; }    
        else
                break;}
        if (k==str.length()) return true;
        else 
            return false;
    }
    
    public static boolean DiagonalDownRight(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(j<0 ||j>49 || i>49||i<0)break;
            if (str.charAt(k)==puzzleinput[i][j]){
                i++;j++; }    
        else
                break;}
        if (k==str.length()) return true;
        else 
            return false;
    }
    public static boolean DiagonalDownLeft(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(j<0 ||j>49 || i>49||i<0)break;
            if (str.charAt(k)==puzzleinput[i][j]){
                i=i+1;j=j-1; }    
            else 
                break;}
        if (k==str.length()) return true;
        else 
            return false;
    }
    
    
    public static void SolutionArray(String str,int i, int j, String d){
         
        switch (d){
            case "hf":
                for(int k=0;k<str.length();k++){
                    solutionarray[i][j]=puzzleinput[i][j];
                    j=j+1;
                } break;
            case "hb":
                for(int k=0;k<str.length();k++){
                    solutionarray[i][j]=puzzleinput[i][j];
                    j=j-1;
                } break;
            case "print":
                for(int k=0;k<solutionarray.length;k++)
                     System.out.println(solutionarray[k]);
            case "vu":
                for(int k=0;k<str.length()-1;k++){
                solutionarray[i][j]=puzzleinput[i][j];
                i=i-1;
                }
            case "vd":
                for(int k=0;k<str.length();k++){
                solutionarray[i][j]=puzzleinput[i][j];
                i=i+1;
                }
            case "dur":
                for(int k=str.length()-1;k>=0;k--){
                solutionarray[i][j]=puzzleinput[i][j];
                i=i-1;j=j+1;
                }
            case "dul":
                for(int k=str.length()-1;k>=0;k--){
                    if(i>49||j>49)break;
                solutionarray[i][j]=puzzleinput[i][j];
                i=i+1;j=j+1;
                }
            case "ddr":
                for(int k=str.length()-1;k>=0;k--){
                    if(i>49||j>49)break;
                solutionarray[i][j]=puzzleinput[i][j];
                i=i+1;j=j+1;
                }
            case "ddl":
                if(i>49||j>49)break;
                for(int k=str.length()-1;k>=0;k--){    
                solutionarray[i][j]=puzzleinput[i][j];
               i=i+1;j=j-1;
                }
                
    }
        
                
        
    }
    public static void main(String[] args) {
        // TODO code application logic here
   
   
   String [] wordlist=new String [20];
   try {
			Scanner scan=new Scanner(new File("puzzleinput.txt"));
                        String str="";
                        for(int i=0;i<puzzleinput.length;i++){
                         str=scan.next();
                         puzzleinput[i]=str.toCharArray();
                        
                        }
                        
		} catch(FileNotFoundException e) {
			System.out.println("Couldn't open file \"puzzleinput.txt\"");
		}
   try {
			Scanner scan=new Scanner(new File("wordlist.txt"));
                        String str="";
                        for(int i=0;i<wordlist.length;i++){
                         str=scan.next();
                         wordlist[i]=str;
                        
                        }
                        
		} catch(FileNotFoundException e) {
			System.out.println("Couldn't open file \"wordlist.txt\"");
		}
   String str1="";
   String direction="";
   
   for (int i=0;i<wordlist.length;i++){
           str1=wordlist[i];
           str1=str1.toUpperCase();
               for( int j=0;j<puzzleinput.length;j++){
                  for(int l=0;l<puzzleinput.length;l++){
                      if (str1.charAt(0)==puzzleinput[j][l]){
                          if (HorizForward(str1,j,l)){
                              direction="hf";
                              SolutionArray(str1,j,l,direction);}
                          else if (HorizBackward(str1,j,l)){
                              direction="hb";
                              SolutionArray(str1,j,l,direction);}
                          else if (VerticalUp(str1,j,l)){
                              direction="vu";
                              SolutionArray(str1,j,l,direction);}
                          else if (VerticalDown(str1,j,l)){
                              direction="vd";
                              SolutionArray(str1,j,l,direction);}
                          else if (DiagonalUpRight(str1,j,l)==true){
                              direction="dur";
                              SolutionArray(str1,j,l,direction);}
                          else if (DiagonalUpLeft(str1,j,l)==true){
                              direction="dul";
                              SolutionArray(str1,j,l,direction);}
                          else if (DiagonalDownRight(str1,j,l)==true){
                              direction="ddr";
                              SolutionArray(str1,j,l,direction);}
                          else if (DiagonalDownLeft(str1,j,l)==true){
                              direction="ddl";
                              SolutionArray(str1,j,l,direction);}
                      }}
                      }
                          
                      
                      
                      
               }
               
          for (int i=0;i<puzzleinput.length;i++)
        System.out.println(puzzleinput[i]);
      for (int i=0;i<wordlist.length;i++)
        System.out.println(wordlist[i]);
      String str2="";
      int a=50,b=50;
      
      SolutionArray(str2,a,b,"print");
            
       }
    
          
   
    }


    

Open in new window

This is my new code after adding break but still the output for the diagonal direction is wrong
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package wordpuzzle;
import java.util.*;
import java.io.*;

/**
 *
 * @author Fadel
 */
public class WordPuzzle {
static char solutionarray[][]=new char[50][50];
static char puzzleinput[][]= new char[50][50];
     
    public static boolean HorizForward(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(j>49)break;
            if (str.charAt(k)==puzzleinput[i][j])
             j=j+1;   
            else break;
        }
        if (k==str.length()) return true;
        else 
            return false;
    }
    
    public static boolean HorizBackward(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(j<0)break;
            if (str.charAt(k)==puzzleinput[i][j])
                j=j-1;     
        else
                break;}
        if (k==str.length()) return true;
        else 
            return false;
    }
    
    public static boolean VerticalUp(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(i<0)break;
            if (str.charAt(k)==puzzleinput[i][j])
                i=i-1;     
        else
                break;}
        if (k==str.length()) return true;
        else 
            return false;
    }
    
    public static boolean VerticalDown(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(i>49)break;
            if (str.charAt(k)==puzzleinput[i][j])
                i=i+1;     
        else
                break;}
        if (k==str.length()) return true;
        else 
            return false;
    }
    
    public static boolean DiagonalUpRight(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(j<0 ||j>49 || i>49||i<0)break;
            if (str.charAt(k)==puzzleinput[i][j]){
                i=i-1;j=j+1; }    
            else break;}
        if (k==str.length()) return true;
        else return false;
    }
    
    public static boolean DiagonalUpLeft(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(j<0 ||j>49 || i>49||i<0)break;
            if (str.charAt(k)==puzzleinput[i][j]){
                i--;j--; }    
        else
                break;}
        if (k==str.length()) return true;
        else 
            return false;
    }
    
    public static boolean DiagonalDownRight(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(j<0 ||j>49 || i>49||i<0)break;
            if (str.charAt(k)==puzzleinput[i][j]){
                i++;j++; }    
        else
                break;}
        if (k==str.length()) return true;
        else 
            return false;
    }
    public static boolean DiagonalDownLeft(String str,int i, int j){
        int k;
        for (k=0;k<str.length();k++){
            
            if(j<0 ||j>49 || i>49||i<0)break;
            if (str.charAt(k)==puzzleinput[i][j]){
                i=i+1;j=j-1; }    
            else 
                break;}
        if (k==str.length()) return true;
        else 
            return false;
    }
    
    
    public static void SolutionArray(String str,int i, int j, String d){
         
        switch (d){
            case "hf":
                for(int k=0;k<str.length();k++){
                    solutionarray[i][j]=puzzleinput[i][j];
                    j=j+1;
                } break;
            case "hb":
                for(int k=0;k<str.length();k++){
                    solutionarray[i][j]=puzzleinput[i][j];
                    j=j-1;
                } break;
            case "print":
                for(int k=0;k<solutionarray.length;k++)
                     System.out.println(solutionarray[k]);break;
            case "vu":
                for(int k=0;k<str.length();k++){
                solutionarray[i][j]=puzzleinput[i][j];
                i=i-1;
                }break;
            case "vd":
                for(int k=0;k<str.length();k++){
                solutionarray[i][j]=puzzleinput[i][j];
                i=i+1;
                }break;
            case "dur":
                for(int k=0;k<str.length();k++){
                solutionarray[i][j]=puzzleinput[i][j];
                i=i-1;j=j+1;
                }break;
            case "dul":
                for(int k=0;k<str.length();k++){
                    if(i>49||j>49)break;
                solutionarray[i][j]=puzzleinput[i][j];
                i=i+1;j=j-1;
                }break;
            case "ddr":
                for(int k=0;k<str.length();k++){
                    if(i>49||j>49)break;
                solutionarray[i][j]=puzzleinput[i][j];
                i=i+1;j=j+1;
                }break;
            case "ddl":
                if(i>49||j>49)break;
                for(int k=0;k<str.length();k++){    
                solutionarray[i][j]=puzzleinput[i][j];
               i=i+1;j=j-1;
                }break;
                
    }
        
                
        
    }
    public static void main(String[] args) {
        // TODO code application logic here
   
   
   String [] wordlist=new String [20];
   try {
			Scanner scan=new Scanner(new File("puzzleinput.txt"));
                        String str="";
                        for(int i=0;i<puzzleinput.length;i++){
                         str=scan.next();
                         puzzleinput[i]=str.toCharArray();
                        
                        }
                        
		} catch(FileNotFoundException e) {
			System.out.println("Couldn't open file \"puzzleinput.txt\"");
		}
   try {
			Scanner scan=new Scanner(new File("wordlist.txt"));
                        String str="";
                        for(int i=0;i<wordlist.length;i++){
                         str=scan.next();
                         wordlist[i]=str;
                        
                        }
                        
		} catch(FileNotFoundException e) {
			System.out.println("Couldn't open file \"wordlist.txt\"");
		}
   String str1="";
   String direction="";
   
   for (int i=0;i<wordlist.length;i++){
           str1=wordlist[i];
           str1=str1.toUpperCase();
               for( int j=0;j<puzzleinput.length;j++){
                  for(int l=0;l<puzzleinput.length;l++){
                      if (str1.charAt(0)==puzzleinput[j][l]){
                          if (HorizForward(str1,j,l)){
                              direction="hf";
                              SolutionArray(str1,j,l,direction);}
                          else if (HorizBackward(str1,j,l)){
                              direction="hb";
                              SolutionArray(str1,j,l,direction);}
                          else if (VerticalUp(str1,j,l)){
                              direction="vu";
                              SolutionArray(str1,j,l,direction);}
                          else if (VerticalDown(str1,j,l)){
                              direction="vd";
                              SolutionArray(str1,j,l,direction);}
                          else if (DiagonalUpRight(str1,j,l)==true){
                              direction="dur";
                              SolutionArray(str1,j,l,direction);}
                          else if (DiagonalUpLeft(str1,j,l)==true){
                              direction="dul";
                              SolutionArray(str1,j,l,direction);}
                          else if (DiagonalDownRight(str1,j,l)==true){
                              direction="ddr";
                              SolutionArray(str1,j,l,direction);}
                          else if (DiagonalDownLeft(str1,j,l)==true){
                              direction="ddl";
                              SolutionArray(str1,j,l,direction);}
                      }}
                      }
                          
                      
                      
                      
               }
               
          for (int i=0;i<puzzleinput.length;i++)
        System.out.println(puzzleinput[i]);
      for (int i=0;i<wordlist.length;i++)
        System.out.println(wordlist[i]);
      String str2="";
      int a=50,b=50;
      
      SolutionArray(str2,a,b,"print");
            
       }
    
          
   
    }


    

Open in new window

Thanks mccarl I followed your suggestions and my program worked fine. Thanks a lot.
You're welcome! :)