Newer
Older
KA-Visual / src / showGrid.java
import java.util.ArrayList;
import java.util.List;

public class showGrid {
	private List<String[]> gridList = new ArrayList<String[]>();
	
	public void run() {
	    
	    //Whatever you want to search for here. Any x or y length.
		gridList.add(new String[] {"H", " ", " ", "P"});
		gridList.add(new String[] {"?", "G", "N", "?"});
		gridList.add(new String[] {"L", "A", "?", "S"});
		gridList.add(new String[] {"S", "?", "A", "S"});
		gridList.add(new String[] {" ", "L", " ", "I"});
		gridList.add(new String[] {" ", "E", "T", "O"});
		gridList.add(new String[] {" ", "R", "I", "N"});
		gridList.add(new String[] {" ", "I", "K", " "});
		
		//First loop through and check for all words it can find.
		//Second loop check again to see if the previous check change anything for it.
		//Third loop: Same as second loop.
		for (int i = 0; i < 3; ++i) {
			if (!needChecking()) break;
			System.out.println("Starting round " + (i + 1));
			checkGrid();
		}
	}
	
	private void checkGrid() {
		for (int i = 0; i < gridList.size(); ++i) {
			for (int z = 0; z < gridList.get(i).length; ++z) {
				//Look for question marks.
				if (gridList.get(i)[z].equals("?")) {
					String x = searchX(i, z);
					String y = searchY(i, z);

					System.out.println(x);
					System.out.println(y);
					
					List<String> wordsX = getUrlResults.getSearchResults(x);
					List<String> wordsY = getUrlResults.getSearchResults(y);
					
					int indexX = x.indexOf("?");
					int indexY = y.indexOf("?");
					
					if (wordsX.size() == 1) {
						gridList.get(i)[z] = wordsX.get(0).charAt(indexX) + "";
						continue;
					}
					
					if (wordsY.size() == 1) {
						gridList.get(i)[z] = wordsY.get(0).charAt(indexY) + "";
						continue;
					}
					
					List<String> charX = new ArrayList<String>();
					List<String> charY = new ArrayList<String>();
					
					for (String word : wordsX) {
						charX.add(word.charAt(indexX) + "");
					}
					for (String word : wordsY) {
						charY.add(word.charAt(indexY) + "");
					}
					
					List<String> common = new ArrayList<String>();
					for (int x3 = 0; x3 < charX.size(); ++x3) {
						for (int y3 = 0; y3 < charY.size(); ++y3) {
							if (charX.get(x3).equals(charY.get(y3))) {
								boolean exists = false;
								for (int c = 0; c < common.size(); ++c) {
									if (common.get(c).equals(charX.get(x3))) {
										exists = true;
										break;
									}
								}
								if (!exists) {
									common.add(charX.get(x3));
								}
							}
						}
					}
					
					if (common.size() == 1) {
						gridList.get(i)[z] = common.get(0);
					}
				}
			}
		}
		
		//Print current grid-result.
		for (int i = 0; i < gridList.size(); ++i) {
			for (int z = 0; z < gridList.get(i).length; ++z) {
				System.out.print(gridList.get(i)[z]);
			}
			System.out.println();
		}
	}
	
	private String searchX(int i, int z) {
		for (int x = z; x >= 0; --x) {
			if (x <= 0
					|| gridList.get(i)[x - 1].equals(" ")) {
				String word = "";
				for (int x2 = x; x2 < gridList.get(i).length; ++x2) {
					if (gridList.get(i)[x2].equals(" ")) {
						break;
					}
					word += gridList.get(i)[x2];
				}
				return word;
			}
		}
		return "";
	}
	
	private String searchY(int i, int z) {
		for (int y = i; y >= 0; --y) {
			if (y <= 0
					|| gridList.get(y - 1)[z].equals(" ")) {
				String word = "";
				for (int y2 = y; y2 < gridList.size(); ++y2) {
					if (gridList.get(y2)[z].equals(" ")) {
						break;
					}
					word += gridList.get(y2)[z];
				}
				return word;
			}
		}
		return "";
	}
	
	private boolean needChecking() {
		for (int i = 0; i < gridList.size(); ++i) {
			for (int z = 0; z < gridList.get(i).length; ++z) {
				if (gridList.get(i)[z].equals("?")) return true;
			}
		}
		return false;
	}
}