We have this program we are supposed to write that takes input from a file with a random word puzzle throws it into an array and prompts the user for a word input or to quit. So far I can get the program to find elements in forward/reverse, up/down, by finding stuff in diagonal elements is proving to be a bit tricky so far I have this (apologize for it being so long) code

import java.util.*;
import java.io.*;
import java.lang.String;

public class pg6a
{
  private static char[][] puzzle;
  private static int sizeHorz;
  private static int sizeVert;
  private static String filename, foundEntry ="";
  private static String foundEntryReverse ="";
  private static String foundWordList = "found word(s): ";
  private static Scanner fromFile;
  private static char lineEntry;
  private static boolean test = true;

   public static void main(String[] args)throws IOException 
  {
     Scanner keyboard = new Scanner(System.in); 
     System.out.println("Enter the name of the file"); 
     filename=keyboard.next(); 

     fromFile = new Scanner(new FileReader(filename));

        sizeVert = fromFile.nextInt();
        sizeHorz = fromFile.nextInt();

     gridDefine(fromFile);
     display();
     while (!foundEntry.equals("q"))
     {
        System.out.println("Enter a word to guess or q to (q)uit");
        foundEntry = keyboard.next();
        StringBuffer reverseBuffer = new StringBuffer(foundEntry);
        reverseBuffer = reverseBuffer.reverse();
        foundEntryReverse = reverseBuffer.toString();
        gameFunction();
        display();
     }
  }

   public static void gridDefine(Scanner Input)
  {
     char character;
     String line = "";
     puzzle = new char[sizeVert][sizeHorz];
     Input.nextLine();

     for (int r=0; r < sizeVert; r++)
     {
        line = Input.nextLine().toLowerCase();

        for (int c=0; c < sizeHorz; c++)
        {
           puzzle[r][c] = line.charAt(c);           
        }
     }        
  }  

   public static void display()  
  {
     String line = "";
     for (int i=0; i <= sizeVert; i++)
        line += "";
     for (int r=0; r < sizeVert; r++)
     {
        for (int c=0; c < sizeHorz; c++)
        {   System.out.print(puzzle[r][c] + " ");
           if (c < sizeHorz-1 && c%3 == 2)
              System.out.print("");
        }
        System.out.println();
     }
  } 

   public static void gameFunction()
  {
     int count = 1;
     if (foundEntry.equalsIgnoreCase("q"))
     {
        System.out.println("Thanks for playing");
        return;
     }
     else
     {
        count = 0;
        lineEntry = foundEntry.charAt(count);

        for (int r=0; r < sizeVert; r++)
        { 
           for (int c=0; c < (sizeHorz - foundEntry.length()); c++)
           {  String check = "";
              for (count=0; count < foundEntry.length(); count++)
                 check = check + puzzle[r][c+count];
              if (check.equalsIgnoreCase(foundEntry) || check.equalsIgnoreCase(foundEntryReverse)) 
              {
                 System.out.println(r + " " + c);
                 foundWordList += foundEntry + " ";
                 System.out.println("word found! ");
                 System.out.println(foundWordList); 
                 for (count=0; count < foundEntry.length(); count++)
                 {
                    puzzle[r][c+count] = Character.toUpperCase(puzzle[r][c+count]);
                 }
              }
           }
        }
        for (int r=0; r < (sizeVert - foundEntry.length()); r++)
        { 
           for (int c=0; c < sizeHorz; c++)
           {  String check = "";
              for (count=0; count < foundEntry.length(); count++)
                 check = check + puzzle[r+count][c];
              if (check.equalsIgnoreCase(foundEntry) || check.equalsIgnoreCase(foundEntryReverse)) 
              {
                 System.out.println(r + " " + c);
                 foundWordList += foundEntry + " ";
                 System.out.println("word found! ");
                 System.out.println(foundWordList); 
                 for (count=0; count < foundEntry.length(); count++)
                 {
                    puzzle[r+count][c] = Character.toUpperCase(puzzle[r+count][c]);
                 }
              }
           }
        } 
            for (int r=0; r < (sizeVert - foundEntry.length()); r++)
        { 
           for (int c=0; c < (sizeHorz - foundEntry.length()); c++)
           {  String check = "";
              for (count=0; count < foundEntry.length(); count++)
                 check = check + puzzle[r+count][c+count];
              if (check.equalsIgnoreCase(foundEntry) || check.equalsIgnoreCase(foundEntryReverse)) 
              {
                 System.out.println(r + " " + c);
                 foundWordList += foundEntry + " ";
                 System.out.println("word found! ");
                 System.out.println(foundWordList); 
                 for (count=0; count < foundEntry.length(); count++)
                 {
                    puzzle[r+count][c+count] = Character.toUpperCase(puzzle[r+count][c+count]);
                 }
              }
           }
        }          
     }

  }
}

The first two for loop groups cover the forward/reverse and up/down stuff but the last one is stumping me. I wrote it so it would have a counter that increases each time it goes through the loop just like with the first two for groups but this time it ups both the row and column variables [r][c] instead of just [r] or [c] individually like I did in the first two.

here is a sample input file

10 7
ABCDEFt
SsGKLaN
OPpRcJW
PLDrJWO
ELKJiIJ
SLeOJnL
happyTg
BEoREEa
JFhSwen
ALSOEId

However it loops through when I enter a word like "happy" it find the word, confirms it is found and converts the word to uppercase in the array. I can't get it to find a word like "cat" which is a diagonal entry at the top. I will enter the word it will do nothing (meaning it couldn't find anything) and prompt for input again (no compile errors). Any suggestions what methods would be the best way to approach this would be greatly appreciated.

Recommended Answers

All 2 Replies

You need another loop,

for (int r=0; r < (sizeVert - foundEntry.length()); r++)
  { 
	for (int c=sizeHorz-1;c>=foundEntry.length(); c--)
	{ String check = "";
	  for (count=0; count < foundEntry.length(); count++)
		check = check + puzzle[r+count][c-count];
	if (check.equalsIgnoreCase(foundEntry) || check.equalsIgnoreCase(foundEntryReverse)) 
	{
	System.out.println(r + " " + c);
	foundWordList += foundEntry + " ";
	System.out.println("word found! ");
	System.out.println(foundWordList); 
	for (count=0; count < foundEntry.length(); count++)
	{
	puzzle[r+count][c-count] = Character.toUpperCase(puzzle[r+count][c-count]);
	}
	}
      }
 }

Is there an admin that could delete the top half of the code in my post or at least let me edit the post? I guess I forgot some of the rules of the course I am taking and am not allowed to post the ENTIRE code (just parts).

As for the loop adatapost provided thanks for your help and it seems to work for parts of the diagonal entries but it doesn't work for all of them in that direction. It is proving to be difficult to identify when it is going in and out of the array and I seem to be overcompensating for it at some points

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.