O dia todo nessa programa e até que enfim consegui terminar… Aqui vai o codigo do driver galera e o prgrama em si galera… [caça-palavras]
/**
* This class allows a game of WordFind to be created and words to be sought.
*
* @author Kaio Jonathas
* @version April 25, 2013
*/
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class WordFind
{
private char [][] grid; // the grid of letters
public boolean buildGrid(String fileName)
{
String line;
try
{
Scanner file;
file = new Scanner (new File (fileName));
int rows = file.nextInt();
int columns = file.nextInt();
grid = new char[rows][columns];
line = file.nextLine();
for (int row = 0; row < grid.length; row++)
{
line = file.nextLine();
for (int col = 0; col < grid[0].length; col++)
{
grid[row][col] = line.charAt(col*2);
}
}
return true;
}
catch (FileNotFoundException exception)
{
return false;
}
}
public String toString()
{
String result = " 1 2 3 4 5 6 7 8 9 10\n"+" "+lineSeparator()+"\n";
int i = 1;
for (int row = 0; row < grid.length; row++)
{
if(i!=10)
{
result = result + i++ +" |";
}
else
{
result = result + i++ +" |";
}
for (int col = 0; col < grid[0].length; col++)
{
result = result + " " +grid[row][col];
}
result = result + "\n";
}
return result;
}
private String lineSeparator()
{
String result = "";
for (int i = 0; i < grid[0].length+1; i++)
{
if(i == 0)
{
result += "+";
}
else
{
result += "---";
}
}
return result;
}
public void find(String word)
{
int n = 0; //Number of occurences in a specific position
int nTotal = 0; //Total number of occurrences
for (int row = 0; row < grid.length; row++)
{
for (int col = 0; col < grid[0].length; col++)
{
n = 0; // Clearing n to start counting again in another position
if(grid[row][col] == word.charAt(0))
{
if(lookInDirections(0,word,row,col,1))
{
n = n+1;
nTotal = nTotal +1;
}
if(lookInDirections(0,word,row,col,2))
{
n = n+1;
nTotal = nTotal +1;
}
if(lookInDirections(0,word,row,col,3))
{
n = n+1;
nTotal = nTotal +1;
}
if(lookInDirections(0,word,row,col,4))
{
n = n+1;
nTotal = nTotal +1;
}
if(lookInDirections(0,word,row,col,5))
{
n = n+1;
nTotal = nTotal +1;
}
if(lookInDirections(0,word,row,col,6))
{
n = n+1;
nTotal = nTotal +1;
}
if(lookInDirections(0,word,row,col,7))
{
n = n+1;
nTotal = nTotal +1;
}
if(lookInDirections(0,word,row,col,8))
{
n = n+1;
nTotal = nTotal +1;
}
}
if(n>0)
{
System.out.println("... found "+ n +" occurrence(s) at row "+ (row+1) +", column " + (col+1));
}
}
}
System.out.println("Found " +nTotal+ " total occurrences.\n");
}
//1 - North
//2 - Northeast
//3 - East
//4 - Southeast
//5 - South
//6 - Southwest
//7 - West
//8 - Northwest
private boolean lookInDirections(int index, String word, int x, int y, int direction)
{
if(direction == 1)//if direction is north I just have to worry about x because we are not changing y
{
if(word.length()==1)
{
return true;
}
else if (index+1 == word.length())
{
return true;
}
if(x>0)
{
if(grid[x-1][y] == word.charAt(index+1))
{
return lookInDirections(index+1, word, x-1, y, 1); //I have to take care of the bounds
}
else
{
return false;
}
}
}
if(direction == 2)
{
if(word.length()==1)
{
return false; //because I have already returned true above and if I return true again it will count a letter 8 times, like if it was in the 8 directions
}
if(index+1 == word.length())
{
return true;
}
if(x>0 && y<grid[0].length-1)
{
if(grid[x-1][y+1] == word.charAt(index+1))
{
return lookInDirections(index+1, word, x-1, y+1, 2); //I have to take care of the bounds
}
else
{
return false;
}
}
}
if(direction == 3)
{
if(word.length()==1)
{
return false;
}
if(index+1 == word.length())
{
return true;
}
if(y<grid[0].length-1)
{
if(grid[x][y+1] == word.charAt(index+1))
{
return lookInDirections(index+1, word, x, y+1, 3); //I have to take care of the bounds
}
else
{
return false;
}
}
}
if(direction == 4)
{
if(word.length()==1)
{
return false;
}
if(index+1 == word.length())
{
return true;
}
if(x<grid[0].length-1 && y<grid[0].length-1)
{
if(grid[x+1][y+1] == word.charAt(index+1))
{
return lookInDirections(index+1, word, x+1, y+1, 4); //I have to take care of the bounds
}
else
{
return false;
}
}
}
if(direction == 5)
{
if(word.length()==1)
{
return false;
}
if(index+1 == word.length())
{
return true;
}
if(x<grid[0].length-1)
{
if(grid[x+1][y] == word.charAt(index+1))
{
return lookInDirections(index+1, word, x+1, y, 5); //I have to take care of the bounds
}
else
{
return false;
}
}
}
if(direction == 6)
{
if(word.length()==1)
{
return false;
}
if(index+1 == word.length())
{
return true;
}
if(y>0 && x<grid[0].length-1)
{
if(grid[x+1][y-1] == word.charAt(index+1))
{
return lookInDirections(index+1, word, x+1, y-1, 6); //I have to take care of the bounds
}
else
{
return false;
}
}
}
if(direction == 7)
{
if(word.length()==1)
{
return false;
}
if(index+1 == word.length())
{
return true;
}
if(y>0)
{
if(grid[x][y-1] == word.charAt(index+1))
{
return lookInDirections(index+1, word, x, y-1, 7); //I have to take care of the bounds
}
else
{
return false;
}
}
}
if(direction == 8)
{
if(word.length()==1)
{
return false;
}
if(index+1 == word.length())
{
return true;
}
if(y>0 && x>0)
{
if(grid[x-1][y-1] == word.charAt(index+1))
{
return lookInDirections(index+1, word, x-1, y-1, 8); //I have to take care of the bounds
}
else
{
return false;
}
}
}
return false;
}
}