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.