Advertisement
ShadowZek

Untitled

Apr 19th, 2019
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3. public class WordMaker {
  4. private static int listSize = 0;//Determines size of the word list being read.
  5. public static int DEF_SIZE = 100000000;//Arbitrary size for the list of words.
  6. public static void main (String[] args)
  7. {
  8. char[][] graph = { { 'R','A','H', 'J', 'M' },
  9. { 'Y','U','W', 'W', 'K' },
  10. { 'R','X','N', 'F', 'M' },
  11. { 'Q','G','E', 'E', 'B' },
  12. { 'E','O','A', 'P', 'E' } };
  13. File dictionary = new File("dict.txt");
  14. String[] wordList = new String[DEF_SIZE];//String array for the file being read, stores the words we want.
  15. readAFile(dictionary, wordList);
  16. for (int i = 0; i < wordList.length; i++)
  17. {
  18. findAWord(graph, wordList[i]);
  19. }
  20. }
  21. //Sweeps through the char array, determines whether one of the cells matches a word.
  22. public static boolean findAWord(char[][] board, String word) {
  23. boolean result;
  24. for (int i = 0; i < board.length; i++)
  25. {
  26. for (int j = 0; j < board[i].length; j++)
  27. {
  28. if (board[i][j] == word.charAt(0))
  29. {
  30. result = DFS(board, word, i, j, 0);
  31. if (result)
  32. {
  33. return true;
  34. }
  35. }
  36. }
  37. }
  38. return false;
  39. }
  40. private static boolean DFS(char[][] board, String word, int i, int j, int index) {
  41. //If we find the word,
  42. if (index == word.length()) {
  43. return true;
  44. }
  45. //If we go out of bounds.
  46. if (i < 0 || i >= board.length || j < 0 || j >= board[i].length) {
  47. return false;
  48. }
  49. //If the current index of the char array we are looking at does not match the current character of the word we are analyzing.
  50. if (board[i][j] != word.charAt(index)){
  51. return false;
  52. }
  53. //Boolean to determine whether we have found the word or not.
  54. boolean result;
  55. board[i][j] += 1000;//Makes the current cell invalid, so it can not be used again (allows traversal of the character array).
  56. result = DFS(board, word, i - 1, j, index + 1)
  57. || DFS(board, word, i + 1, j, index + 1)
  58. || DFS(board, word, i, j - 1, index + 1)
  59. || DFS(board, word, i, j + 1, index + 1);
  60. board[i][j] -= 1000;//Resets the current cell, making it valid again for the next word to be found.
  61. return result;
  62. }
  63. //Reading a file.
  64. public static String[] readAFile(File fileName, String[] wordList)
  65. {
  66. try
  67. {
  68. Scanner fileScanner = new Scanner(fileName);
  69. while (fileScanner.hasNextLine())
  70. {
  71. if (fileScanner.next().length() >= 2 && fileScanner.next().length() <= 6)
  72. {
  73. wordList[listSize++] = fileScanner.next().toLowerCase();
  74. }
  75. }
  76. fileScanner.close();
  77. }
  78. catch (Exception e)
  79. {
  80. System.out.println(e);
  81. }
  82. return wordList;
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement