Newer
Older
KA-Visual / src / showGrid.java
Jonathan Ström on 21 Feb 2017 6 KB Minor changes.
import java.util.ArrayList;
import java.util.List;

public class showGrid {
	private List<String[]> gridList = new ArrayList<String[]>();
	private List<String[]> gridList2 = new ArrayList<String[]>();
	private List<String[]> gridList3 = new ArrayList<String[]>();
	
	public void run() {
		gridList3.add(new String[] { " ", " ", " ", " " });
		gridList3.add(new String[] { " ", " ", " ", " " });
		gridList3.add(new String[] { " ", " ", " ", " " });
		gridList3.add(new String[] { " ", " ", " ", " " });
		gridList3.add(new String[] { " ", " ", " ", " " });
		gridList3.add(new String[] { " ", " ", " ", " " });
		gridList3.add(new String[] { " ", " ", " ", " " });
		gridList3.add(new String[] { " ", " ", " ", " " });
		
		gridList2.add(new String[] { " ", " ", " ", " " });
		gridList2.add(new String[] { " ", " ", " ", " " });
		gridList2.add(new String[] { " ", " ", " ", " " });
		gridList2.add(new String[] { " ", " ", " ", " " });
		gridList2.add(new String[] { " ", " ", " ", " " });
		gridList2.add(new String[] { " ", " ", " ", " " });
		gridList2.add(new String[] { " ", " ", " ", " " });
		gridList2.add(new String[] { " ", " ", " ", " " });
		
		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", " "});
		
		for (int i = 0; i < gridList.size(); ++i) {
			for (int z = 0; z < gridList.get(i).length; ++z) {
				if (!gridList.get(i)[z].equals(" ")) {
					if (z + 1 < gridList.get(i).length
						&& i + 1 < gridList.size()) {
						if (!gridList.get(i)[z + 1].equals(" ")
								&& !gridList.get(i + 1)[z].equals(" ")) {
							gridList2.get(i)[z] = "B";
						}
						else if (!gridList.get(i)[z + 1].equals(" ")) {
							gridList2.get(i)[z] = "R";
						}
						else if (!gridList.get(i + 1)[z].equals(" ")) {
							gridList2.get(i)[z] = "D";
						}
					} else {
						if (z + 1 == gridList.get(i).length
								&& i + 1 < gridList.size()) {
							gridList2.get(i)[z] = "D";
						}
						else if (z + 1 < gridList.get(i).length
								&& i + 1 == gridList.size()) {
							gridList2.get(i)[z] = "R";
						}
						else {
							gridList2.get(i)[z] = "E";
						}
					}
				}

				System.out.print(gridList2.get(i)[z]);
			}
			System.out.println();
		}
		
		for (int i = 0; i < 3; ++i) {
			if (!needChecking()) break;
			System.out.println("Starting round " + (i + 1));
			checkGrid();
		}
		
		//List words found
		/*for (int i = 0; i < gridList.size(); ++i) {
			for (int z = 0; z < gridList.get(i).length; ++z) {
				if (gridList2.get(i)[z].equals("B")) {
					if (i <= 0
							|| gridList2.get(i - 1)[z].equals(" ")) {
						
						String word = "";
						for (int y = i; y < gridList.size(); ++y) {
							if (y + 1 < gridList.size()
									&& gridList.get(y)[z].equals(" ")) {
								break;
							}
							word += gridList.get(y)[z];
						}
						System.out.println("Word3: " + word);
					}
					if (z <= 0
							|| gridList2.get(i)[z - 1].equals(" ")) {
						
						String word = "";
						for (int x = z; x < gridList.get(i).length; ++x) {
							if (x + 1 < gridList.size()
									&& gridList.get(i)[x].equals(" ")) {
								break;
							}
							word += gridList.get(i)[x];
						}
						System.out.println("Word3: " + word);
					}
				}
				if (gridList2.get(i)[z].equals("D")) {
					if (i <= 0
							|| gridList2.get(i - 1)[z].equals(" ")) {
						
						String word = "";
						for (int y = i; y < gridList.size(); ++y) {
							if (y + 1 < gridList.size()
									&& gridList.get(y)[z].equals(" ")) {
								break;
							}
							word += gridList.get(y)[z];
						}
						System.out.println("Word: " + word);
					}
				}
				else if(gridList2.get(i)[z].equals("R")) {
					if (z <= 0
							|| gridList2.get(i)[z - 1].equals(" ")) {
						
						String word = "";
						for (int x = z; x < gridList.get(i).length; ++x) {
							if (x + 1 < gridList.size()
									&& gridList.get(i)[x].equals(" ")) {
								break;
							}
							word += gridList.get(i)[x];
						}
						System.out.println("Word2: " + word);
					}
				}
			}
		}
			*/
	}
	
	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);
					}
				}
			}
		}
		
		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
					|| gridList2.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
					|| gridList2.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;
	}
}