import java.util.*;
import java.lang.*;
import java.io.*;
public class MyBoggle2{
static final int ROWS = 4, COLS = 4;
static final int LOW = 'A', HIGH = 'Z';
static char[][] board = new char[4][4];
public static void main(String[] args) throws IOException, FileNotFoundException{
ArrayList<String> found = new ArrayList<String>();
ArrayList<String> wrong = new ArrayList<String>();
ArrayList<String> real = new ArrayList<String>();
String temp;
String word;
int size;
int words;
int index = 0;
char restart = 'Y';
Scanner input = new Scanner(System.in);
File dic = new File ("bogdict.txt");
FileReader in = new FileReader(dic);
BufferedReader readFile = new BufferedReader(in);
while (restart != 'N') {
word = " ";
makeBoard();
System.out.println("Let's play a game of Boggle: ");
printBoard();
real = new ArrayList<String>();
wrong = new ArrayList<String>();
found = new ArrayList<String>();
temp = readFile.readLine();
while(temp != null) {
if (checkWord(temp) == true) {
real.add(temp);
}
temp = readFile.readLine();
}
size = real.size();
System.out.println("Enter words or ZZ to quit: ");
while (word.equals("zz") == false){
word = input.next().toLowerCase();
System.out.print("");
index = 0;
if (!word.equals("zz"))
while (index < size && real.get(index) != word){
if (word.equals(real.get(index))) {
found.add(real.get(index));
real.set(index, "");
}
index += 1;
}
}
System.out.println("Words you guessed correctly: ");
for (String i : found) {
System.out.println(i);
}
words = real.size() - found.size();
System.out.println("Words the computer found that you could have guessed: " + words);
System.out.println("Words you could have guessed: ");
for (String i : real) {
if (!i.equals("")) System.out.println(i);
}
}
in.close();
readFile.close();
}
public static void printBoard() {
for (int y = 0; y <= 3; y++) {
for (int x = 0; x <= 3; x++) {
System.out.print(board[y][x] + " ");
}
System.out.println("");
}
}
public static void makeBoard() {
for (int y = 0; y <= 3; y++) {
for (int x = 0; x <= 3; x++) {
board[y][x]=(char)((int)'a'+Math.random()*((int)'z'-(int)'a'+1));
}
}
}
private static boolean findWord(int row, int col, String word) {
if (word.length() == 0) {return true;}
if (row < 0 || row >= ROWS) {return false;}
if (col < 0 || col >= COLS) {return false;}
if (word.charAt(0) != board[row][col]) {return false;}
char saveChar = board[row][col];
board[row][col] = ' ';
for (int r = row - 1; r <= row + 1; r++) {
for (int c = col - 1; c <= col + 1; c++) {
if (findWord(r, c, word.substring(1))) {
board[row][col] = saveChar;
return true;
}
}
}
board[row][col] = saveChar;
return (false);
}
private static boolean checkWord(String word) {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (findWord(row, col, word)) return true;
}
}
return (false);
}
}