Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 KB | None | 0 0
  1. /**
  2. * Created by Justin on 7/09/2017.
  3. */
  4.  
  5. import java.util.*;
  6. import java.util.Scanner;
  7. import java.util.Arrays;
  8.  
  9. public class WordSearch {
  10. private static int boardBounds;
  11. private static char[][] board;
  12. private static char[][] solutionBoard;
  13.  
  14.  
  15. public static void printIntro() {
  16. System.out.println("Welcome to a Word Search Generator created by Justin!");
  17. System.out.println("You will be able to choose your size, and create a word search for yourself");
  18. System.out.println("Please review the options below and have fun!");
  19. System.out.println("");
  20. System.out.println("Type in (g) to generate a new word search");
  21. System.out.println("Type in (p) to print out the word search to console");
  22. System.out.println("Type in (s) to show the solution to your word search");
  23. System.out.println("Type in (q) to quit the program :-(");
  24. }
  25.  
  26. public static void inputFromUser() {
  27. Scanner sc = new Scanner(System.in);
  28. switch (sc.next()) {
  29. case "g":
  30. generate();
  31. break;
  32. case "p":
  33. print();
  34. break;
  35. case "s":
  36. //showSolution();
  37. break;
  38. case "q":
  39. System.exit(0);
  40.  
  41. }
  42. }
  43.  
  44. public static void generate() {
  45. int genWords;
  46. Scanner genScanner = new Scanner(System.in);
  47.  
  48. System.out.println("How many words would you like to be placed into the search?");
  49. genWords = genScanner.nextInt();
  50.  
  51. System.out.println("Please input the words separated by commas (i.e. 'java, cats, beer, cheese, burgers')' ");
  52. genScanner.nextLine();
  53. String[] wordInput = genScanner.nextLine().replace(" ", "").split(",");
  54.  
  55. while (wordInput.length != genWords) {
  56. System.out.println("You must input " + genWords + " words");
  57. wordInput = genScanner.nextLine().replace(" ", "").split(",");
  58. }
  59.  
  60.  
  61. boardBounds = getGenSize(wordInput);
  62. initBoard(boardBounds);
  63. solutionBoard = new char[boardBounds][boardBounds];
  64.  
  65.  
  66. for (String words : wordInput) {
  67. Random random = new Random();
  68. int picker = random.nextInt(3);
  69.  
  70. for (int i = 0; i < 3; i++) {
  71. switch ((i + picker) % 3) {
  72. case 1:
  73. isVertical(words);
  74. break;
  75. case 2:
  76. isHorizontal(words);
  77. break;
  78. case 3:
  79. isDiagonal(words);
  80. break;
  81. }
  82. }
  83. }
  84. for (int i = 0; i < boardBounds; i++) {
  85. for (int j = 0; j < boardBounds; j++) {
  86. if (solutionBoard[i][j] != 0) board[i][j] = solutionBoard[i][j];
  87.  
  88. }
  89.  
  90. }
  91. }
  92.  
  93. public static void isVertical(String words) {
  94. int tries = 0;
  95. tries++;
  96. while (tries < 100) {
  97. Random r = new Random();
  98. int i = r.nextInt(boardBounds - words.length() + 1);
  99. int j = r.nextInt(boardBounds - words.length() + 1);
  100. if (checkDown(i, j, words)) {
  101. for (int k = 0; k < words.length(); k++) {
  102. solutionBoard[i][j + k] = words.charAt(k);
  103.  
  104. }
  105. }
  106. }
  107. }
  108.  
  109.  
  110. public static void isHorizontal(String words) {
  111. int tries = 0;
  112. tries++;
  113. while (tries < 100) {
  114. Random r = new Random();
  115. int i = r.nextInt(boardBounds - words.length() + 1);
  116. int j = r.nextInt(boardBounds - words.length() + 1);
  117. if (checkRight(i, j, words)) {
  118. for (int k = 0; k < words.length(); k++) {
  119. solutionBoard[i + k][j] = words.charAt(k);
  120.  
  121. }
  122. }
  123. }
  124. }
  125.  
  126. public static void isDiagonal(String words) {
  127. int tries = 0;
  128. tries++;
  129. while (tries < 100) {
  130. Random r = new Random();
  131. int i = r.nextInt(boardBounds - words.length() + 1);
  132. int j = r.nextInt(boardBounds - words.length() + 1);
  133. if (checkRight(i, j, words)) {
  134. for (int k = 0; k < words.length(); k++) {
  135. solutionBoard[i + k][j + k] = words.charAt(k);
  136.  
  137. }
  138. }
  139. }
  140. }
  141.  
  142. public static int getGenSize(String[] words) {
  143. int longWord = 0;
  144. for (String word : words)
  145. if (word.length() > longWord)
  146. longWord = word.length();
  147. return longWord;
  148.  
  149.  
  150. }
  151.  
  152. public static void initBoard(int bounds) {
  153. Random random = new Random();
  154. board = new char[bounds][bounds];
  155. for (int i = 0; i < bounds; i++) {
  156. for (int j = 0; j < bounds; j++) {
  157. board[i][j] = (char) (random.nextInt(26) + 'a');
  158.  
  159. }
  160. }
  161. }
  162.  
  163.  
  164. public static void print() {
  165. if (boardBounds == 0) {
  166. System.out.println("Must generate board first");
  167. System.out.println();
  168. printIntro();
  169. inputFromUser();
  170. }
  171. for (int i = 0; i < boardBounds; i++) {
  172. for (int j = 0; j < boardBounds; j++) {
  173. System.out.print(board[i][j] + " ");
  174.  
  175. }
  176.  
  177. }
  178. }
  179.  
  180. public static boolean checkDown(int i, int j, String words) {
  181.  
  182. if (j + words.length() < j + boardBounds) {
  183. return false;
  184. }
  185. for (int k = 0; k < words.length(); k++) {
  186. if (solutionBoard[i][j + k] != words.charAt(k) && solutionBoard[i][j + k] !=' ') {
  187. return false;
  188. }
  189. }
  190. return true;
  191. }
  192.  
  193.  
  194.  
  195.  
  196. public static boolean checkRight(int i, int j, String words) {
  197. if (i + words.length() < i + boardBounds) {
  198. return false;
  199. }
  200. for (int k = 0; k < words.length(); k++) {
  201. if (solutionBoard[i + k][j] != words.charAt(k) && solutionBoard[i + k][j] !=' ') {
  202. return false;
  203. }
  204. }
  205. return true;
  206. }
  207.  
  208. public static boolean checkDiagonal(int i, int j, String words) {
  209. if (j + words.length() < j + boardBounds || i + words.length() < i + words.length()){
  210. return false;
  211. }
  212. for (int k = 0; k < words.length(); k++) {
  213. if (solutionBoard[i + k][j + k] != words.charAt(k) && solutionBoard[i + k][j + k] !=' ') {
  214. return false;
  215. }
  216. }
  217. return true;
  218. }
  219.  
  220.  
  221. public static void main(String[] args) {
  222. printIntro();
  223. inputFromUser();
  224.  
  225. }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement